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>
38 #include <linux/proc_fs.h>
40 EXPORT_SYMBOL(journal_start);
41 EXPORT_SYMBOL(journal_restart);
42 EXPORT_SYMBOL(journal_extend);
43 EXPORT_SYMBOL(journal_stop);
44 EXPORT_SYMBOL(journal_lock_updates);
45 EXPORT_SYMBOL(journal_unlock_updates);
46 EXPORT_SYMBOL(journal_get_write_access);
47 EXPORT_SYMBOL(journal_get_create_access);
48 EXPORT_SYMBOL(journal_get_undo_access);
49 EXPORT_SYMBOL(journal_dirty_data);
50 EXPORT_SYMBOL(journal_dirty_metadata);
51 EXPORT_SYMBOL(journal_release_buffer);
52 EXPORT_SYMBOL(journal_forget);
54 EXPORT_SYMBOL(journal_sync_buffer);
56 EXPORT_SYMBOL(journal_flush);
57 EXPORT_SYMBOL(journal_revoke);
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_force_commit_nested);
77 EXPORT_SYMBOL(journal_wipe);
78 EXPORT_SYMBOL(journal_blocks_per_page);
79 EXPORT_SYMBOL(journal_invalidatepage);
80 EXPORT_SYMBOL(journal_try_to_free_buffers);
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 if (journal->j_flags & JFS_UNMOUNT)
157 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
158 journal->j_commit_sequence, journal->j_commit_request);
160 if (journal->j_commit_sequence != journal->j_commit_request) {
161 jbd_debug(1, "OK, requests differ\n");
162 spin_unlock(&journal->j_state_lock);
163 del_timer_sync(journal->j_commit_timer);
164 journal_commit_transaction(journal);
165 spin_lock(&journal->j_state_lock);
169 wake_up(&journal->j_wait_done_commit);
170 if (freezing(current)) {
172 * The simpler the better. Flushing journal isn't a
173 * good idea, because that depends on threads that may
174 * be already stopped.
176 jbd_debug(1, "Now suspending kjournald\n");
177 spin_unlock(&journal->j_state_lock);
179 spin_lock(&journal->j_state_lock);
182 * We assume on resume that commits are already there,
186 int should_sleep = 1;
188 prepare_to_wait(&journal->j_wait_commit, &wait,
190 if (journal->j_commit_sequence != journal->j_commit_request)
192 transaction = journal->j_running_transaction;
193 if (transaction && time_after_eq(jiffies,
194 transaction->t_expires))
197 spin_unlock(&journal->j_state_lock);
199 spin_lock(&journal->j_state_lock);
201 finish_wait(&journal->j_wait_commit, &wait);
204 jbd_debug(1, "kjournald wakes\n");
207 * Were we woken up by a commit wakeup event?
209 transaction = journal->j_running_transaction;
210 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
211 journal->j_commit_request = transaction->t_tid;
212 jbd_debug(1, "woke because of timeout\n");
217 spin_unlock(&journal->j_state_lock);
218 del_timer_sync(journal->j_commit_timer);
219 journal->j_task = NULL;
220 wake_up(&journal->j_wait_done_commit);
221 jbd_debug(1, "Journal thread exiting.\n");
225 static void journal_start_thread(journal_t *journal)
227 kernel_thread(kjournald, journal, CLONE_VM|CLONE_FS|CLONE_FILES);
228 wait_event(journal->j_wait_done_commit, journal->j_task != 0);
231 static void journal_kill_thread(journal_t *journal)
233 spin_lock(&journal->j_state_lock);
234 journal->j_flags |= JFS_UNMOUNT;
236 while (journal->j_task) {
237 wake_up(&journal->j_wait_commit);
238 spin_unlock(&journal->j_state_lock);
239 wait_event(journal->j_wait_done_commit, journal->j_task == 0);
240 spin_lock(&journal->j_state_lock);
242 spin_unlock(&journal->j_state_lock);
246 * journal_write_metadata_buffer: write a metadata buffer to the journal.
248 * Writes a metadata buffer to a given disk block. The actual IO is not
249 * performed but a new buffer_head is constructed which labels the data
250 * to be written with the correct destination disk block.
252 * Any magic-number escaping which needs to be done will cause a
253 * copy-out here. If the buffer happens to start with the
254 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
255 * magic number is only written to the log for descripter blocks. In
256 * this case, we copy the data and replace the first word with 0, and we
257 * return a result code which indicates that this buffer needs to be
258 * marked as an escaped buffer in the corresponding log descriptor
259 * block. The missing word can then be restored when the block is read
262 * If the source buffer has already been modified by a new transaction
263 * since we took the last commit snapshot, we use the frozen copy of
264 * that data for IO. If we end up using the existing buffer_head's data
265 * for the write, then we *have* to lock the buffer to prevent anyone
266 * else from using and possibly modifying it while the IO is in
269 * The function returns a pointer to the buffer_heads to be used for IO.
271 * We assume that the journal has already been locked in this function.
278 * Bit 0 set == escape performed on the data
279 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
282 int journal_write_metadata_buffer(transaction_t *transaction,
283 struct journal_head *jh_in,
284 struct journal_head **jh_out,
287 int need_copy_out = 0;
288 int done_copy_out = 0;
291 struct buffer_head *new_bh;
292 struct journal_head *new_jh;
293 struct page *new_page;
294 unsigned int new_offset;
295 struct buffer_head *bh_in = jh2bh(jh_in);
298 * The buffer really shouldn't be locked: only the current committing
299 * transaction is allowed to write it, so nobody else is allowed
302 * akpm: except if we're journalling data, and write() output is
303 * also part of a shared mapping, and another thread has
304 * decided to launch a writepage() against this buffer.
306 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
308 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
311 * If a new transaction has already done a buffer copy-out, then
312 * we use that version of the data for the commit.
314 jbd_lock_bh_state(bh_in);
316 if (jh_in->b_frozen_data) {
318 new_page = virt_to_page(jh_in->b_frozen_data);
319 new_offset = offset_in_page(jh_in->b_frozen_data);
321 new_page = jh2bh(jh_in)->b_page;
322 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
325 mapped_data = kmap_atomic(new_page, KM_USER0);
329 if (*((__be32 *)(mapped_data + new_offset)) ==
330 cpu_to_be32(JFS_MAGIC_NUMBER)) {
334 kunmap_atomic(mapped_data, KM_USER0);
337 * Do we need to do a data copy?
339 if (need_copy_out && !done_copy_out) {
342 jbd_unlock_bh_state(bh_in);
343 tmp = jbd_rep_kmalloc(bh_in->b_size, GFP_NOFS);
344 jbd_lock_bh_state(bh_in);
345 if (jh_in->b_frozen_data) {
350 jh_in->b_frozen_data = tmp;
351 mapped_data = kmap_atomic(new_page, KM_USER0);
352 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
353 kunmap_atomic(mapped_data, KM_USER0);
355 new_page = virt_to_page(tmp);
356 new_offset = offset_in_page(tmp);
361 * Did we need to do an escaping? Now we've done all the
362 * copying, we can finally do so.
365 mapped_data = kmap_atomic(new_page, KM_USER0);
366 *((unsigned int *)(mapped_data + new_offset)) = 0;
367 kunmap_atomic(mapped_data, KM_USER0);
370 /* keep subsequent assertions sane */
372 init_buffer(new_bh, NULL, NULL);
373 atomic_set(&new_bh->b_count, 1);
374 jbd_unlock_bh_state(bh_in);
376 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
378 set_bh_page(new_bh, new_page, new_offset);
379 new_jh->b_transaction = NULL;
380 new_bh->b_size = jh2bh(jh_in)->b_size;
381 new_bh->b_bdev = transaction->t_journal->j_dev;
382 new_bh->b_blocknr = blocknr;
383 set_buffer_mapped(new_bh);
384 set_buffer_dirty(new_bh);
389 * The to-be-written buffer needs to get moved to the io queue,
390 * and the original buffer whose contents we are shadowing or
391 * copying is moved to the transaction's shadow queue.
393 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
394 journal_file_buffer(jh_in, transaction, BJ_Shadow);
395 JBUFFER_TRACE(new_jh, "file as BJ_IO");
396 journal_file_buffer(new_jh, transaction, BJ_IO);
398 return do_escape | (done_copy_out << 1);
402 * Allocation code for the journal file. Manage the space left in the
403 * journal, so that we can begin checkpointing when appropriate.
407 * __log_space_left: Return the number of free blocks left in the journal.
409 * Called with the journal already locked.
411 * Called under j_state_lock
414 int __log_space_left(journal_t *journal)
416 int left = journal->j_free;
418 assert_spin_locked(&journal->j_state_lock);
421 * Be pessimistic here about the number of those free blocks which
422 * might be required for log descriptor control blocks.
425 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
427 left -= MIN_LOG_RESERVED_BLOCKS;
436 * Called under j_state_lock. Returns true if a transaction was started.
438 int __log_start_commit(journal_t *journal, tid_t target)
441 * Are we already doing a recent enough commit?
443 if (!tid_geq(journal->j_commit_request, target)) {
445 * We want a new commit: OK, mark the request and wakup the
446 * commit thread. We do _not_ do the commit ourselves.
449 journal->j_commit_request = target;
450 jbd_debug(1, "JBD: requesting commit %d/%d\n",
451 journal->j_commit_request,
452 journal->j_commit_sequence);
453 wake_up(&journal->j_wait_commit);
459 int log_start_commit(journal_t *journal, tid_t tid)
463 spin_lock(&journal->j_state_lock);
464 ret = __log_start_commit(journal, tid);
465 spin_unlock(&journal->j_state_lock);
470 * Force and wait upon a commit if the calling process is not within
471 * transaction. This is used for forcing out undo-protected data which contains
472 * bitmaps, when the fs is running out of space.
474 * We can only force the running transaction if we don't have an active handle;
475 * otherwise, we will deadlock.
477 * Returns true if a transaction was started.
479 int journal_force_commit_nested(journal_t *journal)
481 transaction_t *transaction = NULL;
484 spin_lock(&journal->j_state_lock);
485 if (journal->j_running_transaction && !current->journal_info) {
486 transaction = journal->j_running_transaction;
487 __log_start_commit(journal, transaction->t_tid);
488 } else if (journal->j_committing_transaction)
489 transaction = journal->j_committing_transaction;
492 spin_unlock(&journal->j_state_lock);
493 return 0; /* Nothing to retry */
496 tid = transaction->t_tid;
497 spin_unlock(&journal->j_state_lock);
498 log_wait_commit(journal, tid);
503 * Start a commit of the current running transaction (if any). Returns true
504 * if a transaction was started, and fills its tid in at *ptid
506 int journal_start_commit(journal_t *journal, tid_t *ptid)
510 spin_lock(&journal->j_state_lock);
511 if (journal->j_running_transaction) {
512 tid_t tid = journal->j_running_transaction->t_tid;
514 ret = __log_start_commit(journal, tid);
517 } else if (journal->j_committing_transaction && ptid) {
519 * If ext3_write_super() recently started a commit, then we
520 * have to wait for completion of that transaction
522 *ptid = journal->j_committing_transaction->t_tid;
525 spin_unlock(&journal->j_state_lock);
530 * Wait for a specified commit to complete.
531 * The caller may not hold the journal lock.
533 int log_wait_commit(journal_t *journal, tid_t tid)
537 #ifdef CONFIG_JBD_DEBUG
538 spin_lock(&journal->j_state_lock);
539 if (!tid_geq(journal->j_commit_request, tid)) {
541 "%s: error: j_commit_request=%d, tid=%d\n",
542 __FUNCTION__, journal->j_commit_request, tid);
544 spin_unlock(&journal->j_state_lock);
546 spin_lock(&journal->j_state_lock);
547 while (tid_gt(tid, journal->j_commit_sequence)) {
548 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
549 tid, journal->j_commit_sequence);
550 wake_up(&journal->j_wait_commit);
551 spin_unlock(&journal->j_state_lock);
552 wait_event(journal->j_wait_done_commit,
553 !tid_gt(tid, journal->j_commit_sequence));
554 spin_lock(&journal->j_state_lock);
556 spin_unlock(&journal->j_state_lock);
558 if (unlikely(is_journal_aborted(journal))) {
559 printk(KERN_EMERG "journal commit I/O error\n");
566 * Log buffer allocation routines:
569 int journal_next_log_block(journal_t *journal, unsigned long *retp)
571 unsigned long blocknr;
573 spin_lock(&journal->j_state_lock);
574 J_ASSERT(journal->j_free > 1);
576 blocknr = journal->j_head;
579 if (journal->j_head == journal->j_last)
580 journal->j_head = journal->j_first;
581 spin_unlock(&journal->j_state_lock);
582 return journal_bmap(journal, blocknr, retp);
586 * Conversion of logical to physical block numbers for the journal
588 * On external journals the journal blocks are identity-mapped, so
589 * this is a no-op. If needed, we can use j_blk_offset - everything is
592 int journal_bmap(journal_t *journal, unsigned long blocknr,
598 if (journal->j_inode) {
599 ret = bmap(journal->j_inode, blocknr);
603 char b[BDEVNAME_SIZE];
605 printk(KERN_ALERT "%s: journal block not found "
606 "at offset %lu on %s\n",
609 bdevname(journal->j_dev, b));
611 __journal_abort_soft(journal, err);
614 *retp = blocknr; /* +journal->j_blk_offset */
620 * We play buffer_head aliasing tricks to write data/metadata blocks to
621 * the journal without copying their contents, but for journal
622 * descriptor blocks we do need to generate bona fide buffers.
624 * After the caller of journal_get_descriptor_buffer() has finished modifying
625 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
626 * But we don't bother doing that, so there will be coherency problems with
627 * mmaps of blockdevs which hold live JBD-controlled filesystems.
629 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
631 struct buffer_head *bh;
632 unsigned long blocknr;
635 err = journal_next_log_block(journal, &blocknr);
640 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
642 memset(bh->b_data, 0, journal->j_blocksize);
643 set_buffer_uptodate(bh);
645 BUFFER_TRACE(bh, "return this buffer");
646 return journal_add_journal_head(bh);
650 * Management for journal control blocks: functions to create and
651 * destroy journal_t structures, and to initialise and read existing
652 * journal blocks from disk. */
654 /* First: create and setup a journal_t object in memory. We initialise
655 * very few fields yet: that has to wait until we have created the
656 * journal structures from from scratch, or loaded them from disk. */
658 static journal_t * journal_init_common (void)
663 journal = jbd_kmalloc(sizeof(*journal), GFP_KERNEL);
666 memset(journal, 0, sizeof(*journal));
668 init_waitqueue_head(&journal->j_wait_transaction_locked);
669 init_waitqueue_head(&journal->j_wait_logspace);
670 init_waitqueue_head(&journal->j_wait_done_commit);
671 init_waitqueue_head(&journal->j_wait_checkpoint);
672 init_waitqueue_head(&journal->j_wait_commit);
673 init_waitqueue_head(&journal->j_wait_updates);
674 init_MUTEX(&journal->j_barrier);
675 init_MUTEX(&journal->j_checkpoint_sem);
676 spin_lock_init(&journal->j_revoke_lock);
677 spin_lock_init(&journal->j_list_lock);
678 spin_lock_init(&journal->j_state_lock);
680 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
682 /* The journal is marked for error until we succeed with recovery! */
683 journal->j_flags = JFS_ABORT;
685 /* Set up a default-sized revoke table for the new mount. */
686 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
696 /* journal_init_dev and journal_init_inode:
698 * Create a journal structure assigned some fixed set of disk blocks to
699 * the journal. We don't actually touch those disk blocks yet, but we
700 * need to set up all of the mapping information to tell the journaling
701 * system where the journal blocks are.
706 * journal_t * journal_init_dev() - creates an initialises a journal structure
707 * @bdev: Block device on which to create the journal
708 * @fs_dev: Device which hold journalled filesystem for this journal.
709 * @start: Block nr Start of journal.
710 * @len: Lenght of the journal in blocks.
711 * @blocksize: blocksize of journalling device
712 * @returns: a newly created journal_t *
714 * journal_init_dev creates a journal which maps a fixed contiguous
715 * range of blocks on an arbitrary block device.
718 journal_t * journal_init_dev(struct block_device *bdev,
719 struct block_device *fs_dev,
720 int start, int len, int blocksize)
722 journal_t *journal = journal_init_common();
723 struct buffer_head *bh;
729 journal->j_dev = bdev;
730 journal->j_fs_dev = fs_dev;
731 journal->j_blk_offset = start;
732 journal->j_maxlen = len;
733 journal->j_blocksize = blocksize;
735 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
736 J_ASSERT(bh != NULL);
737 journal->j_sb_buffer = bh;
738 journal->j_superblock = (journal_superblock_t *)bh->b_data;
740 /* journal descriptor can store up to n blocks -bzzz */
741 n = journal->j_blocksize / sizeof(journal_block_tag_t);
742 journal->j_wbufsize = n;
743 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
744 if (!journal->j_wbuf) {
745 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
755 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
756 * @inode: An inode to create the journal in
758 * journal_init_inode creates a journal which maps an on-disk inode as
759 * the journal. The inode must exist already, must support bmap() and
760 * must have all data blocks preallocated.
762 journal_t * journal_init_inode (struct inode *inode)
764 struct buffer_head *bh;
765 journal_t *journal = journal_init_common();
768 unsigned long blocknr;
773 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
774 journal->j_inode = inode;
776 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
777 journal, inode->i_sb->s_id, inode->i_ino,
778 (long long) inode->i_size,
779 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
781 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
782 journal->j_blocksize = inode->i_sb->s_blocksize;
784 /* journal descriptor can store up to n blocks -bzzz */
785 n = journal->j_blocksize / sizeof(journal_block_tag_t);
786 journal->j_wbufsize = n;
787 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
788 if (!journal->j_wbuf) {
789 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
795 err = journal_bmap(journal, 0, &blocknr);
796 /* If that failed, give up */
798 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
804 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
805 J_ASSERT(bh != NULL);
806 journal->j_sb_buffer = bh;
807 journal->j_superblock = (journal_superblock_t *)bh->b_data;
813 * If the journal init or create aborts, we need to mark the journal
814 * superblock as being NULL to prevent the journal destroy from writing
815 * back a bogus superblock.
817 static void journal_fail_superblock (journal_t *journal)
819 struct buffer_head *bh = journal->j_sb_buffer;
821 journal->j_sb_buffer = NULL;
825 * Given a journal_t structure, initialise the various fields for
826 * startup of a new journaling session. We use this both when creating
827 * a journal, and after recovering an old journal to reset it for
831 static int journal_reset(journal_t *journal)
833 journal_superblock_t *sb = journal->j_superblock;
834 unsigned int first, last;
836 first = be32_to_cpu(sb->s_first);
837 last = be32_to_cpu(sb->s_maxlen);
839 journal->j_first = first;
840 journal->j_last = last;
842 journal->j_head = first;
843 journal->j_tail = first;
844 journal->j_free = last - first;
846 journal->j_tail_sequence = journal->j_transaction_sequence;
847 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
848 journal->j_commit_request = journal->j_commit_sequence;
850 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
852 /* Add the dynamic fields and write it to disk. */
853 journal_update_superblock(journal, 1);
854 journal_start_thread(journal);
859 * int journal_create() - Initialise the new journal file
860 * @journal: Journal to create. This structure must have been initialised
862 * Given a journal_t structure which tells us which disk blocks we can
863 * use, create a new journal superblock and initialise all of the
864 * journal fields from scratch.
866 int journal_create(journal_t *journal)
868 unsigned long blocknr;
869 struct buffer_head *bh;
870 journal_superblock_t *sb;
873 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
874 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
876 journal_fail_superblock(journal);
880 if (journal->j_inode == NULL) {
882 * We don't know what block to start at!
885 "%s: creation of journal on external device!\n",
890 /* Zero out the entire journal on disk. We cannot afford to
891 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
892 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
893 for (i = 0; i < journal->j_maxlen; i++) {
894 err = journal_bmap(journal, i, &blocknr);
897 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
899 memset (bh->b_data, 0, journal->j_blocksize);
900 BUFFER_TRACE(bh, "marking dirty");
901 mark_buffer_dirty(bh);
902 BUFFER_TRACE(bh, "marking uptodate");
903 set_buffer_uptodate(bh);
908 sync_blockdev(journal->j_dev);
909 jbd_debug(1, "JBD: journal cleared.\n");
911 /* OK, fill in the initial static fields in the new superblock */
912 sb = journal->j_superblock;
914 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
915 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
917 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
918 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
919 sb->s_first = cpu_to_be32(1);
921 journal->j_transaction_sequence = 1;
923 journal->j_flags &= ~JFS_ABORT;
924 journal->j_format_version = 2;
926 return journal_reset(journal);
930 * void journal_update_superblock() - Update journal sb on disk.
931 * @journal: The journal to update.
932 * @wait: Set to '0' if you don't want to wait for IO completion.
934 * Update a journal's dynamic superblock fields and write it to disk,
935 * optionally waiting for the IO to complete.
937 void journal_update_superblock(journal_t *journal, int wait)
939 journal_superblock_t *sb = journal->j_superblock;
940 struct buffer_head *bh = journal->j_sb_buffer;
943 * As a special case, if the on-disk copy is already marked as needing
944 * no recovery (s_start == 0) and there are no outstanding transactions
945 * in the filesystem, then we can safely defer the superblock update
946 * until the next commit by setting JFS_FLUSHED. This avoids
947 * attempting a write to a potential-readonly device.
949 if (sb->s_start == 0 && journal->j_tail_sequence ==
950 journal->j_transaction_sequence) {
951 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
952 "(start %ld, seq %d, errno %d)\n",
953 journal->j_tail, journal->j_tail_sequence,
958 spin_lock(&journal->j_state_lock);
959 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
960 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
962 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
963 sb->s_start = cpu_to_be32(journal->j_tail);
964 sb->s_errno = cpu_to_be32(journal->j_errno);
965 spin_unlock(&journal->j_state_lock);
967 BUFFER_TRACE(bh, "marking dirty");
968 mark_buffer_dirty(bh);
970 sync_dirty_buffer(bh);
972 ll_rw_block(WRITE, 1, &bh);
975 /* If we have just flushed the log (by marking s_start==0), then
976 * any future commit will have to be careful to update the
977 * superblock again to re-record the true start of the log. */
979 spin_lock(&journal->j_state_lock);
981 journal->j_flags &= ~JFS_FLUSHED;
983 journal->j_flags |= JFS_FLUSHED;
984 spin_unlock(&journal->j_state_lock);
988 * Read the superblock for a given journal, performing initial
989 * validation of the format.
992 static int journal_get_superblock(journal_t *journal)
994 struct buffer_head *bh;
995 journal_superblock_t *sb;
998 bh = journal->j_sb_buffer;
1000 J_ASSERT(bh != NULL);
1001 if (!buffer_uptodate(bh)) {
1002 ll_rw_block(READ, 1, &bh);
1004 if (!buffer_uptodate(bh)) {
1006 "JBD: IO error reading journal superblock\n");
1011 sb = journal->j_superblock;
1015 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1016 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1017 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1021 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1022 case JFS_SUPERBLOCK_V1:
1023 journal->j_format_version = 1;
1025 case JFS_SUPERBLOCK_V2:
1026 journal->j_format_version = 2;
1029 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1033 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1034 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1035 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1036 printk (KERN_WARNING "JBD: journal file too short\n");
1043 journal_fail_superblock(journal);
1048 * Load the on-disk journal superblock and read the key fields into the
1052 static int load_superblock(journal_t *journal)
1055 journal_superblock_t *sb;
1057 err = journal_get_superblock(journal);
1061 sb = journal->j_superblock;
1063 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1064 journal->j_tail = be32_to_cpu(sb->s_start);
1065 journal->j_first = be32_to_cpu(sb->s_first);
1066 journal->j_last = be32_to_cpu(sb->s_maxlen);
1067 journal->j_errno = be32_to_cpu(sb->s_errno);
1074 * int journal_load() - Read journal from disk.
1075 * @journal: Journal to act on.
1077 * Given a journal_t structure which tells us which disk blocks contain
1078 * a journal, read the journal from disk to initialise the in-memory
1081 int journal_load(journal_t *journal)
1085 err = load_superblock(journal);
1089 /* If this is a V2 superblock, then we have to check the
1090 * features flags on it. */
1092 if (journal->j_format_version >= 2) {
1093 journal_superblock_t *sb = journal->j_superblock;
1095 if ((sb->s_feature_ro_compat &
1096 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1097 (sb->s_feature_incompat &
1098 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1099 printk (KERN_WARNING
1100 "JBD: Unrecognised features on journal\n");
1105 /* Let the recovery code check whether it needs to recover any
1106 * data from the journal. */
1107 if (journal_recover(journal))
1108 goto recovery_error;
1110 /* OK, we've finished with the dynamic journal bits:
1111 * reinitialise the dynamic contents of the superblock in memory
1112 * and reset them on disk. */
1113 if (journal_reset(journal))
1114 goto recovery_error;
1116 journal->j_flags &= ~JFS_ABORT;
1117 journal->j_flags |= JFS_LOADED;
1121 printk (KERN_WARNING "JBD: recovery failed\n");
1126 * void journal_destroy() - Release a journal_t structure.
1127 * @journal: Journal to act on.
1129 * Release a journal_t structure once it is no longer in use by the
1132 void journal_destroy(journal_t *journal)
1134 /* Wait for the commit thread to wake up and die. */
1135 journal_kill_thread(journal);
1137 /* Force a final log commit */
1138 if (journal->j_running_transaction)
1139 journal_commit_transaction(journal);
1141 /* Force any old transactions to disk */
1143 /* Totally anal locking here... */
1144 spin_lock(&journal->j_list_lock);
1145 while (journal->j_checkpoint_transactions != NULL) {
1146 spin_unlock(&journal->j_list_lock);
1147 log_do_checkpoint(journal);
1148 spin_lock(&journal->j_list_lock);
1151 J_ASSERT(journal->j_running_transaction == NULL);
1152 J_ASSERT(journal->j_committing_transaction == NULL);
1153 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1154 spin_unlock(&journal->j_list_lock);
1156 /* We can now mark the journal as empty. */
1157 journal->j_tail = 0;
1158 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1159 if (journal->j_sb_buffer) {
1160 journal_update_superblock(journal, 1);
1161 brelse(journal->j_sb_buffer);
1164 if (journal->j_inode)
1165 iput(journal->j_inode);
1166 if (journal->j_revoke)
1167 journal_destroy_revoke(journal);
1168 kfree(journal->j_wbuf);
1174 *int journal_check_used_features () - Check if features specified are used.
1175 * @journal: Journal to check.
1176 * @compat: bitmask of compatible features
1177 * @ro: bitmask of features that force read-only mount
1178 * @incompat: bitmask of incompatible features
1180 * Check whether the journal uses all of a given set of
1181 * features. Return true (non-zero) if it does.
1184 int journal_check_used_features (journal_t *journal, unsigned long compat,
1185 unsigned long ro, unsigned long incompat)
1187 journal_superblock_t *sb;
1189 if (!compat && !ro && !incompat)
1191 if (journal->j_format_version == 1)
1194 sb = journal->j_superblock;
1196 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1197 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1198 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1205 * int journal_check_available_features() - Check feature set in journalling layer
1206 * @journal: Journal to check.
1207 * @compat: bitmask of compatible features
1208 * @ro: bitmask of features that force read-only mount
1209 * @incompat: bitmask of incompatible features
1211 * Check whether the journaling code supports the use of
1212 * all of a given set of features on this journal. Return true
1213 * (non-zero) if it can. */
1215 int journal_check_available_features (journal_t *journal, unsigned long compat,
1216 unsigned long ro, unsigned long incompat)
1218 journal_superblock_t *sb;
1220 if (!compat && !ro && !incompat)
1223 sb = journal->j_superblock;
1225 /* We can support any known requested features iff the
1226 * superblock is in version 2. Otherwise we fail to support any
1227 * extended sb features. */
1229 if (journal->j_format_version != 2)
1232 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1233 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1234 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1241 * int journal_set_features () - Mark a given journal feature in the superblock
1242 * @journal: Journal to act on.
1243 * @compat: bitmask of compatible features
1244 * @ro: bitmask of features that force read-only mount
1245 * @incompat: bitmask of incompatible features
1247 * Mark a given journal feature as present on the
1248 * superblock. Returns true if the requested features could be set.
1252 int journal_set_features (journal_t *journal, unsigned long compat,
1253 unsigned long ro, unsigned long incompat)
1255 journal_superblock_t *sb;
1257 if (journal_check_used_features(journal, compat, ro, incompat))
1260 if (!journal_check_available_features(journal, compat, ro, incompat))
1263 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1264 compat, ro, incompat);
1266 sb = journal->j_superblock;
1268 sb->s_feature_compat |= cpu_to_be32(compat);
1269 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1270 sb->s_feature_incompat |= cpu_to_be32(incompat);
1277 * int journal_update_format () - Update on-disk journal structure.
1278 * @journal: Journal to act on.
1280 * Given an initialised but unloaded journal struct, poke about in the
1281 * on-disk structure to update it to the most recent supported version.
1283 int journal_update_format (journal_t *journal)
1285 journal_superblock_t *sb;
1288 err = journal_get_superblock(journal);
1292 sb = journal->j_superblock;
1294 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1295 case JFS_SUPERBLOCK_V2:
1297 case JFS_SUPERBLOCK_V1:
1298 return journal_convert_superblock_v1(journal, sb);
1305 static int journal_convert_superblock_v1(journal_t *journal,
1306 journal_superblock_t *sb)
1308 int offset, blocksize;
1309 struct buffer_head *bh;
1312 "JBD: Converting superblock from version 1 to 2.\n");
1314 /* Pre-initialise new fields to zero */
1315 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1316 blocksize = be32_to_cpu(sb->s_blocksize);
1317 memset(&sb->s_feature_compat, 0, blocksize-offset);
1319 sb->s_nr_users = cpu_to_be32(1);
1320 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1321 journal->j_format_version = 2;
1323 bh = journal->j_sb_buffer;
1324 BUFFER_TRACE(bh, "marking dirty");
1325 mark_buffer_dirty(bh);
1326 sync_dirty_buffer(bh);
1332 * int journal_flush () - Flush journal
1333 * @journal: Journal to act on.
1335 * Flush all data for a given journal to disk and empty the journal.
1336 * Filesystems can use this when remounting readonly to ensure that
1337 * recovery does not need to happen on remount.
1340 int journal_flush(journal_t *journal)
1343 transaction_t *transaction = NULL;
1344 unsigned long old_tail;
1346 spin_lock(&journal->j_state_lock);
1348 /* Force everything buffered to the log... */
1349 if (journal->j_running_transaction) {
1350 transaction = journal->j_running_transaction;
1351 __log_start_commit(journal, transaction->t_tid);
1352 } else if (journal->j_committing_transaction)
1353 transaction = journal->j_committing_transaction;
1355 /* Wait for the log commit to complete... */
1357 tid_t tid = transaction->t_tid;
1359 spin_unlock(&journal->j_state_lock);
1360 log_wait_commit(journal, tid);
1362 spin_unlock(&journal->j_state_lock);
1365 /* ...and flush everything in the log out to disk. */
1366 spin_lock(&journal->j_list_lock);
1367 while (!err && journal->j_checkpoint_transactions != NULL) {
1368 spin_unlock(&journal->j_list_lock);
1369 err = log_do_checkpoint(journal);
1370 spin_lock(&journal->j_list_lock);
1372 spin_unlock(&journal->j_list_lock);
1373 cleanup_journal_tail(journal);
1375 /* Finally, mark the journal as really needing no recovery.
1376 * This sets s_start==0 in the underlying superblock, which is
1377 * the magic code for a fully-recovered superblock. Any future
1378 * commits of data to the journal will restore the current
1380 spin_lock(&journal->j_state_lock);
1381 old_tail = journal->j_tail;
1382 journal->j_tail = 0;
1383 spin_unlock(&journal->j_state_lock);
1384 journal_update_superblock(journal, 1);
1385 spin_lock(&journal->j_state_lock);
1386 journal->j_tail = old_tail;
1388 J_ASSERT(!journal->j_running_transaction);
1389 J_ASSERT(!journal->j_committing_transaction);
1390 J_ASSERT(!journal->j_checkpoint_transactions);
1391 J_ASSERT(journal->j_head == journal->j_tail);
1392 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1393 spin_unlock(&journal->j_state_lock);
1398 * int journal_wipe() - Wipe journal contents
1399 * @journal: Journal to act on.
1400 * @write: flag (see below)
1402 * Wipe out all of the contents of a journal, safely. This will produce
1403 * a warning if the journal contains any valid recovery information.
1404 * Must be called between journal_init_*() and journal_load().
1406 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1407 * we merely suppress recovery.
1410 int journal_wipe(journal_t *journal, int write)
1412 journal_superblock_t *sb;
1415 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1417 err = load_superblock(journal);
1421 sb = journal->j_superblock;
1423 if (!journal->j_tail)
1426 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1427 write ? "Clearing" : "Ignoring");
1429 err = journal_skip_recovery(journal);
1431 journal_update_superblock(journal, 1);
1438 * journal_dev_name: format a character string to describe on what
1439 * device this journal is present.
1442 const char *journal_dev_name(journal_t *journal, char *buffer)
1444 struct block_device *bdev;
1446 if (journal->j_inode)
1447 bdev = journal->j_inode->i_sb->s_bdev;
1449 bdev = journal->j_dev;
1451 return bdevname(bdev, buffer);
1455 * Journal abort has very specific semantics, which we describe
1456 * for journal abort.
1458 * Two internal function, which provide abort to te jbd layer
1463 * Quick version for internal journal use (doesn't lock the journal).
1464 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1465 * and don't attempt to make any other journal updates.
1467 void __journal_abort_hard(journal_t *journal)
1469 transaction_t *transaction;
1470 char b[BDEVNAME_SIZE];
1472 if (journal->j_flags & JFS_ABORT)
1475 printk(KERN_ERR "Aborting journal on device %s.\n",
1476 journal_dev_name(journal, b));
1478 spin_lock(&journal->j_state_lock);
1479 journal->j_flags |= JFS_ABORT;
1480 transaction = journal->j_running_transaction;
1482 __log_start_commit(journal, transaction->t_tid);
1483 spin_unlock(&journal->j_state_lock);
1486 /* Soft abort: record the abort error status in the journal superblock,
1487 * but don't do any other IO. */
1488 void __journal_abort_soft (journal_t *journal, int errno)
1490 if (journal->j_flags & JFS_ABORT)
1493 if (!journal->j_errno)
1494 journal->j_errno = errno;
1496 __journal_abort_hard(journal);
1499 journal_update_superblock(journal, 1);
1503 * void journal_abort () - Shutdown the journal immediately.
1504 * @journal: the journal to shutdown.
1505 * @errno: an error number to record in the journal indicating
1506 * the reason for the shutdown.
1508 * Perform a complete, immediate shutdown of the ENTIRE
1509 * journal (not of a single transaction). This operation cannot be
1510 * undone without closing and reopening the journal.
1512 * The journal_abort function is intended to support higher level error
1513 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1516 * Journal abort has very specific semantics. Any existing dirty,
1517 * unjournaled buffers in the main filesystem will still be written to
1518 * disk by bdflush, but the journaling mechanism will be suspended
1519 * immediately and no further transaction commits will be honoured.
1521 * Any dirty, journaled buffers will be written back to disk without
1522 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1523 * filesystem, but we _do_ attempt to leave as much data as possible
1524 * behind for fsck to use for cleanup.
1526 * Any attempt to get a new transaction handle on a journal which is in
1527 * ABORT state will just result in an -EROFS error return. A
1528 * journal_stop on an existing handle will return -EIO if we have
1529 * entered abort state during the update.
1531 * Recursive transactions are not disturbed by journal abort until the
1532 * final journal_stop, which will receive the -EIO error.
1534 * Finally, the journal_abort call allows the caller to supply an errno
1535 * which will be recorded (if possible) in the journal superblock. This
1536 * allows a client to record failure conditions in the middle of a
1537 * transaction without having to complete the transaction to record the
1538 * failure to disk. ext3_error, for example, now uses this
1541 * Errors which originate from within the journaling layer will NOT
1542 * supply an errno; a null errno implies that absolutely no further
1543 * writes are done to the journal (unless there are any already in
1548 void journal_abort(journal_t *journal, int errno)
1550 __journal_abort_soft(journal, errno);
1554 * int journal_errno () - returns the journal's error state.
1555 * @journal: journal to examine.
1557 * This is the errno numbet set with journal_abort(), the last
1558 * time the journal was mounted - if the journal was stopped
1559 * without calling abort this will be 0.
1561 * If the journal has been aborted on this mount time -EROFS will
1564 int journal_errno(journal_t *journal)
1568 spin_lock(&journal->j_state_lock);
1569 if (journal->j_flags & JFS_ABORT)
1572 err = journal->j_errno;
1573 spin_unlock(&journal->j_state_lock);
1578 * int journal_clear_err () - clears the journal's error state
1579 * @journal: journal to act on.
1581 * An error must be cleared or Acked to take a FS out of readonly
1584 int journal_clear_err(journal_t *journal)
1588 spin_lock(&journal->j_state_lock);
1589 if (journal->j_flags & JFS_ABORT)
1592 journal->j_errno = 0;
1593 spin_unlock(&journal->j_state_lock);
1598 * void journal_ack_err() - Ack journal err.
1599 * @journal: journal to act on.
1601 * An error must be cleared or Acked to take a FS out of readonly
1604 void journal_ack_err(journal_t *journal)
1606 spin_lock(&journal->j_state_lock);
1607 if (journal->j_errno)
1608 journal->j_flags |= JFS_ACK_ERR;
1609 spin_unlock(&journal->j_state_lock);
1612 int journal_blocks_per_page(struct inode *inode)
1614 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1618 * Simple support for retrying memory allocations. Introduced to help to
1619 * debug different VM deadlock avoidance strategies.
1621 void * __jbd_kmalloc (const char *where, size_t size, int flags, int retry)
1623 return kmalloc(size, flags | (retry ? __GFP_NOFAIL : 0));
1627 * Journal_head storage management
1629 static kmem_cache_t *journal_head_cache;
1630 #ifdef CONFIG_JBD_DEBUG
1631 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1634 static int journal_init_journal_head_cache(void)
1638 J_ASSERT(journal_head_cache == 0);
1639 journal_head_cache = kmem_cache_create("journal_head",
1640 sizeof(struct journal_head),
1646 if (journal_head_cache == 0) {
1648 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1653 static void journal_destroy_journal_head_cache(void)
1655 J_ASSERT(journal_head_cache != NULL);
1656 kmem_cache_destroy(journal_head_cache);
1657 journal_head_cache = NULL;
1661 * journal_head splicing and dicing
1663 static struct journal_head *journal_alloc_journal_head(void)
1665 struct journal_head *ret;
1666 static unsigned long last_warning;
1668 #ifdef CONFIG_JBD_DEBUG
1669 atomic_inc(&nr_journal_heads);
1671 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1673 jbd_debug(1, "out of memory for journal_head\n");
1674 if (time_after(jiffies, last_warning + 5*HZ)) {
1675 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1677 last_warning = jiffies;
1681 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1687 static void journal_free_journal_head(struct journal_head *jh)
1689 #ifdef CONFIG_JBD_DEBUG
1690 atomic_dec(&nr_journal_heads);
1691 memset(jh, 0x5b, sizeof(*jh));
1693 kmem_cache_free(journal_head_cache, jh);
1697 * A journal_head is attached to a buffer_head whenever JBD has an
1698 * interest in the buffer.
1700 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1701 * is set. This bit is tested in core kernel code where we need to take
1702 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1705 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1707 * When a buffer has its BH_JBD bit set it is immune from being released by
1708 * core kernel code, mainly via ->b_count.
1710 * A journal_head may be detached from its buffer_head when the journal_head's
1711 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1712 * Various places in JBD call journal_remove_journal_head() to indicate that the
1713 * journal_head can be dropped if needed.
1715 * Various places in the kernel want to attach a journal_head to a buffer_head
1716 * _before_ attaching the journal_head to a transaction. To protect the
1717 * journal_head in this situation, journal_add_journal_head elevates the
1718 * journal_head's b_jcount refcount by one. The caller must call
1719 * journal_put_journal_head() to undo this.
1721 * So the typical usage would be:
1723 * (Attach a journal_head if needed. Increments b_jcount)
1724 * struct journal_head *jh = journal_add_journal_head(bh);
1726 * jh->b_transaction = xxx;
1727 * journal_put_journal_head(jh);
1729 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1730 * because it has a non-zero b_transaction.
1734 * Give a buffer_head a journal_head.
1736 * Doesn't need the journal lock.
1739 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1741 struct journal_head *jh;
1742 struct journal_head *new_jh = NULL;
1745 if (!buffer_jbd(bh)) {
1746 new_jh = journal_alloc_journal_head();
1747 memset(new_jh, 0, sizeof(*new_jh));
1750 jbd_lock_bh_journal_head(bh);
1751 if (buffer_jbd(bh)) {
1755 (atomic_read(&bh->b_count) > 0) ||
1756 (bh->b_page && bh->b_page->mapping));
1759 jbd_unlock_bh_journal_head(bh);
1764 new_jh = NULL; /* We consumed it */
1769 BUFFER_TRACE(bh, "added journal_head");
1772 jbd_unlock_bh_journal_head(bh);
1774 journal_free_journal_head(new_jh);
1775 return bh->b_private;
1779 * Grab a ref against this buffer_head's journal_head. If it ended up not
1780 * having a journal_head, return NULL
1782 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1784 struct journal_head *jh = NULL;
1786 jbd_lock_bh_journal_head(bh);
1787 if (buffer_jbd(bh)) {
1791 jbd_unlock_bh_journal_head(bh);
1795 static void __journal_remove_journal_head(struct buffer_head *bh)
1797 struct journal_head *jh = bh2jh(bh);
1799 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1802 if (jh->b_jcount == 0) {
1803 if (jh->b_transaction == NULL &&
1804 jh->b_next_transaction == NULL &&
1805 jh->b_cp_transaction == NULL) {
1806 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1807 J_ASSERT_BH(bh, buffer_jbd(bh));
1808 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1809 BUFFER_TRACE(bh, "remove journal_head");
1810 if (jh->b_frozen_data) {
1811 printk(KERN_WARNING "%s: freeing "
1814 kfree(jh->b_frozen_data);
1816 if (jh->b_committed_data) {
1817 printk(KERN_WARNING "%s: freeing "
1818 "b_committed_data\n",
1820 kfree(jh->b_committed_data);
1822 bh->b_private = NULL;
1823 jh->b_bh = NULL; /* debug, really */
1824 clear_buffer_jbd(bh);
1826 journal_free_journal_head(jh);
1828 BUFFER_TRACE(bh, "journal_head was locked");
1834 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1835 * and has a zero b_jcount then remove and release its journal_head. If we did
1836 * see that the buffer is not used by any transaction we also "logically"
1837 * decrement ->b_count.
1839 * We in fact take an additional increment on ->b_count as a convenience,
1840 * because the caller usually wants to do additional things with the bh
1841 * after calling here.
1842 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1843 * time. Once the caller has run __brelse(), the buffer is eligible for
1844 * reaping by try_to_free_buffers().
1846 void journal_remove_journal_head(struct buffer_head *bh)
1848 jbd_lock_bh_journal_head(bh);
1849 __journal_remove_journal_head(bh);
1850 jbd_unlock_bh_journal_head(bh);
1854 * Drop a reference on the passed journal_head. If it fell to zero then try to
1855 * release the journal_head from the buffer_head.
1857 void journal_put_journal_head(struct journal_head *jh)
1859 struct buffer_head *bh = jh2bh(jh);
1861 jbd_lock_bh_journal_head(bh);
1862 J_ASSERT_JH(jh, jh->b_jcount > 0);
1864 if (!jh->b_jcount && !jh->b_transaction) {
1865 __journal_remove_journal_head(bh);
1868 jbd_unlock_bh_journal_head(bh);
1874 #if defined(CONFIG_JBD_DEBUG)
1875 int journal_enable_debug;
1876 EXPORT_SYMBOL(journal_enable_debug);
1879 #if defined(CONFIG_JBD_DEBUG) && defined(CONFIG_PROC_FS)
1881 static struct proc_dir_entry *proc_jbd_debug;
1883 int read_jbd_debug(char *page, char **start, off_t off,
1884 int count, int *eof, void *data)
1888 ret = sprintf(page + off, "%d\n", journal_enable_debug);
1893 int write_jbd_debug(struct file *file, const char __user *buffer,
1894 unsigned long count, void *data)
1898 if (count > ARRAY_SIZE(buf) - 1)
1899 count = ARRAY_SIZE(buf) - 1;
1900 if (copy_from_user(buf, buffer, count))
1902 buf[ARRAY_SIZE(buf) - 1] = '\0';
1903 journal_enable_debug = simple_strtoul(buf, NULL, 10);
1907 #define JBD_PROC_NAME "sys/fs/jbd-debug"
1909 static void __init create_jbd_proc_entry(void)
1911 proc_jbd_debug = create_proc_entry(JBD_PROC_NAME, 0644, NULL);
1912 if (proc_jbd_debug) {
1913 /* Why is this so hard? */
1914 proc_jbd_debug->read_proc = read_jbd_debug;
1915 proc_jbd_debug->write_proc = write_jbd_debug;
1919 static void __exit remove_jbd_proc_entry(void)
1922 remove_proc_entry(JBD_PROC_NAME, NULL);
1927 #define create_jbd_proc_entry() do {} while (0)
1928 #define remove_jbd_proc_entry() do {} while (0)
1932 kmem_cache_t *jbd_handle_cache;
1934 static int __init journal_init_handle_cache(void)
1936 jbd_handle_cache = kmem_cache_create("journal_handle",
1942 if (jbd_handle_cache == NULL) {
1943 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1949 static void journal_destroy_handle_cache(void)
1951 if (jbd_handle_cache)
1952 kmem_cache_destroy(jbd_handle_cache);
1956 * Module startup and shutdown
1959 static int __init journal_init_caches(void)
1963 ret = journal_init_revoke_caches();
1965 ret = journal_init_journal_head_cache();
1967 ret = journal_init_handle_cache();
1971 static void journal_destroy_caches(void)
1973 journal_destroy_revoke_caches();
1974 journal_destroy_journal_head_cache();
1975 journal_destroy_handle_cache();
1978 static int __init journal_init(void)
1982 ret = journal_init_caches();
1984 journal_destroy_caches();
1985 create_jbd_proc_entry();
1989 static void __exit journal_exit(void)
1991 #ifdef CONFIG_JBD_DEBUG
1992 int n = atomic_read(&nr_journal_heads);
1994 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
1996 remove_jbd_proc_entry();
1997 journal_destroy_caches();
2000 MODULE_LICENSE("GPL");
2001 module_init(journal_init);
2002 module_exit(journal_exit);