allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / jbd / journal.c
blob3e1a628ff96827ce7e75abdb1358a610c9d9f454
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>
39 #include <asm/uaccess.h>
40 #include <asm/page.h>
42 EXPORT_SYMBOL(journal_start);
43 EXPORT_SYMBOL(journal_restart);
44 EXPORT_SYMBOL(journal_extend);
45 EXPORT_SYMBOL(journal_stop);
46 EXPORT_SYMBOL(journal_lock_updates);
47 EXPORT_SYMBOL(journal_unlock_updates);
48 EXPORT_SYMBOL(journal_get_write_access);
49 EXPORT_SYMBOL(journal_get_create_access);
50 EXPORT_SYMBOL(journal_get_undo_access);
51 EXPORT_SYMBOL(journal_dirty_data);
52 EXPORT_SYMBOL(journal_dirty_metadata);
53 EXPORT_SYMBOL(journal_release_buffer);
54 EXPORT_SYMBOL(journal_forget);
55 #if 0
56 EXPORT_SYMBOL(journal_sync_buffer);
57 #endif
58 EXPORT_SYMBOL(journal_flush);
59 EXPORT_SYMBOL(journal_revoke);
61 EXPORT_SYMBOL(journal_init_dev);
62 EXPORT_SYMBOL(journal_init_inode);
63 EXPORT_SYMBOL(journal_update_format);
64 EXPORT_SYMBOL(journal_check_used_features);
65 EXPORT_SYMBOL(journal_check_available_features);
66 EXPORT_SYMBOL(journal_set_features);
67 EXPORT_SYMBOL(journal_create);
68 EXPORT_SYMBOL(journal_load);
69 EXPORT_SYMBOL(journal_destroy);
70 EXPORT_SYMBOL(journal_update_superblock);
71 EXPORT_SYMBOL(journal_abort);
72 EXPORT_SYMBOL(journal_errno);
73 EXPORT_SYMBOL(journal_ack_err);
74 EXPORT_SYMBOL(journal_clear_err);
75 EXPORT_SYMBOL(log_wait_commit);
76 EXPORT_SYMBOL(journal_start_commit);
77 EXPORT_SYMBOL(journal_force_commit_nested);
78 EXPORT_SYMBOL(journal_wipe);
79 EXPORT_SYMBOL(journal_blocks_per_page);
80 EXPORT_SYMBOL(journal_invalidatepage);
81 EXPORT_SYMBOL(journal_try_to_free_buffers);
82 EXPORT_SYMBOL(journal_force_commit);
84 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
85 static void __journal_abort_soft (journal_t *journal, int errno);
88 * Helper function used to manage commit timeouts
91 static void commit_timeout(unsigned long __data)
93 struct task_struct * p = (struct task_struct *) __data;
95 wake_up_process(p);
99 * kjournald: The main thread function used to manage a logging device
100 * journal.
102 * This kernel thread is responsible for two things:
104 * 1) COMMIT: Every so often we need to commit the current state of the
105 * filesystem to disk. The journal thread is responsible for writing
106 * all of the metadata buffers to disk.
108 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
109 * of the data in that part of the log has been rewritten elsewhere on
110 * the disk. Flushing these old buffers to reclaim space in the log is
111 * known as checkpointing, and this thread is responsible for that job.
114 static int kjournald(void *arg)
116 journal_t *journal = arg;
117 transaction_t *transaction;
120 * Set up an interval timer which can be used to trigger a commit wakeup
121 * after the commit interval expires
123 setup_timer(&journal->j_commit_timer, commit_timeout,
124 (unsigned long)current);
126 /* Record that the journal thread is running */
127 journal->j_task = current;
128 wake_up(&journal->j_wait_done_commit);
130 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
131 journal->j_commit_interval / HZ);
134 * And now, wait forever for commit wakeup events.
136 spin_lock(&journal->j_state_lock);
138 loop:
139 if (journal->j_flags & JFS_UNMOUNT)
140 goto end_loop;
142 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
143 journal->j_commit_sequence, journal->j_commit_request);
145 if (journal->j_commit_sequence != journal->j_commit_request) {
146 jbd_debug(1, "OK, requests differ\n");
147 spin_unlock(&journal->j_state_lock);
148 del_timer_sync(&journal->j_commit_timer);
149 journal_commit_transaction(journal);
150 spin_lock(&journal->j_state_lock);
151 goto loop;
154 wake_up(&journal->j_wait_done_commit);
155 if (freezing(current)) {
157 * The simpler the better. Flushing journal isn't a
158 * good idea, because that depends on threads that may
159 * be already stopped.
161 jbd_debug(1, "Now suspending kjournald\n");
162 spin_unlock(&journal->j_state_lock);
163 refrigerator();
164 spin_lock(&journal->j_state_lock);
165 } else {
167 * We assume on resume that commits are already there,
168 * so we don't sleep
170 DEFINE_WAIT(wait);
171 int should_sleep = 1;
173 prepare_to_wait(&journal->j_wait_commit, &wait,
174 TASK_INTERRUPTIBLE);
175 if (journal->j_commit_sequence != journal->j_commit_request)
176 should_sleep = 0;
177 transaction = journal->j_running_transaction;
178 if (transaction && time_after_eq(jiffies,
179 transaction->t_expires))
180 should_sleep = 0;
181 if (journal->j_flags & JFS_UNMOUNT)
182 should_sleep = 0;
183 if (should_sleep) {
184 spin_unlock(&journal->j_state_lock);
185 schedule();
186 spin_lock(&journal->j_state_lock);
188 finish_wait(&journal->j_wait_commit, &wait);
191 jbd_debug(1, "kjournald wakes\n");
194 * Were we woken up by a commit wakeup event?
196 transaction = journal->j_running_transaction;
197 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
198 journal->j_commit_request = transaction->t_tid;
199 jbd_debug(1, "woke because of timeout\n");
201 goto loop;
203 end_loop:
204 spin_unlock(&journal->j_state_lock);
205 del_timer_sync(&journal->j_commit_timer);
206 journal->j_task = NULL;
207 wake_up(&journal->j_wait_done_commit);
208 jbd_debug(1, "Journal thread exiting.\n");
209 return 0;
212 static int journal_start_thread(journal_t *journal)
214 struct task_struct *t;
216 t = kthread_run(kjournald, journal, "kjournald");
217 if (IS_ERR(t))
218 return PTR_ERR(t);
220 wait_event(journal->j_wait_done_commit, journal->j_task != 0);
221 return 0;
224 static void journal_kill_thread(journal_t *journal)
226 spin_lock(&journal->j_state_lock);
227 journal->j_flags |= JFS_UNMOUNT;
229 while (journal->j_task) {
230 wake_up(&journal->j_wait_commit);
231 spin_unlock(&journal->j_state_lock);
232 wait_event(journal->j_wait_done_commit, journal->j_task == 0);
233 spin_lock(&journal->j_state_lock);
235 spin_unlock(&journal->j_state_lock);
239 * journal_write_metadata_buffer: write a metadata buffer to the journal.
241 * Writes a metadata buffer to a given disk block. The actual IO is not
242 * performed but a new buffer_head is constructed which labels the data
243 * to be written with the correct destination disk block.
245 * Any magic-number escaping which needs to be done will cause a
246 * copy-out here. If the buffer happens to start with the
247 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
248 * magic number is only written to the log for descripter blocks. In
249 * this case, we copy the data and replace the first word with 0, and we
250 * return a result code which indicates that this buffer needs to be
251 * marked as an escaped buffer in the corresponding log descriptor
252 * block. The missing word can then be restored when the block is read
253 * during recovery.
255 * If the source buffer has already been modified by a new transaction
256 * since we took the last commit snapshot, we use the frozen copy of
257 * that data for IO. If we end up using the existing buffer_head's data
258 * for the write, then we *have* to lock the buffer to prevent anyone
259 * else from using and possibly modifying it while the IO is in
260 * progress.
262 * The function returns a pointer to the buffer_heads to be used for IO.
264 * We assume that the journal has already been locked in this function.
266 * Return value:
267 * <0: Error
268 * >=0: Finished OK
270 * On success:
271 * Bit 0 set == escape performed on the data
272 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
275 int journal_write_metadata_buffer(transaction_t *transaction,
276 struct journal_head *jh_in,
277 struct journal_head **jh_out,
278 unsigned long blocknr)
280 int need_copy_out = 0;
281 int done_copy_out = 0;
282 int do_escape = 0;
283 char *mapped_data;
284 struct buffer_head *new_bh;
285 struct journal_head *new_jh;
286 struct page *new_page;
287 unsigned int new_offset;
288 struct buffer_head *bh_in = jh2bh(jh_in);
291 * The buffer really shouldn't be locked: only the current committing
292 * transaction is allowed to write it, so nobody else is allowed
293 * to do any IO.
295 * akpm: except if we're journalling data, and write() output is
296 * also part of a shared mapping, and another thread has
297 * decided to launch a writepage() against this buffer.
299 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
301 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
304 * If a new transaction has already done a buffer copy-out, then
305 * we use that version of the data for the commit.
307 jbd_lock_bh_state(bh_in);
308 repeat:
309 if (jh_in->b_frozen_data) {
310 done_copy_out = 1;
311 new_page = virt_to_page(jh_in->b_frozen_data);
312 new_offset = offset_in_page(jh_in->b_frozen_data);
313 } else {
314 new_page = jh2bh(jh_in)->b_page;
315 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
318 mapped_data = kmap_atomic(new_page, KM_USER0);
320 * Check for escaping
322 if (*((__be32 *)(mapped_data + new_offset)) ==
323 cpu_to_be32(JFS_MAGIC_NUMBER)) {
324 need_copy_out = 1;
325 do_escape = 1;
327 kunmap_atomic(mapped_data, KM_USER0);
330 * Do we need to do a data copy?
332 if (need_copy_out && !done_copy_out) {
333 char *tmp;
335 jbd_unlock_bh_state(bh_in);
336 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
337 jbd_lock_bh_state(bh_in);
338 if (jh_in->b_frozen_data) {
339 jbd_free(tmp, bh_in->b_size);
340 goto repeat;
343 jh_in->b_frozen_data = tmp;
344 mapped_data = kmap_atomic(new_page, KM_USER0);
345 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
346 kunmap_atomic(mapped_data, KM_USER0);
348 new_page = virt_to_page(tmp);
349 new_offset = offset_in_page(tmp);
350 done_copy_out = 1;
354 * Did we need to do an escaping? Now we've done all the
355 * copying, we can finally do so.
357 if (do_escape) {
358 mapped_data = kmap_atomic(new_page, KM_USER0);
359 *((unsigned int *)(mapped_data + new_offset)) = 0;
360 kunmap_atomic(mapped_data, KM_USER0);
363 /* keep subsequent assertions sane */
364 new_bh->b_state = 0;
365 init_buffer(new_bh, NULL, NULL);
366 atomic_set(&new_bh->b_count, 1);
367 jbd_unlock_bh_state(bh_in);
369 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
371 set_bh_page(new_bh, new_page, new_offset);
372 new_jh->b_transaction = NULL;
373 new_bh->b_size = jh2bh(jh_in)->b_size;
374 new_bh->b_bdev = transaction->t_journal->j_dev;
375 new_bh->b_blocknr = blocknr;
376 set_buffer_mapped(new_bh);
377 set_buffer_dirty(new_bh);
379 *jh_out = new_jh;
382 * The to-be-written buffer needs to get moved to the io queue,
383 * and the original buffer whose contents we are shadowing or
384 * copying is moved to the transaction's shadow queue.
386 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
387 journal_file_buffer(jh_in, transaction, BJ_Shadow);
388 JBUFFER_TRACE(new_jh, "file as BJ_IO");
389 journal_file_buffer(new_jh, transaction, BJ_IO);
391 return do_escape | (done_copy_out << 1);
395 * Allocation code for the journal file. Manage the space left in the
396 * journal, so that we can begin checkpointing when appropriate.
400 * __log_space_left: Return the number of free blocks left in the journal.
402 * Called with the journal already locked.
404 * Called under j_state_lock
407 int __log_space_left(journal_t *journal)
409 int left = journal->j_free;
411 assert_spin_locked(&journal->j_state_lock);
414 * Be pessimistic here about the number of those free blocks which
415 * might be required for log descriptor control blocks.
418 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
420 left -= MIN_LOG_RESERVED_BLOCKS;
422 if (left <= 0)
423 return 0;
424 left -= (left >> 3);
425 return left;
429 * Called under j_state_lock. Returns true if a transaction was started.
431 int __log_start_commit(journal_t *journal, tid_t target)
434 * Are we already doing a recent enough commit?
436 if (!tid_geq(journal->j_commit_request, target)) {
438 * We want a new commit: OK, mark the request and wakup the
439 * commit thread. We do _not_ do the commit ourselves.
442 journal->j_commit_request = target;
443 jbd_debug(1, "JBD: requesting commit %d/%d\n",
444 journal->j_commit_request,
445 journal->j_commit_sequence);
446 wake_up(&journal->j_wait_commit);
447 return 1;
449 return 0;
452 int log_start_commit(journal_t *journal, tid_t tid)
454 int ret;
456 spin_lock(&journal->j_state_lock);
457 ret = __log_start_commit(journal, tid);
458 spin_unlock(&journal->j_state_lock);
459 return ret;
463 * Force and wait upon a commit if the calling process is not within
464 * transaction. This is used for forcing out undo-protected data which contains
465 * bitmaps, when the fs is running out of space.
467 * We can only force the running transaction if we don't have an active handle;
468 * otherwise, we will deadlock.
470 * Returns true if a transaction was started.
472 int journal_force_commit_nested(journal_t *journal)
474 transaction_t *transaction = NULL;
475 tid_t tid;
477 spin_lock(&journal->j_state_lock);
478 if (journal->j_running_transaction && !current->journal_info) {
479 transaction = journal->j_running_transaction;
480 __log_start_commit(journal, transaction->t_tid);
481 } else if (journal->j_committing_transaction)
482 transaction = journal->j_committing_transaction;
484 if (!transaction) {
485 spin_unlock(&journal->j_state_lock);
486 return 0; /* Nothing to retry */
489 tid = transaction->t_tid;
490 spin_unlock(&journal->j_state_lock);
491 log_wait_commit(journal, tid);
492 return 1;
496 * Start a commit of the current running transaction (if any). Returns true
497 * if a transaction was started, and fills its tid in at *ptid
499 int journal_start_commit(journal_t *journal, tid_t *ptid)
501 int ret = 0;
503 spin_lock(&journal->j_state_lock);
504 if (journal->j_running_transaction) {
505 tid_t tid = journal->j_running_transaction->t_tid;
507 ret = __log_start_commit(journal, tid);
508 if (ret && ptid)
509 *ptid = tid;
510 } else if (journal->j_committing_transaction && ptid) {
512 * If ext3_write_super() recently started a commit, then we
513 * have to wait for completion of that transaction
515 *ptid = journal->j_committing_transaction->t_tid;
516 ret = 1;
518 spin_unlock(&journal->j_state_lock);
519 return ret;
523 * Wait for a specified commit to complete.
524 * The caller may not hold the journal lock.
526 int log_wait_commit(journal_t *journal, tid_t tid)
528 int err = 0;
530 #ifdef CONFIG_JBD_DEBUG
531 spin_lock(&journal->j_state_lock);
532 if (!tid_geq(journal->j_commit_request, tid)) {
533 printk(KERN_EMERG
534 "%s: error: j_commit_request=%d, tid=%d\n",
535 __FUNCTION__, journal->j_commit_request, tid);
537 spin_unlock(&journal->j_state_lock);
538 #endif
539 spin_lock(&journal->j_state_lock);
540 while (tid_gt(tid, journal->j_commit_sequence)) {
541 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
542 tid, journal->j_commit_sequence);
543 wake_up(&journal->j_wait_commit);
544 spin_unlock(&journal->j_state_lock);
545 wait_event(journal->j_wait_done_commit,
546 !tid_gt(tid, journal->j_commit_sequence));
547 spin_lock(&journal->j_state_lock);
549 spin_unlock(&journal->j_state_lock);
551 if (unlikely(is_journal_aborted(journal))) {
552 printk(KERN_EMERG "journal commit I/O error\n");
553 err = -EIO;
555 return err;
559 * Log buffer allocation routines:
562 int journal_next_log_block(journal_t *journal, unsigned long *retp)
564 unsigned long blocknr;
566 spin_lock(&journal->j_state_lock);
567 J_ASSERT(journal->j_free > 1);
569 blocknr = journal->j_head;
570 journal->j_head++;
571 journal->j_free--;
572 if (journal->j_head == journal->j_last)
573 journal->j_head = journal->j_first;
574 spin_unlock(&journal->j_state_lock);
575 return journal_bmap(journal, blocknr, retp);
579 * Conversion of logical to physical block numbers for the journal
581 * On external journals the journal blocks are identity-mapped, so
582 * this is a no-op. If needed, we can use j_blk_offset - everything is
583 * ready.
585 int journal_bmap(journal_t *journal, unsigned long blocknr,
586 unsigned long *retp)
588 int err = 0;
589 unsigned long ret;
591 if (journal->j_inode) {
592 ret = bmap(journal->j_inode, blocknr);
593 if (ret)
594 *retp = ret;
595 else {
596 char b[BDEVNAME_SIZE];
598 printk(KERN_ALERT "%s: journal block not found "
599 "at offset %lu on %s\n",
600 __FUNCTION__,
601 blocknr,
602 bdevname(journal->j_dev, b));
603 err = -EIO;
604 __journal_abort_soft(journal, err);
606 } else {
607 *retp = blocknr; /* +journal->j_blk_offset */
609 return err;
613 * We play buffer_head aliasing tricks to write data/metadata blocks to
614 * the journal without copying their contents, but for journal
615 * descriptor blocks we do need to generate bona fide buffers.
617 * After the caller of journal_get_descriptor_buffer() has finished modifying
618 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
619 * But we don't bother doing that, so there will be coherency problems with
620 * mmaps of blockdevs which hold live JBD-controlled filesystems.
622 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
624 struct buffer_head *bh;
625 unsigned long blocknr;
626 int err;
628 err = journal_next_log_block(journal, &blocknr);
630 if (err)
631 return NULL;
633 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
634 lock_buffer(bh);
635 memset(bh->b_data, 0, journal->j_blocksize);
636 set_buffer_uptodate(bh);
637 unlock_buffer(bh);
638 BUFFER_TRACE(bh, "return this buffer");
639 return journal_add_journal_head(bh);
643 * Management for journal control blocks: functions to create and
644 * destroy journal_t structures, and to initialise and read existing
645 * journal blocks from disk. */
647 /* First: create and setup a journal_t object in memory. We initialise
648 * very few fields yet: that has to wait until we have created the
649 * journal structures from from scratch, or loaded them from disk. */
651 static journal_t * journal_init_common (void)
653 journal_t *journal;
654 int err;
656 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
657 if (!journal)
658 goto fail;
660 init_waitqueue_head(&journal->j_wait_transaction_locked);
661 init_waitqueue_head(&journal->j_wait_logspace);
662 init_waitqueue_head(&journal->j_wait_done_commit);
663 init_waitqueue_head(&journal->j_wait_checkpoint);
664 init_waitqueue_head(&journal->j_wait_commit);
665 init_waitqueue_head(&journal->j_wait_updates);
666 mutex_init(&journal->j_barrier);
667 mutex_init(&journal->j_checkpoint_mutex);
668 spin_lock_init(&journal->j_revoke_lock);
669 spin_lock_init(&journal->j_list_lock);
670 spin_lock_init(&journal->j_state_lock);
672 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
674 /* The journal is marked for error until we succeed with recovery! */
675 journal->j_flags = JFS_ABORT;
677 /* Set up a default-sized revoke table for the new mount. */
678 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
679 if (err) {
680 kfree(journal);
681 goto fail;
683 return journal;
684 fail:
685 return NULL;
688 /* journal_init_dev and journal_init_inode:
690 * Create a journal structure assigned some fixed set of disk blocks to
691 * the journal. We don't actually touch those disk blocks yet, but we
692 * need to set up all of the mapping information to tell the journaling
693 * system where the journal blocks are.
698 * journal_t * journal_init_dev() - creates an initialises a journal structure
699 * @bdev: Block device on which to create the journal
700 * @fs_dev: Device which hold journalled filesystem for this journal.
701 * @start: Block nr Start of journal.
702 * @len: Length of the journal in blocks.
703 * @blocksize: blocksize of journalling device
704 * @returns: a newly created journal_t *
706 * journal_init_dev creates a journal which maps a fixed contiguous
707 * range of blocks on an arbitrary block device.
710 journal_t * journal_init_dev(struct block_device *bdev,
711 struct block_device *fs_dev,
712 int start, int len, int blocksize)
714 journal_t *journal = journal_init_common();
715 struct buffer_head *bh;
716 int n;
718 if (!journal)
719 return NULL;
721 /* journal descriptor can store up to n blocks -bzzz */
722 journal->j_blocksize = blocksize;
723 n = journal->j_blocksize / sizeof(journal_block_tag_t);
724 journal->j_wbufsize = n;
725 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
726 if (!journal->j_wbuf) {
727 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
728 __FUNCTION__);
729 kfree(journal);
730 journal = NULL;
731 goto out;
733 journal->j_dev = bdev;
734 journal->j_fs_dev = fs_dev;
735 journal->j_blk_offset = start;
736 journal->j_maxlen = len;
738 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
739 J_ASSERT(bh != NULL);
740 journal->j_sb_buffer = bh;
741 journal->j_superblock = (journal_superblock_t *)bh->b_data;
742 out:
743 return journal;
747 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
748 * @inode: An inode to create the journal in
750 * journal_init_inode creates a journal which maps an on-disk inode as
751 * the journal. The inode must exist already, must support bmap() and
752 * must have all data blocks preallocated.
754 journal_t * journal_init_inode (struct inode *inode)
756 struct buffer_head *bh;
757 journal_t *journal = journal_init_common();
758 int err;
759 int n;
760 unsigned long blocknr;
762 if (!journal)
763 return NULL;
765 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
766 journal->j_inode = inode;
767 jbd_debug(1,
768 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
769 journal, inode->i_sb->s_id, inode->i_ino,
770 (long long) inode->i_size,
771 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
773 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
774 journal->j_blocksize = inode->i_sb->s_blocksize;
776 /* journal descriptor can store up to n blocks -bzzz */
777 n = journal->j_blocksize / sizeof(journal_block_tag_t);
778 journal->j_wbufsize = n;
779 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
780 if (!journal->j_wbuf) {
781 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
782 __FUNCTION__);
783 kfree(journal);
784 return NULL;
787 err = journal_bmap(journal, 0, &blocknr);
788 /* If that failed, give up */
789 if (err) {
790 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
791 __FUNCTION__);
792 kfree(journal);
793 return NULL;
796 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
797 J_ASSERT(bh != NULL);
798 journal->j_sb_buffer = bh;
799 journal->j_superblock = (journal_superblock_t *)bh->b_data;
801 return journal;
805 * If the journal init or create aborts, we need to mark the journal
806 * superblock as being NULL to prevent the journal destroy from writing
807 * back a bogus superblock.
809 static void journal_fail_superblock (journal_t *journal)
811 struct buffer_head *bh = journal->j_sb_buffer;
812 brelse(bh);
813 journal->j_sb_buffer = NULL;
817 * Given a journal_t structure, initialise the various fields for
818 * startup of a new journaling session. We use this both when creating
819 * a journal, and after recovering an old journal to reset it for
820 * subsequent use.
823 static int journal_reset(journal_t *journal)
825 journal_superblock_t *sb = journal->j_superblock;
826 unsigned long first, last;
828 first = be32_to_cpu(sb->s_first);
829 last = be32_to_cpu(sb->s_maxlen);
831 journal->j_first = first;
832 journal->j_last = last;
834 journal->j_head = first;
835 journal->j_tail = first;
836 journal->j_free = last - first;
838 journal->j_tail_sequence = journal->j_transaction_sequence;
839 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
840 journal->j_commit_request = journal->j_commit_sequence;
842 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
844 /* Add the dynamic fields and write it to disk. */
845 journal_update_superblock(journal, 1);
846 return journal_start_thread(journal);
850 * int journal_create() - Initialise the new journal file
851 * @journal: Journal to create. This structure must have been initialised
853 * Given a journal_t structure which tells us which disk blocks we can
854 * use, create a new journal superblock and initialise all of the
855 * journal fields from scratch.
857 int journal_create(journal_t *journal)
859 unsigned long blocknr;
860 struct buffer_head *bh;
861 journal_superblock_t *sb;
862 int i, err;
864 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
865 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
866 journal->j_maxlen);
867 journal_fail_superblock(journal);
868 return -EINVAL;
871 if (journal->j_inode == NULL) {
873 * We don't know what block to start at!
875 printk(KERN_EMERG
876 "%s: creation of journal on external device!\n",
877 __FUNCTION__);
878 BUG();
881 /* Zero out the entire journal on disk. We cannot afford to
882 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
883 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
884 for (i = 0; i < journal->j_maxlen; i++) {
885 err = journal_bmap(journal, i, &blocknr);
886 if (err)
887 return err;
888 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
889 lock_buffer(bh);
890 memset (bh->b_data, 0, journal->j_blocksize);
891 BUFFER_TRACE(bh, "marking dirty");
892 mark_buffer_dirty(bh);
893 BUFFER_TRACE(bh, "marking uptodate");
894 set_buffer_uptodate(bh);
895 unlock_buffer(bh);
896 __brelse(bh);
899 sync_blockdev(journal->j_dev);
900 jbd_debug(1, "JBD: journal cleared.\n");
902 /* OK, fill in the initial static fields in the new superblock */
903 sb = journal->j_superblock;
905 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
906 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
908 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
909 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
910 sb->s_first = cpu_to_be32(1);
912 journal->j_transaction_sequence = 1;
914 journal->j_flags &= ~JFS_ABORT;
915 journal->j_format_version = 2;
917 return journal_reset(journal);
921 * void journal_update_superblock() - Update journal sb on disk.
922 * @journal: The journal to update.
923 * @wait: Set to '0' if you don't want to wait for IO completion.
925 * Update a journal's dynamic superblock fields and write it to disk,
926 * optionally waiting for the IO to complete.
928 void journal_update_superblock(journal_t *journal, int wait)
930 journal_superblock_t *sb = journal->j_superblock;
931 struct buffer_head *bh = journal->j_sb_buffer;
934 * As a special case, if the on-disk copy is already marked as needing
935 * no recovery (s_start == 0) and there are no outstanding transactions
936 * in the filesystem, then we can safely defer the superblock update
937 * until the next commit by setting JFS_FLUSHED. This avoids
938 * attempting a write to a potential-readonly device.
940 if (sb->s_start == 0 && journal->j_tail_sequence ==
941 journal->j_transaction_sequence) {
942 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
943 "(start %ld, seq %d, errno %d)\n",
944 journal->j_tail, journal->j_tail_sequence,
945 journal->j_errno);
946 goto out;
949 spin_lock(&journal->j_state_lock);
950 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
951 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
953 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
954 sb->s_start = cpu_to_be32(journal->j_tail);
955 sb->s_errno = cpu_to_be32(journal->j_errno);
956 spin_unlock(&journal->j_state_lock);
958 BUFFER_TRACE(bh, "marking dirty");
959 mark_buffer_dirty(bh);
960 if (wait)
961 sync_dirty_buffer(bh);
962 else
963 ll_rw_block(SWRITE, 1, &bh);
965 out:
966 /* If we have just flushed the log (by marking s_start==0), then
967 * any future commit will have to be careful to update the
968 * superblock again to re-record the true start of the log. */
970 spin_lock(&journal->j_state_lock);
971 if (sb->s_start)
972 journal->j_flags &= ~JFS_FLUSHED;
973 else
974 journal->j_flags |= JFS_FLUSHED;
975 spin_unlock(&journal->j_state_lock);
979 * Read the superblock for a given journal, performing initial
980 * validation of the format.
983 static int journal_get_superblock(journal_t *journal)
985 struct buffer_head *bh;
986 journal_superblock_t *sb;
987 int err = -EIO;
989 bh = journal->j_sb_buffer;
991 J_ASSERT(bh != NULL);
992 if (!buffer_uptodate(bh)) {
993 ll_rw_block(READ, 1, &bh);
994 wait_on_buffer(bh);
995 if (!buffer_uptodate(bh)) {
996 printk (KERN_ERR
997 "JBD: IO error reading journal superblock\n");
998 goto out;
1002 sb = journal->j_superblock;
1004 err = -EINVAL;
1006 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1007 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1008 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1009 goto out;
1012 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1013 case JFS_SUPERBLOCK_V1:
1014 journal->j_format_version = 1;
1015 break;
1016 case JFS_SUPERBLOCK_V2:
1017 journal->j_format_version = 2;
1018 break;
1019 default:
1020 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1021 goto out;
1024 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1025 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1026 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1027 printk (KERN_WARNING "JBD: journal file too short\n");
1028 goto out;
1031 return 0;
1033 out:
1034 journal_fail_superblock(journal);
1035 return err;
1039 * Load the on-disk journal superblock and read the key fields into the
1040 * journal_t.
1043 static int load_superblock(journal_t *journal)
1045 int err;
1046 journal_superblock_t *sb;
1048 err = journal_get_superblock(journal);
1049 if (err)
1050 return err;
1052 sb = journal->j_superblock;
1054 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1055 journal->j_tail = be32_to_cpu(sb->s_start);
1056 journal->j_first = be32_to_cpu(sb->s_first);
1057 journal->j_last = be32_to_cpu(sb->s_maxlen);
1058 journal->j_errno = be32_to_cpu(sb->s_errno);
1060 return 0;
1065 * int journal_load() - Read journal from disk.
1066 * @journal: Journal to act on.
1068 * Given a journal_t structure which tells us which disk blocks contain
1069 * a journal, read the journal from disk to initialise the in-memory
1070 * structures.
1072 int journal_load(journal_t *journal)
1074 int err;
1075 journal_superblock_t *sb;
1077 err = load_superblock(journal);
1078 if (err)
1079 return err;
1081 sb = journal->j_superblock;
1082 /* If this is a V2 superblock, then we have to check the
1083 * features flags on it. */
1085 if (journal->j_format_version >= 2) {
1086 if ((sb->s_feature_ro_compat &
1087 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1088 (sb->s_feature_incompat &
1089 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1090 printk (KERN_WARNING
1091 "JBD: Unrecognised features on journal\n");
1092 return -EINVAL;
1096 /* Let the recovery code check whether it needs to recover any
1097 * data from the journal. */
1098 if (journal_recover(journal))
1099 goto recovery_error;
1101 /* OK, we've finished with the dynamic journal bits:
1102 * reinitialise the dynamic contents of the superblock in memory
1103 * and reset them on disk. */
1104 if (journal_reset(journal))
1105 goto recovery_error;
1107 journal->j_flags &= ~JFS_ABORT;
1108 journal->j_flags |= JFS_LOADED;
1109 return 0;
1111 recovery_error:
1112 printk (KERN_WARNING "JBD: recovery failed\n");
1113 return -EIO;
1117 * void journal_destroy() - Release a journal_t structure.
1118 * @journal: Journal to act on.
1120 * Release a journal_t structure once it is no longer in use by the
1121 * journaled object.
1123 void journal_destroy(journal_t *journal)
1125 /* Wait for the commit thread to wake up and die. */
1126 journal_kill_thread(journal);
1128 /* Force a final log commit */
1129 if (journal->j_running_transaction)
1130 journal_commit_transaction(journal);
1132 /* Force any old transactions to disk */
1134 /* Totally anal locking here... */
1135 spin_lock(&journal->j_list_lock);
1136 while (journal->j_checkpoint_transactions != NULL) {
1137 spin_unlock(&journal->j_list_lock);
1138 log_do_checkpoint(journal);
1139 spin_lock(&journal->j_list_lock);
1142 J_ASSERT(journal->j_running_transaction == NULL);
1143 J_ASSERT(journal->j_committing_transaction == NULL);
1144 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1145 spin_unlock(&journal->j_list_lock);
1147 /* We can now mark the journal as empty. */
1148 journal->j_tail = 0;
1149 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1150 if (journal->j_sb_buffer) {
1151 journal_update_superblock(journal, 1);
1152 brelse(journal->j_sb_buffer);
1155 if (journal->j_inode)
1156 iput(journal->j_inode);
1157 if (journal->j_revoke)
1158 journal_destroy_revoke(journal);
1159 kfree(journal->j_wbuf);
1160 kfree(journal);
1165 *int journal_check_used_features () - Check if features specified are used.
1166 * @journal: Journal to check.
1167 * @compat: bitmask of compatible features
1168 * @ro: bitmask of features that force read-only mount
1169 * @incompat: bitmask of incompatible features
1171 * Check whether the journal uses all of a given set of
1172 * features. Return true (non-zero) if it does.
1175 int journal_check_used_features (journal_t *journal, unsigned long compat,
1176 unsigned long ro, unsigned long incompat)
1178 journal_superblock_t *sb;
1180 if (!compat && !ro && !incompat)
1181 return 1;
1182 if (journal->j_format_version == 1)
1183 return 0;
1185 sb = journal->j_superblock;
1187 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1188 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1189 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1190 return 1;
1192 return 0;
1196 * int journal_check_available_features() - Check feature set in journalling layer
1197 * @journal: Journal to check.
1198 * @compat: bitmask of compatible features
1199 * @ro: bitmask of features that force read-only mount
1200 * @incompat: bitmask of incompatible features
1202 * Check whether the journaling code supports the use of
1203 * all of a given set of features on this journal. Return true
1204 * (non-zero) if it can. */
1206 int journal_check_available_features (journal_t *journal, unsigned long compat,
1207 unsigned long ro, unsigned long incompat)
1209 journal_superblock_t *sb;
1211 if (!compat && !ro && !incompat)
1212 return 1;
1214 sb = journal->j_superblock;
1216 /* We can support any known requested features iff the
1217 * superblock is in version 2. Otherwise we fail to support any
1218 * extended sb features. */
1220 if (journal->j_format_version != 2)
1221 return 0;
1223 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1224 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1225 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1226 return 1;
1228 return 0;
1232 * int journal_set_features () - Mark a given journal feature in the superblock
1233 * @journal: Journal to act on.
1234 * @compat: bitmask of compatible features
1235 * @ro: bitmask of features that force read-only mount
1236 * @incompat: bitmask of incompatible features
1238 * Mark a given journal feature as present on the
1239 * superblock. Returns true if the requested features could be set.
1243 int journal_set_features (journal_t *journal, unsigned long compat,
1244 unsigned long ro, unsigned long incompat)
1246 journal_superblock_t *sb;
1248 if (journal_check_used_features(journal, compat, ro, incompat))
1249 return 1;
1251 if (!journal_check_available_features(journal, compat, ro, incompat))
1252 return 0;
1254 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1255 compat, ro, incompat);
1257 sb = journal->j_superblock;
1259 sb->s_feature_compat |= cpu_to_be32(compat);
1260 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1261 sb->s_feature_incompat |= cpu_to_be32(incompat);
1263 return 1;
1268 * int journal_update_format () - Update on-disk journal structure.
1269 * @journal: Journal to act on.
1271 * Given an initialised but unloaded journal struct, poke about in the
1272 * on-disk structure to update it to the most recent supported version.
1274 int journal_update_format (journal_t *journal)
1276 journal_superblock_t *sb;
1277 int err;
1279 err = journal_get_superblock(journal);
1280 if (err)
1281 return err;
1283 sb = journal->j_superblock;
1285 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1286 case JFS_SUPERBLOCK_V2:
1287 return 0;
1288 case JFS_SUPERBLOCK_V1:
1289 return journal_convert_superblock_v1(journal, sb);
1290 default:
1291 break;
1293 return -EINVAL;
1296 static int journal_convert_superblock_v1(journal_t *journal,
1297 journal_superblock_t *sb)
1299 int offset, blocksize;
1300 struct buffer_head *bh;
1302 printk(KERN_WARNING
1303 "JBD: Converting superblock from version 1 to 2.\n");
1305 /* Pre-initialise new fields to zero */
1306 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1307 blocksize = be32_to_cpu(sb->s_blocksize);
1308 memset(&sb->s_feature_compat, 0, blocksize-offset);
1310 sb->s_nr_users = cpu_to_be32(1);
1311 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1312 journal->j_format_version = 2;
1314 bh = journal->j_sb_buffer;
1315 BUFFER_TRACE(bh, "marking dirty");
1316 mark_buffer_dirty(bh);
1317 sync_dirty_buffer(bh);
1318 return 0;
1323 * int journal_flush () - Flush journal
1324 * @journal: Journal to act on.
1326 * Flush all data for a given journal to disk and empty the journal.
1327 * Filesystems can use this when remounting readonly to ensure that
1328 * recovery does not need to happen on remount.
1331 int journal_flush(journal_t *journal)
1333 int err = 0;
1334 transaction_t *transaction = NULL;
1335 unsigned long old_tail;
1337 spin_lock(&journal->j_state_lock);
1339 /* Force everything buffered to the log... */
1340 if (journal->j_running_transaction) {
1341 transaction = journal->j_running_transaction;
1342 __log_start_commit(journal, transaction->t_tid);
1343 } else if (journal->j_committing_transaction)
1344 transaction = journal->j_committing_transaction;
1346 /* Wait for the log commit to complete... */
1347 if (transaction) {
1348 tid_t tid = transaction->t_tid;
1350 spin_unlock(&journal->j_state_lock);
1351 log_wait_commit(journal, tid);
1352 } else {
1353 spin_unlock(&journal->j_state_lock);
1356 /* ...and flush everything in the log out to disk. */
1357 spin_lock(&journal->j_list_lock);
1358 while (!err && journal->j_checkpoint_transactions != NULL) {
1359 spin_unlock(&journal->j_list_lock);
1360 err = log_do_checkpoint(journal);
1361 spin_lock(&journal->j_list_lock);
1363 spin_unlock(&journal->j_list_lock);
1364 cleanup_journal_tail(journal);
1366 /* Finally, mark the journal as really needing no recovery.
1367 * This sets s_start==0 in the underlying superblock, which is
1368 * the magic code for a fully-recovered superblock. Any future
1369 * commits of data to the journal will restore the current
1370 * s_start value. */
1371 spin_lock(&journal->j_state_lock);
1372 old_tail = journal->j_tail;
1373 journal->j_tail = 0;
1374 spin_unlock(&journal->j_state_lock);
1375 journal_update_superblock(journal, 1);
1376 spin_lock(&journal->j_state_lock);
1377 journal->j_tail = old_tail;
1379 J_ASSERT(!journal->j_running_transaction);
1380 J_ASSERT(!journal->j_committing_transaction);
1381 J_ASSERT(!journal->j_checkpoint_transactions);
1382 J_ASSERT(journal->j_head == journal->j_tail);
1383 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1384 spin_unlock(&journal->j_state_lock);
1385 return err;
1389 * int journal_wipe() - Wipe journal contents
1390 * @journal: Journal to act on.
1391 * @write: flag (see below)
1393 * Wipe out all of the contents of a journal, safely. This will produce
1394 * a warning if the journal contains any valid recovery information.
1395 * Must be called between journal_init_*() and journal_load().
1397 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1398 * we merely suppress recovery.
1401 int journal_wipe(journal_t *journal, int write)
1403 journal_superblock_t *sb;
1404 int err = 0;
1406 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1408 err = load_superblock(journal);
1409 if (err)
1410 return err;
1412 sb = journal->j_superblock;
1414 if (!journal->j_tail)
1415 goto no_recovery;
1417 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1418 write ? "Clearing" : "Ignoring");
1420 err = journal_skip_recovery(journal);
1421 if (write)
1422 journal_update_superblock(journal, 1);
1424 no_recovery:
1425 return err;
1429 * journal_dev_name: format a character string to describe on what
1430 * device this journal is present.
1433 static const char *journal_dev_name(journal_t *journal, char *buffer)
1435 struct block_device *bdev;
1437 if (journal->j_inode)
1438 bdev = journal->j_inode->i_sb->s_bdev;
1439 else
1440 bdev = journal->j_dev;
1442 return bdevname(bdev, buffer);
1446 * Journal abort has very specific semantics, which we describe
1447 * for journal abort.
1449 * Two internal function, which provide abort to te jbd layer
1450 * itself are here.
1454 * Quick version for internal journal use (doesn't lock the journal).
1455 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1456 * and don't attempt to make any other journal updates.
1458 void __journal_abort_hard(journal_t *journal)
1460 transaction_t *transaction;
1461 char b[BDEVNAME_SIZE];
1463 if (journal->j_flags & JFS_ABORT)
1464 return;
1466 printk(KERN_ERR "Aborting journal on device %s.\n",
1467 journal_dev_name(journal, b));
1469 spin_lock(&journal->j_state_lock);
1470 journal->j_flags |= JFS_ABORT;
1471 transaction = journal->j_running_transaction;
1472 if (transaction)
1473 __log_start_commit(journal, transaction->t_tid);
1474 spin_unlock(&journal->j_state_lock);
1477 /* Soft abort: record the abort error status in the journal superblock,
1478 * but don't do any other IO. */
1479 static void __journal_abort_soft (journal_t *journal, int errno)
1481 if (journal->j_flags & JFS_ABORT)
1482 return;
1484 if (!journal->j_errno)
1485 journal->j_errno = errno;
1487 __journal_abort_hard(journal);
1489 if (errno)
1490 journal_update_superblock(journal, 1);
1494 * void journal_abort () - Shutdown the journal immediately.
1495 * @journal: the journal to shutdown.
1496 * @errno: an error number to record in the journal indicating
1497 * the reason for the shutdown.
1499 * Perform a complete, immediate shutdown of the ENTIRE
1500 * journal (not of a single transaction). This operation cannot be
1501 * undone without closing and reopening the journal.
1503 * The journal_abort function is intended to support higher level error
1504 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1505 * mode.
1507 * Journal abort has very specific semantics. Any existing dirty,
1508 * unjournaled buffers in the main filesystem will still be written to
1509 * disk by bdflush, but the journaling mechanism will be suspended
1510 * immediately and no further transaction commits will be honoured.
1512 * Any dirty, journaled buffers will be written back to disk without
1513 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1514 * filesystem, but we _do_ attempt to leave as much data as possible
1515 * behind for fsck to use for cleanup.
1517 * Any attempt to get a new transaction handle on a journal which is in
1518 * ABORT state will just result in an -EROFS error return. A
1519 * journal_stop on an existing handle will return -EIO if we have
1520 * entered abort state during the update.
1522 * Recursive transactions are not disturbed by journal abort until the
1523 * final journal_stop, which will receive the -EIO error.
1525 * Finally, the journal_abort call allows the caller to supply an errno
1526 * which will be recorded (if possible) in the journal superblock. This
1527 * allows a client to record failure conditions in the middle of a
1528 * transaction without having to complete the transaction to record the
1529 * failure to disk. ext3_error, for example, now uses this
1530 * functionality.
1532 * Errors which originate from within the journaling layer will NOT
1533 * supply an errno; a null errno implies that absolutely no further
1534 * writes are done to the journal (unless there are any already in
1535 * progress).
1539 void journal_abort(journal_t *journal, int errno)
1541 __journal_abort_soft(journal, errno);
1545 * int journal_errno () - returns the journal's error state.
1546 * @journal: journal to examine.
1548 * This is the errno numbet set with journal_abort(), the last
1549 * time the journal was mounted - if the journal was stopped
1550 * without calling abort this will be 0.
1552 * If the journal has been aborted on this mount time -EROFS will
1553 * be returned.
1555 int journal_errno(journal_t *journal)
1557 int err;
1559 spin_lock(&journal->j_state_lock);
1560 if (journal->j_flags & JFS_ABORT)
1561 err = -EROFS;
1562 else
1563 err = journal->j_errno;
1564 spin_unlock(&journal->j_state_lock);
1565 return err;
1569 * int journal_clear_err () - clears the journal's error state
1570 * @journal: journal to act on.
1572 * An error must be cleared or Acked to take a FS out of readonly
1573 * mode.
1575 int journal_clear_err(journal_t *journal)
1577 int err = 0;
1579 spin_lock(&journal->j_state_lock);
1580 if (journal->j_flags & JFS_ABORT)
1581 err = -EROFS;
1582 else
1583 journal->j_errno = 0;
1584 spin_unlock(&journal->j_state_lock);
1585 return err;
1589 * void journal_ack_err() - Ack journal err.
1590 * @journal: journal to act on.
1592 * An error must be cleared or Acked to take a FS out of readonly
1593 * mode.
1595 void journal_ack_err(journal_t *journal)
1597 spin_lock(&journal->j_state_lock);
1598 if (journal->j_errno)
1599 journal->j_flags |= JFS_ACK_ERR;
1600 spin_unlock(&journal->j_state_lock);
1603 int journal_blocks_per_page(struct inode *inode)
1605 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1609 * Journal_head storage management
1611 static struct kmem_cache *journal_head_cache;
1612 #ifdef CONFIG_JBD_DEBUG
1613 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1614 #endif
1616 static int journal_init_journal_head_cache(void)
1618 int retval;
1620 J_ASSERT(journal_head_cache == 0);
1621 journal_head_cache = kmem_cache_create("journal_head",
1622 sizeof(struct journal_head),
1623 0, /* offset */
1624 0, /* flags */
1625 NULL, /* ctor */
1626 NULL); /* dtor */
1627 retval = 0;
1628 if (journal_head_cache == 0) {
1629 retval = -ENOMEM;
1630 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1632 return retval;
1635 static void journal_destroy_journal_head_cache(void)
1637 J_ASSERT(journal_head_cache != NULL);
1638 kmem_cache_destroy(journal_head_cache);
1639 journal_head_cache = NULL;
1643 * journal_head splicing and dicing
1645 static struct journal_head *journal_alloc_journal_head(void)
1647 struct journal_head *ret;
1648 static unsigned long last_warning;
1650 #ifdef CONFIG_JBD_DEBUG
1651 atomic_inc(&nr_journal_heads);
1652 #endif
1653 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1654 if (ret == 0) {
1655 jbd_debug(1, "out of memory for journal_head\n");
1656 if (time_after(jiffies, last_warning + 5*HZ)) {
1657 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1658 __FUNCTION__);
1659 last_warning = jiffies;
1661 while (ret == 0) {
1662 yield();
1663 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1666 return ret;
1669 static void journal_free_journal_head(struct journal_head *jh)
1671 #ifdef CONFIG_JBD_DEBUG
1672 atomic_dec(&nr_journal_heads);
1673 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1674 #endif
1675 kmem_cache_free(journal_head_cache, jh);
1679 * A journal_head is attached to a buffer_head whenever JBD has an
1680 * interest in the buffer.
1682 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1683 * is set. This bit is tested in core kernel code where we need to take
1684 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1685 * there.
1687 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1689 * When a buffer has its BH_JBD bit set it is immune from being released by
1690 * core kernel code, mainly via ->b_count.
1692 * A journal_head may be detached from its buffer_head when the journal_head's
1693 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1694 * Various places in JBD call journal_remove_journal_head() to indicate that the
1695 * journal_head can be dropped if needed.
1697 * Various places in the kernel want to attach a journal_head to a buffer_head
1698 * _before_ attaching the journal_head to a transaction. To protect the
1699 * journal_head in this situation, journal_add_journal_head elevates the
1700 * journal_head's b_jcount refcount by one. The caller must call
1701 * journal_put_journal_head() to undo this.
1703 * So the typical usage would be:
1705 * (Attach a journal_head if needed. Increments b_jcount)
1706 * struct journal_head *jh = journal_add_journal_head(bh);
1707 * ...
1708 * jh->b_transaction = xxx;
1709 * journal_put_journal_head(jh);
1711 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1712 * because it has a non-zero b_transaction.
1716 * Give a buffer_head a journal_head.
1718 * Doesn't need the journal lock.
1719 * May sleep.
1721 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1723 struct journal_head *jh;
1724 struct journal_head *new_jh = NULL;
1726 repeat:
1727 if (!buffer_jbd(bh)) {
1728 new_jh = journal_alloc_journal_head();
1729 memset(new_jh, 0, sizeof(*new_jh));
1732 jbd_lock_bh_journal_head(bh);
1733 if (buffer_jbd(bh)) {
1734 jh = bh2jh(bh);
1735 } else {
1736 J_ASSERT_BH(bh,
1737 (atomic_read(&bh->b_count) > 0) ||
1738 (bh->b_page && bh->b_page->mapping));
1740 if (!new_jh) {
1741 jbd_unlock_bh_journal_head(bh);
1742 goto repeat;
1745 jh = new_jh;
1746 new_jh = NULL; /* We consumed it */
1747 set_buffer_jbd(bh);
1748 bh->b_private = jh;
1749 jh->b_bh = bh;
1750 get_bh(bh);
1751 BUFFER_TRACE(bh, "added journal_head");
1753 jh->b_jcount++;
1754 jbd_unlock_bh_journal_head(bh);
1755 if (new_jh)
1756 journal_free_journal_head(new_jh);
1757 return bh->b_private;
1761 * Grab a ref against this buffer_head's journal_head. If it ended up not
1762 * having a journal_head, return NULL
1764 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1766 struct journal_head *jh = NULL;
1768 jbd_lock_bh_journal_head(bh);
1769 if (buffer_jbd(bh)) {
1770 jh = bh2jh(bh);
1771 jh->b_jcount++;
1773 jbd_unlock_bh_journal_head(bh);
1774 return jh;
1777 static void __journal_remove_journal_head(struct buffer_head *bh)
1779 struct journal_head *jh = bh2jh(bh);
1781 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1783 get_bh(bh);
1784 if (jh->b_jcount == 0) {
1785 if (jh->b_transaction == NULL &&
1786 jh->b_next_transaction == NULL &&
1787 jh->b_cp_transaction == NULL) {
1788 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1789 J_ASSERT_BH(bh, buffer_jbd(bh));
1790 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1791 BUFFER_TRACE(bh, "remove journal_head");
1792 if (jh->b_frozen_data) {
1793 printk(KERN_WARNING "%s: freeing "
1794 "b_frozen_data\n",
1795 __FUNCTION__);
1796 jbd_free(jh->b_frozen_data, bh->b_size);
1798 if (jh->b_committed_data) {
1799 printk(KERN_WARNING "%s: freeing "
1800 "b_committed_data\n",
1801 __FUNCTION__);
1802 jbd_free(jh->b_committed_data, bh->b_size);
1804 bh->b_private = NULL;
1805 jh->b_bh = NULL; /* debug, really */
1806 clear_buffer_jbd(bh);
1807 __brelse(bh);
1808 journal_free_journal_head(jh);
1809 } else {
1810 BUFFER_TRACE(bh, "journal_head was locked");
1816 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1817 * and has a zero b_jcount then remove and release its journal_head. If we did
1818 * see that the buffer is not used by any transaction we also "logically"
1819 * decrement ->b_count.
1821 * We in fact take an additional increment on ->b_count as a convenience,
1822 * because the caller usually wants to do additional things with the bh
1823 * after calling here.
1824 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1825 * time. Once the caller has run __brelse(), the buffer is eligible for
1826 * reaping by try_to_free_buffers().
1828 void journal_remove_journal_head(struct buffer_head *bh)
1830 jbd_lock_bh_journal_head(bh);
1831 __journal_remove_journal_head(bh);
1832 jbd_unlock_bh_journal_head(bh);
1836 * Drop a reference on the passed journal_head. If it fell to zero then try to
1837 * release the journal_head from the buffer_head.
1839 void journal_put_journal_head(struct journal_head *jh)
1841 struct buffer_head *bh = jh2bh(jh);
1843 jbd_lock_bh_journal_head(bh);
1844 J_ASSERT_JH(jh, jh->b_jcount > 0);
1845 --jh->b_jcount;
1846 if (!jh->b_jcount && !jh->b_transaction) {
1847 __journal_remove_journal_head(bh);
1848 __brelse(bh);
1850 jbd_unlock_bh_journal_head(bh);
1854 * /proc tunables
1856 #if defined(CONFIG_JBD_DEBUG)
1857 int journal_enable_debug;
1858 EXPORT_SYMBOL(journal_enable_debug);
1859 #endif
1861 #if defined(CONFIG_JBD_DEBUG) && defined(CONFIG_PROC_FS)
1863 static struct proc_dir_entry *proc_jbd_debug;
1865 static int read_jbd_debug(char *page, char **start, off_t off,
1866 int count, int *eof, void *data)
1868 int ret;
1870 ret = sprintf(page + off, "%d\n", journal_enable_debug);
1871 *eof = 1;
1872 return ret;
1875 static int write_jbd_debug(struct file *file, const char __user *buffer,
1876 unsigned long count, void *data)
1878 char buf[32];
1880 if (count > ARRAY_SIZE(buf) - 1)
1881 count = ARRAY_SIZE(buf) - 1;
1882 if (copy_from_user(buf, buffer, count))
1883 return -EFAULT;
1884 buf[ARRAY_SIZE(buf) - 1] = '\0';
1885 journal_enable_debug = simple_strtoul(buf, NULL, 10);
1886 return count;
1889 #define JBD_PROC_NAME "sys/fs/jbd-debug"
1891 static void __init create_jbd_proc_entry(void)
1893 proc_jbd_debug = create_proc_entry(JBD_PROC_NAME, 0644, NULL);
1894 if (proc_jbd_debug) {
1895 /* Why is this so hard? */
1896 proc_jbd_debug->read_proc = read_jbd_debug;
1897 proc_jbd_debug->write_proc = write_jbd_debug;
1901 static void __exit remove_jbd_proc_entry(void)
1903 if (proc_jbd_debug)
1904 remove_proc_entry(JBD_PROC_NAME, NULL);
1907 #else
1909 #define create_jbd_proc_entry() do {} while (0)
1910 #define remove_jbd_proc_entry() do {} while (0)
1912 #endif
1914 struct kmem_cache *jbd_handle_cache;
1916 static int __init journal_init_handle_cache(void)
1918 jbd_handle_cache = kmem_cache_create("journal_handle",
1919 sizeof(handle_t),
1920 0, /* offset */
1921 0, /* flags */
1922 NULL, /* ctor */
1923 NULL); /* dtor */
1924 if (jbd_handle_cache == NULL) {
1925 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1926 return -ENOMEM;
1928 return 0;
1931 static void journal_destroy_handle_cache(void)
1933 if (jbd_handle_cache)
1934 kmem_cache_destroy(jbd_handle_cache);
1938 * Module startup and shutdown
1941 static int __init journal_init_caches(void)
1943 int ret;
1945 ret = journal_init_revoke_caches();
1946 if (ret == 0)
1947 ret = journal_init_journal_head_cache();
1948 if (ret == 0)
1949 ret = journal_init_handle_cache();
1950 return ret;
1953 static void journal_destroy_caches(void)
1955 journal_destroy_revoke_caches();
1956 journal_destroy_journal_head_cache();
1957 journal_destroy_handle_cache();
1960 static int __init journal_init(void)
1962 int ret;
1964 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
1966 ret = journal_init_caches();
1967 if (ret != 0)
1968 journal_destroy_caches();
1969 create_jbd_proc_entry();
1970 return ret;
1973 static void __exit journal_exit(void)
1975 #ifdef CONFIG_JBD_DEBUG
1976 int n = atomic_read(&nr_journal_heads);
1977 if (n)
1978 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
1979 #endif
1980 remove_jbd_proc_entry();
1981 journal_destroy_caches();
1984 MODULE_LICENSE("GPL");
1985 module_init(journal_init);
1986 module_exit(journal_exit);