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
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>
28 #include <linux/jbd.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/smp_lock.h>
32 #include <linux/init.h>
34 #include <linux/suspend.h>
35 #include <linux/pagemap.h>
36 #include <asm/uaccess.h>
37 #include <linux/proc_fs.h>
39 EXPORT_SYMBOL(journal_start);
40 EXPORT_SYMBOL(journal_restart);
41 EXPORT_SYMBOL(journal_extend);
42 EXPORT_SYMBOL(journal_stop);
43 EXPORT_SYMBOL(journal_lock_updates);
44 EXPORT_SYMBOL(journal_unlock_updates);
45 EXPORT_SYMBOL(journal_get_write_access);
46 EXPORT_SYMBOL(journal_get_create_access);
47 EXPORT_SYMBOL(journal_get_undo_access);
48 EXPORT_SYMBOL(journal_dirty_data);
49 EXPORT_SYMBOL(journal_dirty_metadata);
50 EXPORT_SYMBOL(journal_release_buffer);
51 EXPORT_SYMBOL(journal_forget);
53 EXPORT_SYMBOL(journal_sync_buffer);
55 EXPORT_SYMBOL(journal_flush);
56 EXPORT_SYMBOL(journal_revoke);
57 EXPORT_SYMBOL(journal_callback_set);
59 EXPORT_SYMBOL(journal_init_dev);
60 EXPORT_SYMBOL(journal_init_inode);
61 EXPORT_SYMBOL(journal_update_format);
62 EXPORT_SYMBOL(journal_check_used_features);
63 EXPORT_SYMBOL(journal_check_available_features);
64 EXPORT_SYMBOL(journal_set_features);
65 EXPORT_SYMBOL(journal_create);
66 EXPORT_SYMBOL(journal_load);
67 EXPORT_SYMBOL(journal_destroy);
68 EXPORT_SYMBOL(journal_recover);
69 EXPORT_SYMBOL(journal_update_superblock);
70 EXPORT_SYMBOL(journal_abort);
71 EXPORT_SYMBOL(journal_errno);
72 EXPORT_SYMBOL(journal_ack_err);
73 EXPORT_SYMBOL(journal_clear_err);
74 EXPORT_SYMBOL(log_wait_commit);
75 EXPORT_SYMBOL(journal_start_commit);
76 EXPORT_SYMBOL(journal_wipe);
77 EXPORT_SYMBOL(journal_blocks_per_page);
78 EXPORT_SYMBOL(journal_invalidatepage);
79 EXPORT_SYMBOL(journal_try_to_free_buffers);
80 EXPORT_SYMBOL(journal_bmap);
81 EXPORT_SYMBOL(journal_force_commit);
83 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
86 * Helper function used to manage commit timeouts
89 static void commit_timeout(unsigned long __data)
91 struct task_struct * p = (struct task_struct *) __data;
96 /* Static check for data structure consistency. There's no code
97 * invoked --- we'll just get a linker failure if things aren't right.
99 void __journal_internal_check(void)
101 extern void journal_bad_superblock_size(void);
102 if (sizeof(struct journal_superblock_s) != 1024)
103 journal_bad_superblock_size();
107 * kjournald: The main thread function used to manage a logging device
110 * This kernel thread is responsible for two things:
112 * 1) COMMIT: Every so often we need to commit the current state of the
113 * filesystem to disk. The journal thread is responsible for writing
114 * all of the metadata buffers to disk.
116 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
117 * of the data in that part of the log has been rewritten elsewhere on
118 * the disk. Flushing these old buffers to reclaim space in the log is
119 * known as checkpointing, and this thread is responsible for that job.
122 journal_t *current_journal; // AKPM: debug
124 int kjournald(void *arg)
126 journal_t *journal = (journal_t *) arg;
127 transaction_t *transaction;
128 struct timer_list timer;
130 current_journal = journal;
132 daemonize("kjournald");
134 /* Set up an interval timer which can be used to trigger a
135 commit wakeup after the commit interval expires */
137 timer.data = (unsigned long) current;
138 timer.function = commit_timeout;
139 journal->j_commit_timer = &timer;
141 /* Record that the journal thread is running */
142 journal->j_task = current;
143 wake_up(&journal->j_wait_done_commit);
145 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
146 journal->j_commit_interval / HZ);
149 * And now, wait forever for commit wakeup events.
151 spin_lock(&journal->j_state_lock);
154 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
155 journal->j_commit_sequence, journal->j_commit_request);
157 if (journal->j_commit_sequence != journal->j_commit_request) {
158 jbd_debug(1, "OK, requests differ\n");
159 spin_unlock(&journal->j_state_lock);
160 del_timer_sync(journal->j_commit_timer);
161 journal_commit_transaction(journal);
162 spin_lock(&journal->j_state_lock);
166 wake_up(&journal->j_wait_done_commit);
167 if (current->flags & PF_FREEZE) {
169 * The simpler the better. Flushing journal isn't a
170 * good idea, because that depends on threads that may
171 * be already stopped.
173 jbd_debug(1, "Now suspending kjournald\n");
174 spin_unlock(&journal->j_state_lock);
175 refrigerator(PF_FREEZE);
176 spin_lock(&journal->j_state_lock);
179 * We assume on resume that commits are already there,
183 int should_sleep = 1;
185 prepare_to_wait(&journal->j_wait_commit, &wait,
187 if (journal->j_commit_sequence != journal->j_commit_request)
189 transaction = journal->j_running_transaction;
190 if (transaction && time_after_eq(jiffies,
191 transaction->t_expires))
194 spin_unlock(&journal->j_state_lock);
196 spin_lock(&journal->j_state_lock);
198 finish_wait(&journal->j_wait_commit, &wait);
201 jbd_debug(1, "kjournald wakes\n");
204 * Were we woken up by a commit wakeup event?
206 transaction = journal->j_running_transaction;
207 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
208 journal->j_commit_request = transaction->t_tid;
209 jbd_debug(1, "woke because of timeout\n");
212 if (!(journal->j_flags & JFS_UNMOUNT))
215 spin_unlock(&journal->j_state_lock);
216 del_timer_sync(journal->j_commit_timer);
217 journal->j_task = NULL;
218 wake_up(&journal->j_wait_done_commit);
219 jbd_debug(1, "Journal thread exiting.\n");
223 static void journal_start_thread(journal_t *journal)
225 kernel_thread(kjournald, journal, CLONE_VM|CLONE_FS|CLONE_FILES);
226 wait_event(journal->j_wait_done_commit, journal->j_task != 0);
229 static void journal_kill_thread(journal_t *journal)
231 spin_lock(&journal->j_state_lock);
232 journal->j_flags |= JFS_UNMOUNT;
234 while (journal->j_task) {
235 wake_up(&journal->j_wait_commit);
236 spin_unlock(&journal->j_state_lock);
237 wait_event(journal->j_wait_done_commit, journal->j_task == 0);
238 spin_lock(&journal->j_state_lock);
240 spin_unlock(&journal->j_state_lock);
244 * journal_write_metadata_buffer: write a metadata buffer to the journal.
246 * Writes a metadata buffer to a given disk block. The actual IO is not
247 * performed but a new buffer_head is constructed which labels the data
248 * to be written with the correct destination disk block.
250 * Any magic-number escaping which needs to be done will cause a
251 * copy-out here. If the buffer happens to start with the
252 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
253 * magic number is only written to the log for descripter blocks. In
254 * this case, we copy the data and replace the first word with 0, and we
255 * return a result code which indicates that this buffer needs to be
256 * marked as an escaped buffer in the corresponding log descriptor
257 * block. The missing word can then be restored when the block is read
260 * If the source buffer has already been modified by a new transaction
261 * since we took the last commit snapshot, we use the frozen copy of
262 * that data for IO. If we end up using the existing buffer_head's data
263 * for the write, then we *have* to lock the buffer to prevent anyone
264 * else from using and possibly modifying it while the IO is in
267 * The function returns a pointer to the buffer_heads to be used for IO.
269 * We assume that the journal has already been locked in this function.
276 * Bit 0 set == escape performed on the data
277 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
280 int journal_write_metadata_buffer(transaction_t *transaction,
281 struct journal_head *jh_in,
282 struct journal_head **jh_out,
285 int need_copy_out = 0;
286 int done_copy_out = 0;
289 struct buffer_head *new_bh;
290 struct journal_head *new_jh;
291 struct page *new_page;
292 unsigned int new_offset;
293 struct buffer_head *bh_in = jh2bh(jh_in);
296 * The buffer really shouldn't be locked: only the current committing
297 * transaction is allowed to write it, so nobody else is allowed
300 * akpm: except if we're journalling data, and write() output is
301 * also part of a shared mapping, and another thread has
302 * decided to launch a writepage() against this buffer.
304 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
306 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
309 * If a new transaction has already done a buffer copy-out, then
310 * we use that version of the data for the commit.
312 jbd_lock_bh_state(bh_in);
314 if (jh_in->b_frozen_data) {
316 new_page = virt_to_page(jh_in->b_frozen_data);
317 new_offset = offset_in_page(jh_in->b_frozen_data);
319 new_page = jh2bh(jh_in)->b_page;
320 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
323 mapped_data = kmap_atomic(new_page, KM_USER0);
327 if (*((unsigned int *)(mapped_data + new_offset)) ==
328 htonl(JFS_MAGIC_NUMBER)) {
332 kunmap_atomic(mapped_data, KM_USER0);
335 * Do we need to do a data copy?
337 if (need_copy_out && !done_copy_out) {
340 jbd_unlock_bh_state(bh_in);
341 tmp = jbd_rep_kmalloc(bh_in->b_size, GFP_NOFS);
342 jbd_lock_bh_state(bh_in);
343 if (jh_in->b_frozen_data) {
348 jh_in->b_frozen_data = tmp;
349 mapped_data = kmap_atomic(new_page, KM_USER0);
350 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
351 kunmap_atomic(mapped_data, KM_USER0);
353 new_page = virt_to_page(tmp);
354 new_offset = offset_in_page(tmp);
359 * Did we need to do an escaping? Now we've done all the
360 * copying, we can finally do so.
363 mapped_data = kmap_atomic(new_page, KM_USER0);
364 *((unsigned int *)(mapped_data + new_offset)) = 0;
365 kunmap_atomic(mapped_data, KM_USER0);
368 /* keep subsequent assertions sane */
370 init_buffer(new_bh, NULL, NULL);
371 atomic_set(&new_bh->b_count, 1);
372 jbd_unlock_bh_state(bh_in);
374 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
376 set_bh_page(new_bh, new_page, new_offset);
377 new_jh->b_transaction = NULL;
378 new_bh->b_size = jh2bh(jh_in)->b_size;
379 new_bh->b_bdev = transaction->t_journal->j_dev;
380 new_bh->b_blocknr = blocknr;
381 set_buffer_mapped(new_bh);
382 set_buffer_dirty(new_bh);
387 * The to-be-written buffer needs to get moved to the io queue,
388 * and the original buffer whose contents we are shadowing or
389 * copying is moved to the transaction's shadow queue.
391 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
392 journal_file_buffer(jh_in, transaction, BJ_Shadow);
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;
434 * Called under j_state_lock. Returns true if a transaction 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);
457 int log_start_commit(journal_t *journal, tid_t tid)
461 spin_lock(&journal->j_state_lock);
462 ret = __log_start_commit(journal, tid);
463 spin_unlock(&journal->j_state_lock);
468 * Start a commit of the current running transaction (if any). Returns true
469 * if a transaction was started, and fills its tid in at *ptid
471 int journal_start_commit(journal_t *journal, tid_t *ptid)
475 spin_lock(&journal->j_state_lock);
476 if (journal->j_running_transaction) {
477 tid_t tid = journal->j_running_transaction->t_tid;
479 ret = __log_start_commit(journal, tid);
482 } else if (journal->j_committing_transaction && ptid) {
484 * If ext3_write_super() recently started a commit, then we
485 * have to wait for completion of that transaction
487 *ptid = journal->j_committing_transaction->t_tid;
490 spin_unlock(&journal->j_state_lock);
495 * Wait for a specified commit to complete.
496 * The caller may not hold the journal lock.
498 int log_wait_commit(journal_t *journal, tid_t tid)
502 #ifdef CONFIG_JBD_DEBUG
503 spin_lock(&journal->j_state_lock);
504 if (!tid_geq(journal->j_commit_request, tid)) {
506 "%s: error: j_commit_request=%d, tid=%d\n",
507 __FUNCTION__, journal->j_commit_request, tid);
509 spin_unlock(&journal->j_state_lock);
511 spin_lock(&journal->j_state_lock);
512 while (tid_gt(tid, journal->j_commit_sequence)) {
513 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
514 tid, journal->j_commit_sequence);
515 wake_up(&journal->j_wait_commit);
516 spin_unlock(&journal->j_state_lock);
517 wait_event(journal->j_wait_done_commit,
518 !tid_gt(tid, journal->j_commit_sequence));
519 spin_lock(&journal->j_state_lock);
521 spin_unlock(&journal->j_state_lock);
523 if (unlikely(is_journal_aborted(journal))) {
524 printk(KERN_EMERG "journal commit I/O error\n");
531 * Log buffer allocation routines:
534 int journal_next_log_block(journal_t *journal, unsigned long *retp)
536 unsigned long blocknr;
538 spin_lock(&journal->j_state_lock);
539 J_ASSERT(journal->j_free > 1);
541 blocknr = journal->j_head;
544 if (journal->j_head == journal->j_last)
545 journal->j_head = journal->j_first;
546 spin_unlock(&journal->j_state_lock);
547 return journal_bmap(journal, blocknr, retp);
551 * Conversion of logical to physical block numbers for the journal
553 * On external journals the journal blocks are identity-mapped, so
554 * this is a no-op. If needed, we can use j_blk_offset - everything is
557 int journal_bmap(journal_t *journal, unsigned long blocknr,
563 if (journal->j_inode) {
564 ret = bmap(journal->j_inode, blocknr);
568 char b[BDEVNAME_SIZE];
570 printk(KERN_ALERT "%s: journal block not found "
571 "at offset %lu on %s\n",
574 bdevname(journal->j_dev, b));
576 __journal_abort_soft(journal, err);
579 *retp = blocknr; /* +journal->j_blk_offset */
585 * We play buffer_head aliasing tricks to write data/metadata blocks to
586 * the journal without copying their contents, but for journal
587 * descriptor blocks we do need to generate bona fide buffers.
589 * After the caller of journal_get_descriptor_buffer() has finished modifying
590 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
591 * But we don't bother doing that, so there will be coherency problems with
592 * mmaps of blockdevs which hold live JBD-controlled filesystems.
594 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
596 struct buffer_head *bh;
597 unsigned long blocknr;
600 err = journal_next_log_block(journal, &blocknr);
605 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
607 memset(bh->b_data, 0, journal->j_blocksize);
608 set_buffer_uptodate(bh);
610 BUFFER_TRACE(bh, "return this buffer");
611 return journal_add_journal_head(bh);
615 * Management for journal control blocks: functions to create and
616 * destroy journal_t structures, and to initialise and read existing
617 * journal blocks from disk. */
619 /* First: create and setup a journal_t object in memory. We initialise
620 * very few fields yet: that has to wait until we have created the
621 * journal structures from from scratch, or loaded them from disk. */
623 static journal_t * journal_init_common (void)
628 journal = jbd_kmalloc(sizeof(*journal), GFP_KERNEL);
631 memset(journal, 0, sizeof(*journal));
633 init_waitqueue_head(&journal->j_wait_transaction_locked);
634 init_waitqueue_head(&journal->j_wait_logspace);
635 init_waitqueue_head(&journal->j_wait_done_commit);
636 init_waitqueue_head(&journal->j_wait_checkpoint);
637 init_waitqueue_head(&journal->j_wait_commit);
638 init_waitqueue_head(&journal->j_wait_updates);
639 init_MUTEX(&journal->j_barrier);
640 init_MUTEX(&journal->j_checkpoint_sem);
641 spin_lock_init(&journal->j_revoke_lock);
642 spin_lock_init(&journal->j_list_lock);
643 spin_lock_init(&journal->j_state_lock);
645 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
647 /* The journal is marked for error until we succeed with recovery! */
648 journal->j_flags = JFS_ABORT;
650 /* Set up a default-sized revoke table for the new mount. */
651 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
661 /* journal_init_dev and journal_init_inode:
663 * Create a journal structure assigned some fixed set of disk blocks to
664 * the journal. We don't actually touch those disk blocks yet, but we
665 * need to set up all of the mapping information to tell the journaling
666 * system where the journal blocks are.
671 * journal_t * journal_init_dev() - creates an initialises a journal structure
672 * @bdev: Block device on which to create the journal
673 * @fs_dev: Device which hold journalled filesystem for this journal.
674 * @start: Block nr Start of journal.
675 * @len: Lenght of the journal in blocks.
676 * @blocksize: blocksize of journalling device
677 * @returns: a newly created journal_t *
679 * journal_init_dev creates a journal which maps a fixed contiguous
680 * range of blocks on an arbitrary block device.
683 journal_t * journal_init_dev(struct block_device *bdev,
684 struct block_device *fs_dev,
685 int start, int len, int blocksize)
687 journal_t *journal = journal_init_common();
688 struct buffer_head *bh;
693 journal->j_dev = bdev;
694 journal->j_fs_dev = fs_dev;
695 journal->j_blk_offset = start;
696 journal->j_maxlen = len;
697 journal->j_blocksize = blocksize;
699 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
700 J_ASSERT(bh != NULL);
701 journal->j_sb_buffer = bh;
702 journal->j_superblock = (journal_superblock_t *)bh->b_data;
708 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
709 * @inode: An inode to create the journal in
711 * journal_init_inode creates a journal which maps an on-disk inode as
712 * the journal. The inode must exist already, must support bmap() and
713 * must have all data blocks preallocated.
715 journal_t * journal_init_inode (struct inode *inode)
717 struct buffer_head *bh;
718 journal_t *journal = journal_init_common();
720 unsigned long blocknr;
725 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
726 journal->j_inode = inode;
728 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
729 journal, inode->i_sb->s_id, inode->i_ino,
730 (long long) inode->i_size,
731 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
733 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
734 journal->j_blocksize = inode->i_sb->s_blocksize;
736 err = journal_bmap(journal, 0, &blocknr);
737 /* If that failed, give up */
739 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
745 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
746 J_ASSERT(bh != NULL);
747 journal->j_sb_buffer = bh;
748 journal->j_superblock = (journal_superblock_t *)bh->b_data;
754 * If the journal init or create aborts, we need to mark the journal
755 * superblock as being NULL to prevent the journal destroy from writing
756 * back a bogus superblock.
758 static void journal_fail_superblock (journal_t *journal)
760 struct buffer_head *bh = journal->j_sb_buffer;
762 journal->j_sb_buffer = NULL;
766 * Given a journal_t structure, initialise the various fields for
767 * startup of a new journaling session. We use this both when creating
768 * a journal, and after recovering an old journal to reset it for
772 static int journal_reset(journal_t *journal)
774 journal_superblock_t *sb = journal->j_superblock;
775 unsigned int first, last;
777 first = ntohl(sb->s_first);
778 last = ntohl(sb->s_maxlen);
780 journal->j_first = first;
781 journal->j_last = last;
783 journal->j_head = first;
784 journal->j_tail = first;
785 journal->j_free = last - first;
787 journal->j_tail_sequence = journal->j_transaction_sequence;
788 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
789 journal->j_commit_request = journal->j_commit_sequence;
791 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
793 /* Add the dynamic fields and write it to disk. */
794 journal_update_superblock(journal, 1);
795 journal_start_thread(journal);
800 * int journal_create() - Initialise the new journal file
801 * @journal: Journal to create. This structure must have been initialised
803 * Given a journal_t structure which tells us which disk blocks we can
804 * use, create a new journal superblock and initialise all of the
805 * journal fields from scratch.
807 int journal_create(journal_t *journal)
809 unsigned long blocknr;
810 struct buffer_head *bh;
811 journal_superblock_t *sb;
814 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
815 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
817 journal_fail_superblock(journal);
821 if (journal->j_inode == NULL) {
823 * We don't know what block to start at!
826 "%s: creation of journal on external device!\n",
831 /* Zero out the entire journal on disk. We cannot afford to
832 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
833 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
834 for (i = 0; i < journal->j_maxlen; i++) {
835 err = journal_bmap(journal, i, &blocknr);
838 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
840 memset (bh->b_data, 0, journal->j_blocksize);
841 BUFFER_TRACE(bh, "marking dirty");
842 mark_buffer_dirty(bh);
843 BUFFER_TRACE(bh, "marking uptodate");
844 set_buffer_uptodate(bh);
849 sync_blockdev(journal->j_dev);
850 jbd_debug(1, "JBD: journal cleared.\n");
852 /* OK, fill in the initial static fields in the new superblock */
853 sb = journal->j_superblock;
855 sb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
856 sb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
858 sb->s_blocksize = htonl(journal->j_blocksize);
859 sb->s_maxlen = htonl(journal->j_maxlen);
860 sb->s_first = htonl(1);
862 journal->j_transaction_sequence = 1;
864 journal->j_flags &= ~JFS_ABORT;
865 journal->j_format_version = 2;
867 return journal_reset(journal);
871 * void journal_update_superblock() - Update journal sb on disk.
872 * @journal: The journal to update.
873 * @wait: Set to '0' if you don't want to wait for IO completion.
875 * Update a journal's dynamic superblock fields and write it to disk,
876 * optionally waiting for the IO to complete.
878 void journal_update_superblock(journal_t *journal, int wait)
880 journal_superblock_t *sb = journal->j_superblock;
881 struct buffer_head *bh = journal->j_sb_buffer;
884 * As a special case, if the on-disk copy is already marked as needing
885 * no recovery (s_start == 0) and there are no outstanding transactions
886 * in the filesystem, then we can safely defer the superblock update
887 * until the next commit by setting JFS_FLUSHED. This avoids
888 * attempting a write to a potential-readonly device.
890 if (sb->s_start == 0 && journal->j_tail_sequence ==
891 journal->j_transaction_sequence) {
892 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
893 "(start %ld, seq %d, errno %d)\n",
894 journal->j_tail, journal->j_tail_sequence,
899 spin_lock(&journal->j_state_lock);
900 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
901 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
903 sb->s_sequence = htonl(journal->j_tail_sequence);
904 sb->s_start = htonl(journal->j_tail);
905 sb->s_errno = htonl(journal->j_errno);
906 spin_unlock(&journal->j_state_lock);
908 BUFFER_TRACE(bh, "marking dirty");
909 mark_buffer_dirty(bh);
911 sync_dirty_buffer(bh);
913 ll_rw_block(WRITE, 1, &bh);
916 /* If we have just flushed the log (by marking s_start==0), then
917 * any future commit will have to be careful to update the
918 * superblock again to re-record the true start of the log. */
920 spin_lock(&journal->j_state_lock);
922 journal->j_flags &= ~JFS_FLUSHED;
924 journal->j_flags |= JFS_FLUSHED;
925 spin_unlock(&journal->j_state_lock);
929 * Read the superblock for a given journal, performing initial
930 * validation of the format.
933 static int journal_get_superblock(journal_t *journal)
935 struct buffer_head *bh;
936 journal_superblock_t *sb;
939 bh = journal->j_sb_buffer;
941 J_ASSERT(bh != NULL);
942 if (!buffer_uptodate(bh)) {
943 ll_rw_block(READ, 1, &bh);
945 if (!buffer_uptodate(bh)) {
947 "JBD: IO error reading journal superblock\n");
952 sb = journal->j_superblock;
956 if (sb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
957 sb->s_blocksize != htonl(journal->j_blocksize)) {
958 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
962 switch(ntohl(sb->s_header.h_blocktype)) {
963 case JFS_SUPERBLOCK_V1:
964 journal->j_format_version = 1;
966 case JFS_SUPERBLOCK_V2:
967 journal->j_format_version = 2;
970 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
974 if (ntohl(sb->s_maxlen) < journal->j_maxlen)
975 journal->j_maxlen = ntohl(sb->s_maxlen);
976 else if (ntohl(sb->s_maxlen) > journal->j_maxlen) {
977 printk (KERN_WARNING "JBD: journal file too short\n");
984 journal_fail_superblock(journal);
989 * Load the on-disk journal superblock and read the key fields into the
993 static int load_superblock(journal_t *journal)
996 journal_superblock_t *sb;
998 err = journal_get_superblock(journal);
1002 sb = journal->j_superblock;
1004 journal->j_tail_sequence = ntohl(sb->s_sequence);
1005 journal->j_tail = ntohl(sb->s_start);
1006 journal->j_first = ntohl(sb->s_first);
1007 journal->j_last = ntohl(sb->s_maxlen);
1008 journal->j_errno = ntohl(sb->s_errno);
1015 * int journal_load() - Read journal from disk.
1016 * @journal: Journal to act on.
1018 * Given a journal_t structure which tells us which disk blocks contain
1019 * a journal, read the journal from disk to initialise the in-memory
1022 int journal_load(journal_t *journal)
1026 err = load_superblock(journal);
1030 /* If this is a V2 superblock, then we have to check the
1031 * features flags on it. */
1033 if (journal->j_format_version >= 2) {
1034 journal_superblock_t *sb = journal->j_superblock;
1036 if ((sb->s_feature_ro_compat &
1037 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1038 (sb->s_feature_incompat &
1039 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1040 printk (KERN_WARNING
1041 "JBD: Unrecognised features on journal\n");
1046 /* Let the recovery code check whether it needs to recover any
1047 * data from the journal. */
1048 if (journal_recover(journal))
1049 goto recovery_error;
1051 /* OK, we've finished with the dynamic journal bits:
1052 * reinitialise the dynamic contents of the superblock in memory
1053 * and reset them on disk. */
1054 if (journal_reset(journal))
1055 goto recovery_error;
1057 journal->j_flags &= ~JFS_ABORT;
1058 journal->j_flags |= JFS_LOADED;
1062 printk (KERN_WARNING "JBD: recovery failed\n");
1067 * void journal_destroy() - Release a journal_t structure.
1068 * @journal: Journal to act on.
1070 * Release a journal_t structure once it is no longer in use by the
1073 void journal_destroy(journal_t *journal)
1075 /* Wait for the commit thread to wake up and die. */
1076 journal_kill_thread(journal);
1078 /* Force a final log commit */
1079 if (journal->j_running_transaction)
1080 journal_commit_transaction(journal);
1082 /* Force any old transactions to disk */
1084 /* Totally anal locking here... */
1085 spin_lock(&journal->j_list_lock);
1086 while (journal->j_checkpoint_transactions != NULL) {
1087 spin_unlock(&journal->j_list_lock);
1088 log_do_checkpoint(journal);
1089 spin_lock(&journal->j_list_lock);
1092 J_ASSERT(journal->j_running_transaction == NULL);
1093 J_ASSERT(journal->j_committing_transaction == NULL);
1094 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1095 spin_unlock(&journal->j_list_lock);
1097 /* We can now mark the journal as empty. */
1098 journal->j_tail = 0;
1099 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1100 if (journal->j_sb_buffer) {
1101 journal_update_superblock(journal, 1);
1102 brelse(journal->j_sb_buffer);
1105 if (journal->j_inode)
1106 iput(journal->j_inode);
1107 if (journal->j_revoke)
1108 journal_destroy_revoke(journal);
1114 *int journal_check_used_features () - Check if features specified are used.
1116 * Check whether the journal uses all of a given set of
1117 * features. Return true (non-zero) if it does.
1120 int journal_check_used_features (journal_t *journal, unsigned long compat,
1121 unsigned long ro, unsigned long incompat)
1123 journal_superblock_t *sb;
1125 if (!compat && !ro && !incompat)
1127 if (journal->j_format_version == 1)
1130 sb = journal->j_superblock;
1132 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1133 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1134 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1141 * int journal_check_available_features() - Check feature set in journalling layer
1143 * Check whether the journaling code supports the use of
1144 * all of a given set of features on this journal. Return true
1145 * (non-zero) if it can. */
1147 int journal_check_available_features (journal_t *journal, unsigned long compat,
1148 unsigned long ro, unsigned long incompat)
1150 journal_superblock_t *sb;
1152 if (!compat && !ro && !incompat)
1155 sb = journal->j_superblock;
1157 /* We can support any known requested features iff the
1158 * superblock is in version 2. Otherwise we fail to support any
1159 * extended sb features. */
1161 if (journal->j_format_version != 2)
1164 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1165 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1166 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1173 * int journal_set_features () - Mark a given journal feature in the superblock
1175 * Mark a given journal feature as present on the
1176 * superblock. Returns true if the requested features could be set.
1180 int journal_set_features (journal_t *journal, unsigned long compat,
1181 unsigned long ro, unsigned long incompat)
1183 journal_superblock_t *sb;
1185 if (journal_check_used_features(journal, compat, ro, incompat))
1188 if (!journal_check_available_features(journal, compat, ro, incompat))
1191 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1192 compat, ro, incompat);
1194 sb = journal->j_superblock;
1196 sb->s_feature_compat |= cpu_to_be32(compat);
1197 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1198 sb->s_feature_incompat |= cpu_to_be32(incompat);
1205 * int journal_update_format () - Update on-disk journal structure.
1207 * Given an initialised but unloaded journal struct, poke about in the
1208 * on-disk structure to update it to the most recent supported version.
1210 int journal_update_format (journal_t *journal)
1212 journal_superblock_t *sb;
1215 err = journal_get_superblock(journal);
1219 sb = journal->j_superblock;
1221 switch (ntohl(sb->s_header.h_blocktype)) {
1222 case JFS_SUPERBLOCK_V2:
1224 case JFS_SUPERBLOCK_V1:
1225 return journal_convert_superblock_v1(journal, sb);
1232 static int journal_convert_superblock_v1(journal_t *journal,
1233 journal_superblock_t *sb)
1235 int offset, blocksize;
1236 struct buffer_head *bh;
1239 "JBD: Converting superblock from version 1 to 2.\n");
1241 /* Pre-initialise new fields to zero */
1242 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1243 blocksize = ntohl(sb->s_blocksize);
1244 memset(&sb->s_feature_compat, 0, blocksize-offset);
1246 sb->s_nr_users = cpu_to_be32(1);
1247 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1248 journal->j_format_version = 2;
1250 bh = journal->j_sb_buffer;
1251 BUFFER_TRACE(bh, "marking dirty");
1252 mark_buffer_dirty(bh);
1253 sync_dirty_buffer(bh);
1259 * int journal_flush () - Flush journal
1260 * @journal: Journal to act on.
1262 * Flush all data for a given journal to disk and empty the journal.
1263 * Filesystems can use this when remounting readonly to ensure that
1264 * recovery does not need to happen on remount.
1267 int journal_flush(journal_t *journal)
1270 transaction_t *transaction = NULL;
1271 unsigned long old_tail;
1273 spin_lock(&journal->j_state_lock);
1275 /* Force everything buffered to the log... */
1276 if (journal->j_running_transaction) {
1277 transaction = journal->j_running_transaction;
1278 __log_start_commit(journal, transaction->t_tid);
1279 } else if (journal->j_committing_transaction)
1280 transaction = journal->j_committing_transaction;
1282 /* Wait for the log commit to complete... */
1284 tid_t tid = transaction->t_tid;
1286 spin_unlock(&journal->j_state_lock);
1287 log_wait_commit(journal, tid);
1289 spin_unlock(&journal->j_state_lock);
1292 /* ...and flush everything in the log out to disk. */
1293 spin_lock(&journal->j_list_lock);
1294 while (!err && journal->j_checkpoint_transactions != NULL) {
1295 spin_unlock(&journal->j_list_lock);
1296 err = log_do_checkpoint(journal);
1297 spin_lock(&journal->j_list_lock);
1299 spin_unlock(&journal->j_list_lock);
1300 cleanup_journal_tail(journal);
1302 /* Finally, mark the journal as really needing no recovery.
1303 * This sets s_start==0 in the underlying superblock, which is
1304 * the magic code for a fully-recovered superblock. Any future
1305 * commits of data to the journal will restore the current
1307 spin_lock(&journal->j_state_lock);
1308 old_tail = journal->j_tail;
1309 journal->j_tail = 0;
1310 spin_unlock(&journal->j_state_lock);
1311 journal_update_superblock(journal, 1);
1312 spin_lock(&journal->j_state_lock);
1313 journal->j_tail = old_tail;
1315 J_ASSERT(!journal->j_running_transaction);
1316 J_ASSERT(!journal->j_committing_transaction);
1317 J_ASSERT(!journal->j_checkpoint_transactions);
1318 J_ASSERT(journal->j_head == journal->j_tail);
1319 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1320 spin_unlock(&journal->j_state_lock);
1325 * int journal_wipe() - Wipe journal contents
1326 * @journal: Journal to act on.
1327 * @write: flag (see below)
1329 * Wipe out all of the contents of a journal, safely. This will produce
1330 * a warning if the journal contains any valid recovery information.
1331 * Must be called between journal_init_*() and journal_load().
1333 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1334 * we merely suppress recovery.
1337 int journal_wipe(journal_t *journal, int write)
1339 journal_superblock_t *sb;
1342 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1344 err = load_superblock(journal);
1348 sb = journal->j_superblock;
1350 if (!journal->j_tail)
1353 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1354 write ? "Clearing" : "Ignoring");
1356 err = journal_skip_recovery(journal);
1358 journal_update_superblock(journal, 1);
1365 * journal_dev_name: format a character string to describe on what
1366 * device this journal is present.
1369 const char *journal_dev_name(journal_t *journal, char *buffer)
1371 struct block_device *bdev;
1373 if (journal->j_inode)
1374 bdev = journal->j_inode->i_sb->s_bdev;
1376 bdev = journal->j_dev;
1378 return bdevname(bdev, buffer);
1382 * Journal abort has very specific semantics, which we describe
1383 * for journal abort.
1385 * Two internal function, which provide abort to te jbd layer
1390 * Quick version for internal journal use (doesn't lock the journal).
1391 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1392 * and don't attempt to make any other journal updates.
1394 void __journal_abort_hard(journal_t *journal)
1396 transaction_t *transaction;
1397 char b[BDEVNAME_SIZE];
1399 if (journal->j_flags & JFS_ABORT)
1402 printk(KERN_ERR "Aborting journal on device %s.\n",
1403 journal_dev_name(journal, b));
1405 spin_lock(&journal->j_state_lock);
1406 journal->j_flags |= JFS_ABORT;
1407 transaction = journal->j_running_transaction;
1409 __log_start_commit(journal, transaction->t_tid);
1410 spin_unlock(&journal->j_state_lock);
1413 /* Soft abort: record the abort error status in the journal superblock,
1414 * but don't do any other IO. */
1415 void __journal_abort_soft (journal_t *journal, int errno)
1417 if (journal->j_flags & JFS_ABORT)
1420 if (!journal->j_errno)
1421 journal->j_errno = errno;
1423 __journal_abort_hard(journal);
1426 journal_update_superblock(journal, 1);
1430 * void journal_abort () - Shutdown the journal immediately.
1431 * @journal: the journal to shutdown.
1432 * @errno: an error number to record in the journal indicating
1433 * the reason for the shutdown.
1435 * Perform a complete, immediate shutdown of the ENTIRE
1436 * journal (not of a single transaction). This operation cannot be
1437 * undone without closing and reopening the journal.
1439 * The journal_abort function is intended to support higher level error
1440 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1443 * Journal abort has very specific semantics. Any existing dirty,
1444 * unjournaled buffers in the main filesystem will still be written to
1445 * disk by bdflush, but the journaling mechanism will be suspended
1446 * immediately and no further transaction commits will be honoured.
1448 * Any dirty, journaled buffers will be written back to disk without
1449 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1450 * filesystem, but we _do_ attempt to leave as much data as possible
1451 * behind for fsck to use for cleanup.
1453 * Any attempt to get a new transaction handle on a journal which is in
1454 * ABORT state will just result in an -EROFS error return. A
1455 * journal_stop on an existing handle will return -EIO if we have
1456 * entered abort state during the update.
1458 * Recursive transactions are not disturbed by journal abort until the
1459 * final journal_stop, which will receive the -EIO error.
1461 * Finally, the journal_abort call allows the caller to supply an errno
1462 * which will be recorded (if possible) in the journal superblock. This
1463 * allows a client to record failure conditions in the middle of a
1464 * transaction without having to complete the transaction to record the
1465 * failure to disk. ext3_error, for example, now uses this
1468 * Errors which originate from within the journaling layer will NOT
1469 * supply an errno; a null errno implies that absolutely no further
1470 * writes are done to the journal (unless there are any already in
1475 void journal_abort(journal_t *journal, int errno)
1477 __journal_abort_soft(journal, errno);
1481 * int journal_errno () - returns the journal's error state.
1482 * @journal: journal to examine.
1484 * This is the errno numbet set with journal_abort(), the last
1485 * time the journal was mounted - if the journal was stopped
1486 * without calling abort this will be 0.
1488 * If the journal has been aborted on this mount time -EROFS will
1491 int journal_errno(journal_t *journal)
1495 spin_lock(&journal->j_state_lock);
1496 if (journal->j_flags & JFS_ABORT)
1499 err = journal->j_errno;
1500 spin_unlock(&journal->j_state_lock);
1505 * int journal_clear_err () - clears the journal's error state
1507 * An error must be cleared or Acked to take a FS out of readonly
1510 int journal_clear_err(journal_t *journal)
1514 spin_lock(&journal->j_state_lock);
1515 if (journal->j_flags & JFS_ABORT)
1518 journal->j_errno = 0;
1519 spin_unlock(&journal->j_state_lock);
1524 * void journal_ack_err() - Ack journal err.
1526 * An error must be cleared or Acked to take a FS out of readonly
1529 void journal_ack_err(journal_t *journal)
1531 spin_lock(&journal->j_state_lock);
1532 if (journal->j_errno)
1533 journal->j_flags |= JFS_ACK_ERR;
1534 spin_unlock(&journal->j_state_lock);
1537 int journal_blocks_per_page(struct inode *inode)
1539 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1543 * Simple support for retying memory allocations. Introduced to help to
1544 * debug different VM deadlock avoidance strategies.
1547 * Simple support for retying memory allocations. Introduced to help to
1548 * debug different VM deadlock avoidance strategies.
1550 void * __jbd_kmalloc (const char *where, size_t size, int flags, int retry)
1552 return kmalloc(size, flags | (retry ? __GFP_NOFAIL : 0));
1556 * Journal_head storage management
1558 static kmem_cache_t *journal_head_cache;
1559 #ifdef CONFIG_JBD_DEBUG
1560 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1563 static int journal_init_journal_head_cache(void)
1567 J_ASSERT(journal_head_cache == 0);
1568 journal_head_cache = kmem_cache_create("journal_head",
1569 sizeof(struct journal_head),
1575 if (journal_head_cache == 0) {
1577 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1582 static void journal_destroy_journal_head_cache(void)
1584 J_ASSERT(journal_head_cache != NULL);
1585 kmem_cache_destroy(journal_head_cache);
1586 journal_head_cache = 0;
1590 * journal_head splicing and dicing
1592 static struct journal_head *journal_alloc_journal_head(void)
1594 struct journal_head *ret;
1595 static unsigned long last_warning;
1597 #ifdef CONFIG_JBD_DEBUG
1598 atomic_inc(&nr_journal_heads);
1600 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1602 jbd_debug(1, "out of memory for journal_head\n");
1603 if (time_after(jiffies, last_warning + 5*HZ)) {
1604 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1606 last_warning = jiffies;
1610 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1616 static void journal_free_journal_head(struct journal_head *jh)
1618 #ifdef CONFIG_JBD_DEBUG
1619 atomic_dec(&nr_journal_heads);
1620 memset(jh, 0x5b, sizeof(*jh));
1622 kmem_cache_free(journal_head_cache, jh);
1626 * A journal_head is attached to a buffer_head whenever JBD has an
1627 * interest in the buffer.
1629 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1630 * is set. This bit is tested in core kernel code where we need to take
1631 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1634 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1636 * When a buffer has its BH_JBD bit set it is immune from being released by
1637 * core kernel code, mainly via ->b_count.
1639 * A journal_head may be detached from its buffer_head when the journal_head's
1640 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1641 * Various places in JBD call journal_remove_journal_head() to indicate that the
1642 * journal_head can be dropped if needed.
1644 * Various places in the kernel want to attach a journal_head to a buffer_head
1645 * _before_ attaching the journal_head to a transaction. To protect the
1646 * journal_head in this situation, journal_add_journal_head elevates the
1647 * journal_head's b_jcount refcount by one. The caller must call
1648 * journal_put_journal_head() to undo this.
1650 * So the typical usage would be:
1652 * (Attach a journal_head if needed. Increments b_jcount)
1653 * struct journal_head *jh = journal_add_journal_head(bh);
1655 * jh->b_transaction = xxx;
1656 * journal_put_journal_head(jh);
1658 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1659 * because it has a non-zero b_transaction.
1663 * Give a buffer_head a journal_head.
1665 * Doesn't need the journal lock.
1668 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1670 struct journal_head *jh;
1671 struct journal_head *new_jh = NULL;
1674 if (!buffer_jbd(bh)) {
1675 new_jh = journal_alloc_journal_head();
1676 memset(new_jh, 0, sizeof(*new_jh));
1679 jbd_lock_bh_journal_head(bh);
1680 if (buffer_jbd(bh)) {
1684 (atomic_read(&bh->b_count) > 0) ||
1685 (bh->b_page && bh->b_page->mapping));
1688 jbd_unlock_bh_journal_head(bh);
1693 new_jh = NULL; /* We consumed it */
1698 BUFFER_TRACE(bh, "added journal_head");
1701 jbd_unlock_bh_journal_head(bh);
1703 journal_free_journal_head(new_jh);
1704 return bh->b_private;
1708 * Grab a ref against this buffer_head's journal_head. If it ended up not
1709 * having a journal_head, return NULL
1711 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1713 struct journal_head *jh = NULL;
1715 jbd_lock_bh_journal_head(bh);
1716 if (buffer_jbd(bh)) {
1720 jbd_unlock_bh_journal_head(bh);
1724 static void __journal_remove_journal_head(struct buffer_head *bh)
1726 struct journal_head *jh = bh2jh(bh);
1728 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1731 if (jh->b_jcount == 0) {
1732 if (jh->b_transaction == NULL &&
1733 jh->b_next_transaction == NULL &&
1734 jh->b_cp_transaction == NULL) {
1735 J_ASSERT_BH(bh, buffer_jbd(bh));
1736 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1737 BUFFER_TRACE(bh, "remove journal_head");
1738 if (jh->b_frozen_data) {
1739 printk(KERN_WARNING "%s: freeing "
1742 kfree(jh->b_frozen_data);
1744 if (jh->b_committed_data) {
1745 printk(KERN_WARNING "%s: freeing "
1746 "b_committed_data\n",
1748 kfree(jh->b_committed_data);
1750 bh->b_private = NULL;
1751 jh->b_bh = NULL; /* debug, really */
1752 clear_buffer_jbd(bh);
1754 journal_free_journal_head(jh);
1756 BUFFER_TRACE(bh, "journal_head was locked");
1762 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1763 * and has a zero b_jcount then remove and release its journal_head. If we did
1764 * see that the buffer is not used by any transaction we also "logically"
1765 * decrement ->b_count.
1767 * We in fact take an additional increment on ->b_count as a convenience,
1768 * because the caller usually wants to do additional things with the bh
1769 * after calling here.
1770 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1771 * time. Once the caller has run __brelse(), the buffer is eligible for
1772 * reaping by try_to_free_buffers().
1774 void journal_remove_journal_head(struct buffer_head *bh)
1776 jbd_lock_bh_journal_head(bh);
1777 __journal_remove_journal_head(bh);
1778 jbd_unlock_bh_journal_head(bh);
1782 * Drop a reference on the passed journal_head. If it fell to zero then try to
1783 * release the journal_head from the buffer_head.
1785 void journal_put_journal_head(struct journal_head *jh)
1787 struct buffer_head *bh = jh2bh(jh);
1789 jbd_lock_bh_journal_head(bh);
1790 J_ASSERT_JH(jh, jh->b_jcount > 0);
1792 if (!jh->b_jcount && !jh->b_transaction) {
1793 __journal_remove_journal_head(bh);
1796 jbd_unlock_bh_journal_head(bh);
1802 #if defined(CONFIG_JBD_DEBUG)
1803 int journal_enable_debug;
1804 EXPORT_SYMBOL(journal_enable_debug);
1807 #if defined(CONFIG_JBD_DEBUG) && defined(CONFIG_PROC_FS)
1809 static struct proc_dir_entry *proc_jbd_debug;
1811 int read_jbd_debug(char *page, char **start, off_t off,
1812 int count, int *eof, void *data)
1816 ret = sprintf(page + off, "%d\n", journal_enable_debug);
1821 int write_jbd_debug(struct file *file, const char __user *buffer,
1822 unsigned long count, void *data)
1826 if (count > ARRAY_SIZE(buf) - 1)
1827 count = ARRAY_SIZE(buf) - 1;
1828 if (copy_from_user(buf, buffer, count))
1830 buf[ARRAY_SIZE(buf) - 1] = '\0';
1831 journal_enable_debug = simple_strtoul(buf, NULL, 10);
1835 #define JBD_PROC_NAME "sys/fs/jbd-debug"
1837 static void __init create_jbd_proc_entry(void)
1839 proc_jbd_debug = create_proc_entry(JBD_PROC_NAME, 0644, NULL);
1840 if (proc_jbd_debug) {
1841 /* Why is this so hard? */
1842 proc_jbd_debug->read_proc = read_jbd_debug;
1843 proc_jbd_debug->write_proc = write_jbd_debug;
1847 static void __exit remove_jbd_proc_entry(void)
1850 remove_proc_entry(JBD_PROC_NAME, NULL);
1855 #define create_jbd_proc_entry() do {} while (0)
1856 #define remove_jbd_proc_entry() do {} while (0)
1860 kmem_cache_t *jbd_handle_cache;
1862 static int __init journal_init_handle_cache(void)
1864 jbd_handle_cache = kmem_cache_create("journal_handle",
1870 if (jbd_handle_cache == NULL) {
1871 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1877 static void journal_destroy_handle_cache(void)
1879 if (jbd_handle_cache)
1880 kmem_cache_destroy(jbd_handle_cache);
1884 * Module startup and shutdown
1887 static int __init journal_init_caches(void)
1891 ret = journal_init_revoke_caches();
1893 ret = journal_init_journal_head_cache();
1895 ret = journal_init_handle_cache();
1899 static void journal_destroy_caches(void)
1901 journal_destroy_revoke_caches();
1902 journal_destroy_journal_head_cache();
1903 journal_destroy_handle_cache();
1906 static int __init journal_init(void)
1910 ret = journal_init_caches();
1912 journal_destroy_caches();
1913 create_jbd_proc_entry();
1917 static void __exit journal_exit(void)
1919 #ifdef CONFIG_JBD_DEBUG
1920 int n = atomic_read(&nr_journal_heads);
1922 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
1924 remove_jbd_proc_entry();
1925 journal_destroy_caches();
1928 MODULE_LICENSE("GPL");
1929 module_init(journal_init);
1930 module_exit(journal_exit);