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_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_bmap);
82 EXPORT_SYMBOL(journal_force_commit);
84 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
87 * Helper function used to manage commit timeouts
90 static void commit_timeout(unsigned long __data)
92 struct task_struct * p = (struct task_struct *) __data;
97 /* Static check for data structure consistency. There's no code
98 * invoked --- we'll just get a linker failure if things aren't right.
100 void __journal_internal_check(void)
102 extern void journal_bad_superblock_size(void);
103 if (sizeof(struct journal_superblock_s) != 1024)
104 journal_bad_superblock_size();
108 * kjournald: The main thread function used to manage a logging device
111 * This kernel thread is responsible for two things:
113 * 1) COMMIT: Every so often we need to commit the current state of the
114 * filesystem to disk. The journal thread is responsible for writing
115 * all of the metadata buffers to disk.
117 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
118 * of the data in that part of the log has been rewritten elsewhere on
119 * the disk. Flushing these old buffers to reclaim space in the log is
120 * known as checkpointing, and this thread is responsible for that job.
123 journal_t *current_journal; // AKPM: debug
125 int kjournald(void *arg)
127 journal_t *journal = (journal_t *) arg;
128 transaction_t *transaction;
129 struct timer_list timer;
131 current_journal = journal;
133 daemonize("kjournald");
135 /* Set up an interval timer which can be used to trigger a
136 commit wakeup after the commit interval expires */
138 timer.data = (unsigned long) current;
139 timer.function = commit_timeout;
140 journal->j_commit_timer = &timer;
142 /* Record that the journal thread is running */
143 journal->j_task = current;
144 wake_up(&journal->j_wait_done_commit);
146 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
147 journal->j_commit_interval / HZ);
150 * And now, wait forever for commit wakeup events.
152 spin_lock(&journal->j_state_lock);
155 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
156 journal->j_commit_sequence, journal->j_commit_request);
158 if (journal->j_commit_sequence != journal->j_commit_request) {
159 jbd_debug(1, "OK, requests differ\n");
160 spin_unlock(&journal->j_state_lock);
161 del_timer_sync(journal->j_commit_timer);
162 journal_commit_transaction(journal);
163 spin_lock(&journal->j_state_lock);
167 wake_up(&journal->j_wait_done_commit);
168 if (current->flags & PF_FREEZE) {
170 * The simpler the better. Flushing journal isn't a
171 * good idea, because that depends on threads that may
172 * be already stopped.
174 jbd_debug(1, "Now suspending kjournald\n");
175 spin_unlock(&journal->j_state_lock);
176 refrigerator(PF_FREEZE);
177 spin_lock(&journal->j_state_lock);
180 * We assume on resume that commits are already there,
184 int should_sleep = 1;
186 prepare_to_wait(&journal->j_wait_commit, &wait,
188 if (journal->j_commit_sequence != journal->j_commit_request)
190 transaction = journal->j_running_transaction;
191 if (transaction && time_after_eq(jiffies,
192 transaction->t_expires))
195 spin_unlock(&journal->j_state_lock);
197 spin_lock(&journal->j_state_lock);
199 finish_wait(&journal->j_wait_commit, &wait);
202 jbd_debug(1, "kjournald wakes\n");
205 * Were we woken up by a commit wakeup event?
207 transaction = journal->j_running_transaction;
208 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
209 journal->j_commit_request = transaction->t_tid;
210 jbd_debug(1, "woke because of timeout\n");
213 if (!(journal->j_flags & JFS_UNMOUNT))
216 spin_unlock(&journal->j_state_lock);
217 del_timer_sync(journal->j_commit_timer);
218 journal->j_task = NULL;
219 wake_up(&journal->j_wait_done_commit);
220 jbd_debug(1, "Journal thread exiting.\n");
224 static void journal_start_thread(journal_t *journal)
226 kernel_thread(kjournald, journal, CLONE_VM|CLONE_FS|CLONE_FILES);
227 wait_event(journal->j_wait_done_commit, journal->j_task != 0);
230 static void journal_kill_thread(journal_t *journal)
232 spin_lock(&journal->j_state_lock);
233 journal->j_flags |= JFS_UNMOUNT;
235 while (journal->j_task) {
236 wake_up(&journal->j_wait_commit);
237 spin_unlock(&journal->j_state_lock);
238 wait_event(journal->j_wait_done_commit, journal->j_task == 0);
239 spin_lock(&journal->j_state_lock);
241 spin_unlock(&journal->j_state_lock);
245 * journal_write_metadata_buffer: write a metadata buffer to the journal.
247 * Writes a metadata buffer to a given disk block. The actual IO is not
248 * performed but a new buffer_head is constructed which labels the data
249 * to be written with the correct destination disk block.
251 * Any magic-number escaping which needs to be done will cause a
252 * copy-out here. If the buffer happens to start with the
253 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
254 * magic number is only written to the log for descripter blocks. In
255 * this case, we copy the data and replace the first word with 0, and we
256 * return a result code which indicates that this buffer needs to be
257 * marked as an escaped buffer in the corresponding log descriptor
258 * block. The missing word can then be restored when the block is read
261 * If the source buffer has already been modified by a new transaction
262 * since we took the last commit snapshot, we use the frozen copy of
263 * that data for IO. If we end up using the existing buffer_head's data
264 * for the write, then we *have* to lock the buffer to prevent anyone
265 * else from using and possibly modifying it while the IO is in
268 * The function returns a pointer to the buffer_heads to be used for IO.
270 * We assume that the journal has already been locked in this function.
277 * Bit 0 set == escape performed on the data
278 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
281 int journal_write_metadata_buffer(transaction_t *transaction,
282 struct journal_head *jh_in,
283 struct journal_head **jh_out,
286 int need_copy_out = 0;
287 int done_copy_out = 0;
290 struct buffer_head *new_bh;
291 struct journal_head *new_jh;
292 struct page *new_page;
293 unsigned int new_offset;
294 struct buffer_head *bh_in = jh2bh(jh_in);
297 * The buffer really shouldn't be locked: only the current committing
298 * transaction is allowed to write it, so nobody else is allowed
301 * akpm: except if we're journalling data, and write() output is
302 * also part of a shared mapping, and another thread has
303 * decided to launch a writepage() against this buffer.
305 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
307 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
310 * If a new transaction has already done a buffer copy-out, then
311 * we use that version of the data for the commit.
313 jbd_lock_bh_state(bh_in);
315 if (jh_in->b_frozen_data) {
317 new_page = virt_to_page(jh_in->b_frozen_data);
318 new_offset = offset_in_page(jh_in->b_frozen_data);
320 new_page = jh2bh(jh_in)->b_page;
321 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
324 mapped_data = kmap_atomic(new_page, KM_USER0);
328 if (*((unsigned int *)(mapped_data + new_offset)) ==
329 htonl(JFS_MAGIC_NUMBER)) {
333 kunmap_atomic(mapped_data, KM_USER0);
336 * Do we need to do a data copy?
338 if (need_copy_out && !done_copy_out) {
341 jbd_unlock_bh_state(bh_in);
342 tmp = jbd_rep_kmalloc(bh_in->b_size, GFP_NOFS);
343 jbd_lock_bh_state(bh_in);
344 if (jh_in->b_frozen_data) {
349 jh_in->b_frozen_data = tmp;
350 mapped_data = kmap_atomic(new_page, KM_USER0);
351 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
352 kunmap_atomic(mapped_data, KM_USER0);
354 new_page = virt_to_page(tmp);
355 new_offset = offset_in_page(tmp);
360 * Did we need to do an escaping? Now we've done all the
361 * copying, we can finally do so.
364 mapped_data = kmap_atomic(new_page, KM_USER0);
365 *((unsigned int *)(mapped_data + new_offset)) = 0;
366 kunmap_atomic(mapped_data, KM_USER0);
369 /* keep subsequent assertions sane */
371 init_buffer(new_bh, NULL, NULL);
372 atomic_set(&new_bh->b_count, 1);
373 jbd_unlock_bh_state(bh_in);
375 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
377 set_bh_page(new_bh, new_page, new_offset);
378 new_jh->b_transaction = NULL;
379 new_bh->b_size = jh2bh(jh_in)->b_size;
380 new_bh->b_bdev = transaction->t_journal->j_dev;
381 new_bh->b_blocknr = blocknr;
382 set_buffer_mapped(new_bh);
383 set_buffer_dirty(new_bh);
388 * The to-be-written buffer needs to get moved to the io queue,
389 * and the original buffer whose contents we are shadowing or
390 * copying is moved to the transaction's shadow queue.
392 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
393 journal_file_buffer(jh_in, transaction, BJ_Shadow);
394 JBUFFER_TRACE(new_jh, "file as BJ_IO");
395 journal_file_buffer(new_jh, transaction, BJ_IO);
397 return do_escape | (done_copy_out << 1);
401 * Allocation code for the journal file. Manage the space left in the
402 * journal, so that we can begin checkpointing when appropriate.
406 * __log_space_left: Return the number of free blocks left in the journal.
408 * Called with the journal already locked.
410 * Called under j_state_lock
413 int __log_space_left(journal_t *journal)
415 int left = journal->j_free;
417 assert_spin_locked(&journal->j_state_lock);
420 * Be pessimistic here about the number of those free blocks which
421 * might be required for log descriptor control blocks.
424 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
426 left -= MIN_LOG_RESERVED_BLOCKS;
435 * Called under j_state_lock. Returns true if a transaction was started.
437 int __log_start_commit(journal_t *journal, tid_t target)
440 * Are we already doing a recent enough commit?
442 if (!tid_geq(journal->j_commit_request, target)) {
444 * We want a new commit: OK, mark the request and wakup the
445 * commit thread. We do _not_ do the commit ourselves.
448 journal->j_commit_request = target;
449 jbd_debug(1, "JBD: requesting commit %d/%d\n",
450 journal->j_commit_request,
451 journal->j_commit_sequence);
452 wake_up(&journal->j_wait_commit);
458 int log_start_commit(journal_t *journal, tid_t tid)
462 spin_lock(&journal->j_state_lock);
463 ret = __log_start_commit(journal, tid);
464 spin_unlock(&journal->j_state_lock);
469 * Force and wait upon a commit if the calling process is not within
470 * transaction. This is used for forcing out undo-protected data which contains
471 * bitmaps, when the fs is running out of space.
473 * We can only force the running transaction if we don't have an active handle;
474 * otherwise, we will deadlock.
476 * Returns true if a transaction was started.
478 int journal_force_commit_nested(journal_t *journal)
480 transaction_t *transaction = NULL;
483 spin_lock(&journal->j_state_lock);
484 if (journal->j_running_transaction && !current->journal_info) {
485 transaction = journal->j_running_transaction;
486 __log_start_commit(journal, transaction->t_tid);
487 } else if (journal->j_committing_transaction)
488 transaction = journal->j_committing_transaction;
491 spin_unlock(&journal->j_state_lock);
492 return 0; /* Nothing to retry */
495 tid = transaction->t_tid;
496 spin_unlock(&journal->j_state_lock);
497 log_wait_commit(journal, tid);
502 * Start a commit of the current running transaction (if any). Returns true
503 * if a transaction was started, and fills its tid in at *ptid
505 int journal_start_commit(journal_t *journal, tid_t *ptid)
509 spin_lock(&journal->j_state_lock);
510 if (journal->j_running_transaction) {
511 tid_t tid = journal->j_running_transaction->t_tid;
513 ret = __log_start_commit(journal, tid);
516 } else if (journal->j_committing_transaction && ptid) {
518 * If ext3_write_super() recently started a commit, then we
519 * have to wait for completion of that transaction
521 *ptid = journal->j_committing_transaction->t_tid;
524 spin_unlock(&journal->j_state_lock);
529 * Wait for a specified commit to complete.
530 * The caller may not hold the journal lock.
532 int log_wait_commit(journal_t *journal, tid_t tid)
536 #ifdef CONFIG_JBD_DEBUG
537 spin_lock(&journal->j_state_lock);
538 if (!tid_geq(journal->j_commit_request, tid)) {
540 "%s: error: j_commit_request=%d, tid=%d\n",
541 __FUNCTION__, journal->j_commit_request, tid);
543 spin_unlock(&journal->j_state_lock);
545 spin_lock(&journal->j_state_lock);
546 while (tid_gt(tid, journal->j_commit_sequence)) {
547 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
548 tid, journal->j_commit_sequence);
549 wake_up(&journal->j_wait_commit);
550 spin_unlock(&journal->j_state_lock);
551 wait_event(journal->j_wait_done_commit,
552 !tid_gt(tid, journal->j_commit_sequence));
553 spin_lock(&journal->j_state_lock);
555 spin_unlock(&journal->j_state_lock);
557 if (unlikely(is_journal_aborted(journal))) {
558 printk(KERN_EMERG "journal commit I/O error\n");
565 * Log buffer allocation routines:
568 int journal_next_log_block(journal_t *journal, unsigned long *retp)
570 unsigned long blocknr;
572 spin_lock(&journal->j_state_lock);
573 J_ASSERT(journal->j_free > 1);
575 blocknr = journal->j_head;
578 if (journal->j_head == journal->j_last)
579 journal->j_head = journal->j_first;
580 spin_unlock(&journal->j_state_lock);
581 return journal_bmap(journal, blocknr, retp);
585 * Conversion of logical to physical block numbers for the journal
587 * On external journals the journal blocks are identity-mapped, so
588 * this is a no-op. If needed, we can use j_blk_offset - everything is
591 int journal_bmap(journal_t *journal, unsigned long blocknr,
597 if (journal->j_inode) {
598 ret = bmap(journal->j_inode, blocknr);
602 char b[BDEVNAME_SIZE];
604 printk(KERN_ALERT "%s: journal block not found "
605 "at offset %lu on %s\n",
608 bdevname(journal->j_dev, b));
610 __journal_abort_soft(journal, err);
613 *retp = blocknr; /* +journal->j_blk_offset */
619 * We play buffer_head aliasing tricks to write data/metadata blocks to
620 * the journal without copying their contents, but for journal
621 * descriptor blocks we do need to generate bona fide buffers.
623 * After the caller of journal_get_descriptor_buffer() has finished modifying
624 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
625 * But we don't bother doing that, so there will be coherency problems with
626 * mmaps of blockdevs which hold live JBD-controlled filesystems.
628 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
630 struct buffer_head *bh;
631 unsigned long blocknr;
634 err = journal_next_log_block(journal, &blocknr);
639 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
641 memset(bh->b_data, 0, journal->j_blocksize);
642 set_buffer_uptodate(bh);
644 BUFFER_TRACE(bh, "return this buffer");
645 return journal_add_journal_head(bh);
649 * Management for journal control blocks: functions to create and
650 * destroy journal_t structures, and to initialise and read existing
651 * journal blocks from disk. */
653 /* First: create and setup a journal_t object in memory. We initialise
654 * very few fields yet: that has to wait until we have created the
655 * journal structures from from scratch, or loaded them from disk. */
657 static journal_t * journal_init_common (void)
662 journal = jbd_kmalloc(sizeof(*journal), GFP_KERNEL);
665 memset(journal, 0, sizeof(*journal));
667 init_waitqueue_head(&journal->j_wait_transaction_locked);
668 init_waitqueue_head(&journal->j_wait_logspace);
669 init_waitqueue_head(&journal->j_wait_done_commit);
670 init_waitqueue_head(&journal->j_wait_checkpoint);
671 init_waitqueue_head(&journal->j_wait_commit);
672 init_waitqueue_head(&journal->j_wait_updates);
673 init_MUTEX(&journal->j_barrier);
674 init_MUTEX(&journal->j_checkpoint_sem);
675 spin_lock_init(&journal->j_revoke_lock);
676 spin_lock_init(&journal->j_list_lock);
677 spin_lock_init(&journal->j_state_lock);
679 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
681 /* The journal is marked for error until we succeed with recovery! */
682 journal->j_flags = JFS_ABORT;
684 /* Set up a default-sized revoke table for the new mount. */
685 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
695 /* journal_init_dev and journal_init_inode:
697 * Create a journal structure assigned some fixed set of disk blocks to
698 * the journal. We don't actually touch those disk blocks yet, but we
699 * need to set up all of the mapping information to tell the journaling
700 * system where the journal blocks are.
705 * journal_t * journal_init_dev() - creates an initialises a journal structure
706 * @bdev: Block device on which to create the journal
707 * @fs_dev: Device which hold journalled filesystem for this journal.
708 * @start: Block nr Start of journal.
709 * @len: Lenght of the journal in blocks.
710 * @blocksize: blocksize of journalling device
711 * @returns: a newly created journal_t *
713 * journal_init_dev creates a journal which maps a fixed contiguous
714 * range of blocks on an arbitrary block device.
717 journal_t * journal_init_dev(struct block_device *bdev,
718 struct block_device *fs_dev,
719 int start, int len, int blocksize)
721 journal_t *journal = journal_init_common();
722 struct buffer_head *bh;
727 journal->j_dev = bdev;
728 journal->j_fs_dev = fs_dev;
729 journal->j_blk_offset = start;
730 journal->j_maxlen = len;
731 journal->j_blocksize = blocksize;
733 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
734 J_ASSERT(bh != NULL);
735 journal->j_sb_buffer = bh;
736 journal->j_superblock = (journal_superblock_t *)bh->b_data;
742 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
743 * @inode: An inode to create the journal in
745 * journal_init_inode creates a journal which maps an on-disk inode as
746 * the journal. The inode must exist already, must support bmap() and
747 * must have all data blocks preallocated.
749 journal_t * journal_init_inode (struct inode *inode)
751 struct buffer_head *bh;
752 journal_t *journal = journal_init_common();
754 unsigned long blocknr;
759 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
760 journal->j_inode = inode;
762 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
763 journal, inode->i_sb->s_id, inode->i_ino,
764 (long long) inode->i_size,
765 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
767 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
768 journal->j_blocksize = inode->i_sb->s_blocksize;
770 err = journal_bmap(journal, 0, &blocknr);
771 /* If that failed, give up */
773 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
779 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
780 J_ASSERT(bh != NULL);
781 journal->j_sb_buffer = bh;
782 journal->j_superblock = (journal_superblock_t *)bh->b_data;
788 * If the journal init or create aborts, we need to mark the journal
789 * superblock as being NULL to prevent the journal destroy from writing
790 * back a bogus superblock.
792 static void journal_fail_superblock (journal_t *journal)
794 struct buffer_head *bh = journal->j_sb_buffer;
796 journal->j_sb_buffer = NULL;
800 * Given a journal_t structure, initialise the various fields for
801 * startup of a new journaling session. We use this both when creating
802 * a journal, and after recovering an old journal to reset it for
806 static int journal_reset(journal_t *journal)
808 journal_superblock_t *sb = journal->j_superblock;
809 unsigned int first, last;
811 first = ntohl(sb->s_first);
812 last = ntohl(sb->s_maxlen);
814 journal->j_first = first;
815 journal->j_last = last;
817 journal->j_head = first;
818 journal->j_tail = first;
819 journal->j_free = last - first;
821 journal->j_tail_sequence = journal->j_transaction_sequence;
822 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
823 journal->j_commit_request = journal->j_commit_sequence;
825 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
827 /* Add the dynamic fields and write it to disk. */
828 journal_update_superblock(journal, 1);
829 journal_start_thread(journal);
834 * int journal_create() - Initialise the new journal file
835 * @journal: Journal to create. This structure must have been initialised
837 * Given a journal_t structure which tells us which disk blocks we can
838 * use, create a new journal superblock and initialise all of the
839 * journal fields from scratch.
841 int journal_create(journal_t *journal)
843 unsigned long blocknr;
844 struct buffer_head *bh;
845 journal_superblock_t *sb;
848 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
849 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
851 journal_fail_superblock(journal);
855 if (journal->j_inode == NULL) {
857 * We don't know what block to start at!
860 "%s: creation of journal on external device!\n",
865 /* Zero out the entire journal on disk. We cannot afford to
866 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
867 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
868 for (i = 0; i < journal->j_maxlen; i++) {
869 err = journal_bmap(journal, i, &blocknr);
872 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
874 memset (bh->b_data, 0, journal->j_blocksize);
875 BUFFER_TRACE(bh, "marking dirty");
876 mark_buffer_dirty(bh);
877 BUFFER_TRACE(bh, "marking uptodate");
878 set_buffer_uptodate(bh);
883 sync_blockdev(journal->j_dev);
884 jbd_debug(1, "JBD: journal cleared.\n");
886 /* OK, fill in the initial static fields in the new superblock */
887 sb = journal->j_superblock;
889 sb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
890 sb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
892 sb->s_blocksize = htonl(journal->j_blocksize);
893 sb->s_maxlen = htonl(journal->j_maxlen);
894 sb->s_first = htonl(1);
896 journal->j_transaction_sequence = 1;
898 journal->j_flags &= ~JFS_ABORT;
899 journal->j_format_version = 2;
901 return journal_reset(journal);
905 * void journal_update_superblock() - Update journal sb on disk.
906 * @journal: The journal to update.
907 * @wait: Set to '0' if you don't want to wait for IO completion.
909 * Update a journal's dynamic superblock fields and write it to disk,
910 * optionally waiting for the IO to complete.
912 void journal_update_superblock(journal_t *journal, int wait)
914 journal_superblock_t *sb = journal->j_superblock;
915 struct buffer_head *bh = journal->j_sb_buffer;
918 * As a special case, if the on-disk copy is already marked as needing
919 * no recovery (s_start == 0) and there are no outstanding transactions
920 * in the filesystem, then we can safely defer the superblock update
921 * until the next commit by setting JFS_FLUSHED. This avoids
922 * attempting a write to a potential-readonly device.
924 if (sb->s_start == 0 && journal->j_tail_sequence ==
925 journal->j_transaction_sequence) {
926 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
927 "(start %ld, seq %d, errno %d)\n",
928 journal->j_tail, journal->j_tail_sequence,
933 spin_lock(&journal->j_state_lock);
934 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
935 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
937 sb->s_sequence = htonl(journal->j_tail_sequence);
938 sb->s_start = htonl(journal->j_tail);
939 sb->s_errno = htonl(journal->j_errno);
940 spin_unlock(&journal->j_state_lock);
942 BUFFER_TRACE(bh, "marking dirty");
943 mark_buffer_dirty(bh);
945 sync_dirty_buffer(bh);
947 ll_rw_block(WRITE, 1, &bh);
950 /* If we have just flushed the log (by marking s_start==0), then
951 * any future commit will have to be careful to update the
952 * superblock again to re-record the true start of the log. */
954 spin_lock(&journal->j_state_lock);
956 journal->j_flags &= ~JFS_FLUSHED;
958 journal->j_flags |= JFS_FLUSHED;
959 spin_unlock(&journal->j_state_lock);
963 * Read the superblock for a given journal, performing initial
964 * validation of the format.
967 static int journal_get_superblock(journal_t *journal)
969 struct buffer_head *bh;
970 journal_superblock_t *sb;
973 bh = journal->j_sb_buffer;
975 J_ASSERT(bh != NULL);
976 if (!buffer_uptodate(bh)) {
977 ll_rw_block(READ, 1, &bh);
979 if (!buffer_uptodate(bh)) {
981 "JBD: IO error reading journal superblock\n");
986 sb = journal->j_superblock;
990 if (sb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
991 sb->s_blocksize != htonl(journal->j_blocksize)) {
992 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
996 switch(ntohl(sb->s_header.h_blocktype)) {
997 case JFS_SUPERBLOCK_V1:
998 journal->j_format_version = 1;
1000 case JFS_SUPERBLOCK_V2:
1001 journal->j_format_version = 2;
1004 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1008 if (ntohl(sb->s_maxlen) < journal->j_maxlen)
1009 journal->j_maxlen = ntohl(sb->s_maxlen);
1010 else if (ntohl(sb->s_maxlen) > journal->j_maxlen) {
1011 printk (KERN_WARNING "JBD: journal file too short\n");
1018 journal_fail_superblock(journal);
1023 * Load the on-disk journal superblock and read the key fields into the
1027 static int load_superblock(journal_t *journal)
1030 journal_superblock_t *sb;
1032 err = journal_get_superblock(journal);
1036 sb = journal->j_superblock;
1038 journal->j_tail_sequence = ntohl(sb->s_sequence);
1039 journal->j_tail = ntohl(sb->s_start);
1040 journal->j_first = ntohl(sb->s_first);
1041 journal->j_last = ntohl(sb->s_maxlen);
1042 journal->j_errno = ntohl(sb->s_errno);
1049 * int journal_load() - Read journal from disk.
1050 * @journal: Journal to act on.
1052 * Given a journal_t structure which tells us which disk blocks contain
1053 * a journal, read the journal from disk to initialise the in-memory
1056 int journal_load(journal_t *journal)
1060 err = load_superblock(journal);
1064 /* If this is a V2 superblock, then we have to check the
1065 * features flags on it. */
1067 if (journal->j_format_version >= 2) {
1068 journal_superblock_t *sb = journal->j_superblock;
1070 if ((sb->s_feature_ro_compat &
1071 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1072 (sb->s_feature_incompat &
1073 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1074 printk (KERN_WARNING
1075 "JBD: Unrecognised features on journal\n");
1080 /* Let the recovery code check whether it needs to recover any
1081 * data from the journal. */
1082 if (journal_recover(journal))
1083 goto recovery_error;
1085 /* OK, we've finished with the dynamic journal bits:
1086 * reinitialise the dynamic contents of the superblock in memory
1087 * and reset them on disk. */
1088 if (journal_reset(journal))
1089 goto recovery_error;
1091 journal->j_flags &= ~JFS_ABORT;
1092 journal->j_flags |= JFS_LOADED;
1096 printk (KERN_WARNING "JBD: recovery failed\n");
1101 * void journal_destroy() - Release a journal_t structure.
1102 * @journal: Journal to act on.
1104 * Release a journal_t structure once it is no longer in use by the
1107 void journal_destroy(journal_t *journal)
1109 /* Wait for the commit thread to wake up and die. */
1110 journal_kill_thread(journal);
1112 /* Force a final log commit */
1113 if (journal->j_running_transaction)
1114 journal_commit_transaction(journal);
1116 /* Force any old transactions to disk */
1118 /* Totally anal locking here... */
1119 spin_lock(&journal->j_list_lock);
1120 while (journal->j_checkpoint_transactions != NULL) {
1121 spin_unlock(&journal->j_list_lock);
1122 log_do_checkpoint(journal);
1123 spin_lock(&journal->j_list_lock);
1126 J_ASSERT(journal->j_running_transaction == NULL);
1127 J_ASSERT(journal->j_committing_transaction == NULL);
1128 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1129 spin_unlock(&journal->j_list_lock);
1131 /* We can now mark the journal as empty. */
1132 journal->j_tail = 0;
1133 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1134 if (journal->j_sb_buffer) {
1135 journal_update_superblock(journal, 1);
1136 brelse(journal->j_sb_buffer);
1139 if (journal->j_inode)
1140 iput(journal->j_inode);
1141 if (journal->j_revoke)
1142 journal_destroy_revoke(journal);
1148 *int journal_check_used_features () - Check if features specified are used.
1150 * Check whether the journal uses all of a given set of
1151 * features. Return true (non-zero) if it does.
1154 int journal_check_used_features (journal_t *journal, unsigned long compat,
1155 unsigned long ro, unsigned long incompat)
1157 journal_superblock_t *sb;
1159 if (!compat && !ro && !incompat)
1161 if (journal->j_format_version == 1)
1164 sb = journal->j_superblock;
1166 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1167 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1168 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1175 * int journal_check_available_features() - Check feature set in journalling layer
1177 * Check whether the journaling code supports the use of
1178 * all of a given set of features on this journal. Return true
1179 * (non-zero) if it can. */
1181 int journal_check_available_features (journal_t *journal, unsigned long compat,
1182 unsigned long ro, unsigned long incompat)
1184 journal_superblock_t *sb;
1186 if (!compat && !ro && !incompat)
1189 sb = journal->j_superblock;
1191 /* We can support any known requested features iff the
1192 * superblock is in version 2. Otherwise we fail to support any
1193 * extended sb features. */
1195 if (journal->j_format_version != 2)
1198 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1199 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1200 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1207 * int journal_set_features () - Mark a given journal feature in the superblock
1209 * Mark a given journal feature as present on the
1210 * superblock. Returns true if the requested features could be set.
1214 int journal_set_features (journal_t *journal, unsigned long compat,
1215 unsigned long ro, unsigned long incompat)
1217 journal_superblock_t *sb;
1219 if (journal_check_used_features(journal, compat, ro, incompat))
1222 if (!journal_check_available_features(journal, compat, ro, incompat))
1225 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1226 compat, ro, incompat);
1228 sb = journal->j_superblock;
1230 sb->s_feature_compat |= cpu_to_be32(compat);
1231 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1232 sb->s_feature_incompat |= cpu_to_be32(incompat);
1239 * int journal_update_format () - Update on-disk journal structure.
1241 * Given an initialised but unloaded journal struct, poke about in the
1242 * on-disk structure to update it to the most recent supported version.
1244 int journal_update_format (journal_t *journal)
1246 journal_superblock_t *sb;
1249 err = journal_get_superblock(journal);
1253 sb = journal->j_superblock;
1255 switch (ntohl(sb->s_header.h_blocktype)) {
1256 case JFS_SUPERBLOCK_V2:
1258 case JFS_SUPERBLOCK_V1:
1259 return journal_convert_superblock_v1(journal, sb);
1266 static int journal_convert_superblock_v1(journal_t *journal,
1267 journal_superblock_t *sb)
1269 int offset, blocksize;
1270 struct buffer_head *bh;
1273 "JBD: Converting superblock from version 1 to 2.\n");
1275 /* Pre-initialise new fields to zero */
1276 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1277 blocksize = ntohl(sb->s_blocksize);
1278 memset(&sb->s_feature_compat, 0, blocksize-offset);
1280 sb->s_nr_users = cpu_to_be32(1);
1281 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1282 journal->j_format_version = 2;
1284 bh = journal->j_sb_buffer;
1285 BUFFER_TRACE(bh, "marking dirty");
1286 mark_buffer_dirty(bh);
1287 sync_dirty_buffer(bh);
1293 * int journal_flush () - Flush journal
1294 * @journal: Journal to act on.
1296 * Flush all data for a given journal to disk and empty the journal.
1297 * Filesystems can use this when remounting readonly to ensure that
1298 * recovery does not need to happen on remount.
1301 int journal_flush(journal_t *journal)
1304 transaction_t *transaction = NULL;
1305 unsigned long old_tail;
1307 spin_lock(&journal->j_state_lock);
1309 /* Force everything buffered to the log... */
1310 if (journal->j_running_transaction) {
1311 transaction = journal->j_running_transaction;
1312 __log_start_commit(journal, transaction->t_tid);
1313 } else if (journal->j_committing_transaction)
1314 transaction = journal->j_committing_transaction;
1316 /* Wait for the log commit to complete... */
1318 tid_t tid = transaction->t_tid;
1320 spin_unlock(&journal->j_state_lock);
1321 log_wait_commit(journal, tid);
1323 spin_unlock(&journal->j_state_lock);
1326 /* ...and flush everything in the log out to disk. */
1327 spin_lock(&journal->j_list_lock);
1328 while (!err && journal->j_checkpoint_transactions != NULL) {
1329 spin_unlock(&journal->j_list_lock);
1330 err = log_do_checkpoint(journal);
1331 spin_lock(&journal->j_list_lock);
1333 spin_unlock(&journal->j_list_lock);
1334 cleanup_journal_tail(journal);
1336 /* Finally, mark the journal as really needing no recovery.
1337 * This sets s_start==0 in the underlying superblock, which is
1338 * the magic code for a fully-recovered superblock. Any future
1339 * commits of data to the journal will restore the current
1341 spin_lock(&journal->j_state_lock);
1342 old_tail = journal->j_tail;
1343 journal->j_tail = 0;
1344 spin_unlock(&journal->j_state_lock);
1345 journal_update_superblock(journal, 1);
1346 spin_lock(&journal->j_state_lock);
1347 journal->j_tail = old_tail;
1349 J_ASSERT(!journal->j_running_transaction);
1350 J_ASSERT(!journal->j_committing_transaction);
1351 J_ASSERT(!journal->j_checkpoint_transactions);
1352 J_ASSERT(journal->j_head == journal->j_tail);
1353 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1354 spin_unlock(&journal->j_state_lock);
1359 * int journal_wipe() - Wipe journal contents
1360 * @journal: Journal to act on.
1361 * @write: flag (see below)
1363 * Wipe out all of the contents of a journal, safely. This will produce
1364 * a warning if the journal contains any valid recovery information.
1365 * Must be called between journal_init_*() and journal_load().
1367 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1368 * we merely suppress recovery.
1371 int journal_wipe(journal_t *journal, int write)
1373 journal_superblock_t *sb;
1376 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1378 err = load_superblock(journal);
1382 sb = journal->j_superblock;
1384 if (!journal->j_tail)
1387 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1388 write ? "Clearing" : "Ignoring");
1390 err = journal_skip_recovery(journal);
1392 journal_update_superblock(journal, 1);
1399 * journal_dev_name: format a character string to describe on what
1400 * device this journal is present.
1403 const char *journal_dev_name(journal_t *journal, char *buffer)
1405 struct block_device *bdev;
1407 if (journal->j_inode)
1408 bdev = journal->j_inode->i_sb->s_bdev;
1410 bdev = journal->j_dev;
1412 return bdevname(bdev, buffer);
1416 * Journal abort has very specific semantics, which we describe
1417 * for journal abort.
1419 * Two internal function, which provide abort to te jbd layer
1424 * Quick version for internal journal use (doesn't lock the journal).
1425 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1426 * and don't attempt to make any other journal updates.
1428 void __journal_abort_hard(journal_t *journal)
1430 transaction_t *transaction;
1431 char b[BDEVNAME_SIZE];
1433 if (journal->j_flags & JFS_ABORT)
1436 printk(KERN_ERR "Aborting journal on device %s.\n",
1437 journal_dev_name(journal, b));
1439 spin_lock(&journal->j_state_lock);
1440 journal->j_flags |= JFS_ABORT;
1441 transaction = journal->j_running_transaction;
1443 __log_start_commit(journal, transaction->t_tid);
1444 spin_unlock(&journal->j_state_lock);
1447 /* Soft abort: record the abort error status in the journal superblock,
1448 * but don't do any other IO. */
1449 void __journal_abort_soft (journal_t *journal, int errno)
1451 if (journal->j_flags & JFS_ABORT)
1454 if (!journal->j_errno)
1455 journal->j_errno = errno;
1457 __journal_abort_hard(journal);
1460 journal_update_superblock(journal, 1);
1464 * void journal_abort () - Shutdown the journal immediately.
1465 * @journal: the journal to shutdown.
1466 * @errno: an error number to record in the journal indicating
1467 * the reason for the shutdown.
1469 * Perform a complete, immediate shutdown of the ENTIRE
1470 * journal (not of a single transaction). This operation cannot be
1471 * undone without closing and reopening the journal.
1473 * The journal_abort function is intended to support higher level error
1474 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1477 * Journal abort has very specific semantics. Any existing dirty,
1478 * unjournaled buffers in the main filesystem will still be written to
1479 * disk by bdflush, but the journaling mechanism will be suspended
1480 * immediately and no further transaction commits will be honoured.
1482 * Any dirty, journaled buffers will be written back to disk without
1483 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1484 * filesystem, but we _do_ attempt to leave as much data as possible
1485 * behind for fsck to use for cleanup.
1487 * Any attempt to get a new transaction handle on a journal which is in
1488 * ABORT state will just result in an -EROFS error return. A
1489 * journal_stop on an existing handle will return -EIO if we have
1490 * entered abort state during the update.
1492 * Recursive transactions are not disturbed by journal abort until the
1493 * final journal_stop, which will receive the -EIO error.
1495 * Finally, the journal_abort call allows the caller to supply an errno
1496 * which will be recorded (if possible) in the journal superblock. This
1497 * allows a client to record failure conditions in the middle of a
1498 * transaction without having to complete the transaction to record the
1499 * failure to disk. ext3_error, for example, now uses this
1502 * Errors which originate from within the journaling layer will NOT
1503 * supply an errno; a null errno implies that absolutely no further
1504 * writes are done to the journal (unless there are any already in
1509 void journal_abort(journal_t *journal, int errno)
1511 __journal_abort_soft(journal, errno);
1515 * int journal_errno () - returns the journal's error state.
1516 * @journal: journal to examine.
1518 * This is the errno numbet set with journal_abort(), the last
1519 * time the journal was mounted - if the journal was stopped
1520 * without calling abort this will be 0.
1522 * If the journal has been aborted on this mount time -EROFS will
1525 int journal_errno(journal_t *journal)
1529 spin_lock(&journal->j_state_lock);
1530 if (journal->j_flags & JFS_ABORT)
1533 err = journal->j_errno;
1534 spin_unlock(&journal->j_state_lock);
1539 * int journal_clear_err () - clears the journal's error state
1541 * An error must be cleared or Acked to take a FS out of readonly
1544 int journal_clear_err(journal_t *journal)
1548 spin_lock(&journal->j_state_lock);
1549 if (journal->j_flags & JFS_ABORT)
1552 journal->j_errno = 0;
1553 spin_unlock(&journal->j_state_lock);
1558 * void journal_ack_err() - Ack journal err.
1560 * An error must be cleared or Acked to take a FS out of readonly
1563 void journal_ack_err(journal_t *journal)
1565 spin_lock(&journal->j_state_lock);
1566 if (journal->j_errno)
1567 journal->j_flags |= JFS_ACK_ERR;
1568 spin_unlock(&journal->j_state_lock);
1571 int journal_blocks_per_page(struct inode *inode)
1573 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1577 * Simple support for retying memory allocations. Introduced to help to
1578 * debug different VM deadlock avoidance strategies.
1581 * Simple support for retying memory allocations. Introduced to help to
1582 * debug different VM deadlock avoidance strategies.
1584 void * __jbd_kmalloc (const char *where, size_t size, int flags, int retry)
1586 return kmalloc(size, flags | (retry ? __GFP_NOFAIL : 0));
1590 * Journal_head storage management
1592 static kmem_cache_t *journal_head_cache;
1593 #ifdef CONFIG_JBD_DEBUG
1594 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1597 static int journal_init_journal_head_cache(void)
1601 J_ASSERT(journal_head_cache == 0);
1602 journal_head_cache = kmem_cache_create("journal_head",
1603 sizeof(struct journal_head),
1609 if (journal_head_cache == 0) {
1611 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1616 static void journal_destroy_journal_head_cache(void)
1618 J_ASSERT(journal_head_cache != NULL);
1619 kmem_cache_destroy(journal_head_cache);
1620 journal_head_cache = NULL;
1624 * journal_head splicing and dicing
1626 static struct journal_head *journal_alloc_journal_head(void)
1628 struct journal_head *ret;
1629 static unsigned long last_warning;
1631 #ifdef CONFIG_JBD_DEBUG
1632 atomic_inc(&nr_journal_heads);
1634 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1636 jbd_debug(1, "out of memory for journal_head\n");
1637 if (time_after(jiffies, last_warning + 5*HZ)) {
1638 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1640 last_warning = jiffies;
1644 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1650 static void journal_free_journal_head(struct journal_head *jh)
1652 #ifdef CONFIG_JBD_DEBUG
1653 atomic_dec(&nr_journal_heads);
1654 memset(jh, 0x5b, sizeof(*jh));
1656 kmem_cache_free(journal_head_cache, jh);
1660 * A journal_head is attached to a buffer_head whenever JBD has an
1661 * interest in the buffer.
1663 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1664 * is set. This bit is tested in core kernel code where we need to take
1665 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1668 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1670 * When a buffer has its BH_JBD bit set it is immune from being released by
1671 * core kernel code, mainly via ->b_count.
1673 * A journal_head may be detached from its buffer_head when the journal_head's
1674 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1675 * Various places in JBD call journal_remove_journal_head() to indicate that the
1676 * journal_head can be dropped if needed.
1678 * Various places in the kernel want to attach a journal_head to a buffer_head
1679 * _before_ attaching the journal_head to a transaction. To protect the
1680 * journal_head in this situation, journal_add_journal_head elevates the
1681 * journal_head's b_jcount refcount by one. The caller must call
1682 * journal_put_journal_head() to undo this.
1684 * So the typical usage would be:
1686 * (Attach a journal_head if needed. Increments b_jcount)
1687 * struct journal_head *jh = journal_add_journal_head(bh);
1689 * jh->b_transaction = xxx;
1690 * journal_put_journal_head(jh);
1692 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1693 * because it has a non-zero b_transaction.
1697 * Give a buffer_head a journal_head.
1699 * Doesn't need the journal lock.
1702 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1704 struct journal_head *jh;
1705 struct journal_head *new_jh = NULL;
1708 if (!buffer_jbd(bh)) {
1709 new_jh = journal_alloc_journal_head();
1710 memset(new_jh, 0, sizeof(*new_jh));
1713 jbd_lock_bh_journal_head(bh);
1714 if (buffer_jbd(bh)) {
1718 (atomic_read(&bh->b_count) > 0) ||
1719 (bh->b_page && bh->b_page->mapping));
1722 jbd_unlock_bh_journal_head(bh);
1727 new_jh = NULL; /* We consumed it */
1732 BUFFER_TRACE(bh, "added journal_head");
1735 jbd_unlock_bh_journal_head(bh);
1737 journal_free_journal_head(new_jh);
1738 return bh->b_private;
1742 * Grab a ref against this buffer_head's journal_head. If it ended up not
1743 * having a journal_head, return NULL
1745 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1747 struct journal_head *jh = NULL;
1749 jbd_lock_bh_journal_head(bh);
1750 if (buffer_jbd(bh)) {
1754 jbd_unlock_bh_journal_head(bh);
1758 static void __journal_remove_journal_head(struct buffer_head *bh)
1760 struct journal_head *jh = bh2jh(bh);
1762 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1765 if (jh->b_jcount == 0) {
1766 if (jh->b_transaction == NULL &&
1767 jh->b_next_transaction == NULL &&
1768 jh->b_cp_transaction == NULL) {
1769 J_ASSERT_BH(bh, buffer_jbd(bh));
1770 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1771 BUFFER_TRACE(bh, "remove journal_head");
1772 if (jh->b_frozen_data) {
1773 printk(KERN_WARNING "%s: freeing "
1776 kfree(jh->b_frozen_data);
1778 if (jh->b_committed_data) {
1779 printk(KERN_WARNING "%s: freeing "
1780 "b_committed_data\n",
1782 kfree(jh->b_committed_data);
1784 bh->b_private = NULL;
1785 jh->b_bh = NULL; /* debug, really */
1786 clear_buffer_jbd(bh);
1788 journal_free_journal_head(jh);
1790 BUFFER_TRACE(bh, "journal_head was locked");
1796 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1797 * and has a zero b_jcount then remove and release its journal_head. If we did
1798 * see that the buffer is not used by any transaction we also "logically"
1799 * decrement ->b_count.
1801 * We in fact take an additional increment on ->b_count as a convenience,
1802 * because the caller usually wants to do additional things with the bh
1803 * after calling here.
1804 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1805 * time. Once the caller has run __brelse(), the buffer is eligible for
1806 * reaping by try_to_free_buffers().
1808 void journal_remove_journal_head(struct buffer_head *bh)
1810 jbd_lock_bh_journal_head(bh);
1811 __journal_remove_journal_head(bh);
1812 jbd_unlock_bh_journal_head(bh);
1816 * Drop a reference on the passed journal_head. If it fell to zero then try to
1817 * release the journal_head from the buffer_head.
1819 void journal_put_journal_head(struct journal_head *jh)
1821 struct buffer_head *bh = jh2bh(jh);
1823 jbd_lock_bh_journal_head(bh);
1824 J_ASSERT_JH(jh, jh->b_jcount > 0);
1826 if (!jh->b_jcount && !jh->b_transaction) {
1827 __journal_remove_journal_head(bh);
1830 jbd_unlock_bh_journal_head(bh);
1836 #if defined(CONFIG_JBD_DEBUG)
1837 int journal_enable_debug;
1838 EXPORT_SYMBOL(journal_enable_debug);
1841 #if defined(CONFIG_JBD_DEBUG) && defined(CONFIG_PROC_FS)
1843 static struct proc_dir_entry *proc_jbd_debug;
1845 int read_jbd_debug(char *page, char **start, off_t off,
1846 int count, int *eof, void *data)
1850 ret = sprintf(page + off, "%d\n", journal_enable_debug);
1855 int write_jbd_debug(struct file *file, const char __user *buffer,
1856 unsigned long count, void *data)
1860 if (count > ARRAY_SIZE(buf) - 1)
1861 count = ARRAY_SIZE(buf) - 1;
1862 if (copy_from_user(buf, buffer, count))
1864 buf[ARRAY_SIZE(buf) - 1] = '\0';
1865 journal_enable_debug = simple_strtoul(buf, NULL, 10);
1869 #define JBD_PROC_NAME "sys/fs/jbd-debug"
1871 static void __init create_jbd_proc_entry(void)
1873 proc_jbd_debug = create_proc_entry(JBD_PROC_NAME, 0644, NULL);
1874 if (proc_jbd_debug) {
1875 /* Why is this so hard? */
1876 proc_jbd_debug->read_proc = read_jbd_debug;
1877 proc_jbd_debug->write_proc = write_jbd_debug;
1881 static void __exit remove_jbd_proc_entry(void)
1884 remove_proc_entry(JBD_PROC_NAME, NULL);
1889 #define create_jbd_proc_entry() do {} while (0)
1890 #define remove_jbd_proc_entry() do {} while (0)
1894 kmem_cache_t *jbd_handle_cache;
1896 static int __init journal_init_handle_cache(void)
1898 jbd_handle_cache = kmem_cache_create("journal_handle",
1904 if (jbd_handle_cache == NULL) {
1905 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1911 static void journal_destroy_handle_cache(void)
1913 if (jbd_handle_cache)
1914 kmem_cache_destroy(jbd_handle_cache);
1918 * Module startup and shutdown
1921 static int __init journal_init_caches(void)
1925 ret = journal_init_revoke_caches();
1927 ret = journal_init_journal_head_cache();
1929 ret = journal_init_handle_cache();
1933 static void journal_destroy_caches(void)
1935 journal_destroy_revoke_caches();
1936 journal_destroy_journal_head_cache();
1937 journal_destroy_handle_cache();
1940 static int __init journal_init(void)
1944 ret = journal_init_caches();
1946 journal_destroy_caches();
1947 create_jbd_proc_entry();
1951 static void __exit journal_exit(void)
1953 #ifdef CONFIG_JBD_DEBUG
1954 int n = atomic_read(&nr_journal_heads);
1956 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
1958 remove_jbd_proc_entry();
1959 journal_destroy_caches();
1962 MODULE_LICENSE("GPL");
1963 module_init(journal_init);
1964 module_exit(journal_exit);