2 * linux/fs/jbd/journal.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
12 * Generic filesystem journal-writing code; part of the ext2fs
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 <linux/kthread.h>
37 #include <linux/poison.h>
38 #include <linux/proc_fs.h>
40 #include <asm/uaccess.h>
43 EXPORT_SYMBOL(journal_start
);
44 EXPORT_SYMBOL(journal_restart
);
45 EXPORT_SYMBOL(journal_extend
);
46 EXPORT_SYMBOL(journal_stop
);
47 EXPORT_SYMBOL(journal_lock_updates
);
48 EXPORT_SYMBOL(journal_unlock_updates
);
49 EXPORT_SYMBOL(journal_get_write_access
);
50 EXPORT_SYMBOL(journal_get_create_access
);
51 EXPORT_SYMBOL(journal_get_undo_access
);
52 EXPORT_SYMBOL(journal_dirty_data
);
53 EXPORT_SYMBOL(journal_dirty_metadata
);
54 EXPORT_SYMBOL(journal_release_buffer
);
55 EXPORT_SYMBOL(journal_forget
);
57 EXPORT_SYMBOL(journal_sync_buffer
);
59 EXPORT_SYMBOL(journal_flush
);
60 EXPORT_SYMBOL(journal_revoke
);
62 EXPORT_SYMBOL(journal_init_dev
);
63 EXPORT_SYMBOL(journal_init_inode
);
64 EXPORT_SYMBOL(journal_update_format
);
65 EXPORT_SYMBOL(journal_check_used_features
);
66 EXPORT_SYMBOL(journal_check_available_features
);
67 EXPORT_SYMBOL(journal_set_features
);
68 EXPORT_SYMBOL(journal_create
);
69 EXPORT_SYMBOL(journal_load
);
70 EXPORT_SYMBOL(journal_destroy
);
71 EXPORT_SYMBOL(journal_update_superblock
);
72 EXPORT_SYMBOL(journal_abort
);
73 EXPORT_SYMBOL(journal_errno
);
74 EXPORT_SYMBOL(journal_ack_err
);
75 EXPORT_SYMBOL(journal_clear_err
);
76 EXPORT_SYMBOL(log_wait_commit
);
77 EXPORT_SYMBOL(journal_start_commit
);
78 EXPORT_SYMBOL(journal_force_commit_nested
);
79 EXPORT_SYMBOL(journal_wipe
);
80 EXPORT_SYMBOL(journal_blocks_per_page
);
81 EXPORT_SYMBOL(journal_invalidatepage
);
82 EXPORT_SYMBOL(journal_try_to_free_buffers
);
83 EXPORT_SYMBOL(journal_force_commit
);
85 static int journal_convert_superblock_v1(journal_t
*, journal_superblock_t
*);
86 static void __journal_abort_soft (journal_t
*journal
, int errno
);
87 static int journal_create_jbd_slab(size_t slab_size
);
90 * Helper function used to manage commit timeouts
93 static void commit_timeout(unsigned long __data
)
95 struct task_struct
* p
= (struct task_struct
*) __data
;
101 * kjournald: The main thread function used to manage a logging device
104 * This kernel thread is responsible for two things:
106 * 1) COMMIT: Every so often we need to commit the current state of the
107 * filesystem to disk. The journal thread is responsible for writing
108 * all of the metadata buffers to disk.
110 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
111 * of the data in that part of the log has been rewritten elsewhere on
112 * the disk. Flushing these old buffers to reclaim space in the log is
113 * known as checkpointing, and this thread is responsible for that job.
116 static int kjournald(void *arg
)
118 journal_t
*journal
= arg
;
119 transaction_t
*transaction
;
122 * Set up an interval timer which can be used to trigger a commit wakeup
123 * after the commit interval expires
125 setup_timer(&journal
->j_commit_timer
, commit_timeout
,
126 (unsigned long)current
);
128 /* Record that the journal thread is running */
129 journal
->j_task
= current
;
130 wake_up(&journal
->j_wait_done_commit
);
132 printk(KERN_INFO
"kjournald starting. Commit interval %ld seconds\n",
133 journal
->j_commit_interval
/ HZ
);
136 * And now, wait forever for commit wakeup events.
138 spin_lock(&journal
->j_state_lock
);
141 if (journal
->j_flags
& JFS_UNMOUNT
)
144 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
145 journal
->j_commit_sequence
, journal
->j_commit_request
);
147 if (journal
->j_commit_sequence
!= journal
->j_commit_request
) {
148 jbd_debug(1, "OK, requests differ\n");
149 spin_unlock(&journal
->j_state_lock
);
150 del_timer_sync(&journal
->j_commit_timer
);
151 journal_commit_transaction(journal
);
152 spin_lock(&journal
->j_state_lock
);
156 wake_up(&journal
->j_wait_done_commit
);
157 if (freezing(current
)) {
159 * The simpler the better. Flushing journal isn't a
160 * good idea, because that depends on threads that may
161 * be already stopped.
163 jbd_debug(1, "Now suspending kjournald\n");
164 spin_unlock(&journal
->j_state_lock
);
166 spin_lock(&journal
->j_state_lock
);
169 * We assume on resume that commits are already there,
173 int should_sleep
= 1;
175 prepare_to_wait(&journal
->j_wait_commit
, &wait
,
177 if (journal
->j_commit_sequence
!= journal
->j_commit_request
)
179 transaction
= journal
->j_running_transaction
;
180 if (transaction
&& time_after_eq(jiffies
,
181 transaction
->t_expires
))
183 if (journal
->j_flags
& JFS_UNMOUNT
)
186 spin_unlock(&journal
->j_state_lock
);
188 spin_lock(&journal
->j_state_lock
);
190 finish_wait(&journal
->j_wait_commit
, &wait
);
193 jbd_debug(1, "kjournald wakes\n");
196 * Were we woken up by a commit wakeup event?
198 transaction
= journal
->j_running_transaction
;
199 if (transaction
&& time_after_eq(jiffies
, transaction
->t_expires
)) {
200 journal
->j_commit_request
= transaction
->t_tid
;
201 jbd_debug(1, "woke because of timeout\n");
206 spin_unlock(&journal
->j_state_lock
);
207 del_timer_sync(&journal
->j_commit_timer
);
208 journal
->j_task
= NULL
;
209 wake_up(&journal
->j_wait_done_commit
);
210 jbd_debug(1, "Journal thread exiting.\n");
214 static void journal_start_thread(journal_t
*journal
)
216 kthread_run(kjournald
, journal
, "kjournald");
217 wait_event(journal
->j_wait_done_commit
, journal
->j_task
!= 0);
220 static void journal_kill_thread(journal_t
*journal
)
222 spin_lock(&journal
->j_state_lock
);
223 journal
->j_flags
|= JFS_UNMOUNT
;
225 while (journal
->j_task
) {
226 wake_up(&journal
->j_wait_commit
);
227 spin_unlock(&journal
->j_state_lock
);
228 wait_event(journal
->j_wait_done_commit
, journal
->j_task
== 0);
229 spin_lock(&journal
->j_state_lock
);
231 spin_unlock(&journal
->j_state_lock
);
235 * journal_write_metadata_buffer: write a metadata buffer to the journal.
237 * Writes a metadata buffer to a given disk block. The actual IO is not
238 * performed but a new buffer_head is constructed which labels the data
239 * to be written with the correct destination disk block.
241 * Any magic-number escaping which needs to be done will cause a
242 * copy-out here. If the buffer happens to start with the
243 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
244 * magic number is only written to the log for descripter blocks. In
245 * this case, we copy the data and replace the first word with 0, and we
246 * return a result code which indicates that this buffer needs to be
247 * marked as an escaped buffer in the corresponding log descriptor
248 * block. The missing word can then be restored when the block is read
251 * If the source buffer has already been modified by a new transaction
252 * since we took the last commit snapshot, we use the frozen copy of
253 * that data for IO. If we end up using the existing buffer_head's data
254 * for the write, then we *have* to lock the buffer to prevent anyone
255 * else from using and possibly modifying it while the IO is in
258 * The function returns a pointer to the buffer_heads to be used for IO.
260 * We assume that the journal has already been locked in this function.
267 * Bit 0 set == escape performed on the data
268 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
271 int journal_write_metadata_buffer(transaction_t
*transaction
,
272 struct journal_head
*jh_in
,
273 struct journal_head
**jh_out
,
274 unsigned long blocknr
)
276 int need_copy_out
= 0;
277 int done_copy_out
= 0;
280 struct buffer_head
*new_bh
;
281 struct journal_head
*new_jh
;
282 struct page
*new_page
;
283 unsigned int new_offset
;
284 struct buffer_head
*bh_in
= jh2bh(jh_in
);
287 * The buffer really shouldn't be locked: only the current committing
288 * transaction is allowed to write it, so nobody else is allowed
291 * akpm: except if we're journalling data, and write() output is
292 * also part of a shared mapping, and another thread has
293 * decided to launch a writepage() against this buffer.
295 J_ASSERT_BH(bh_in
, buffer_jbddirty(bh_in
));
297 new_bh
= alloc_buffer_head(GFP_NOFS
|__GFP_NOFAIL
);
300 * If a new transaction has already done a buffer copy-out, then
301 * we use that version of the data for the commit.
303 jbd_lock_bh_state(bh_in
);
305 if (jh_in
->b_frozen_data
) {
307 new_page
= virt_to_page(jh_in
->b_frozen_data
);
308 new_offset
= offset_in_page(jh_in
->b_frozen_data
);
310 new_page
= jh2bh(jh_in
)->b_page
;
311 new_offset
= offset_in_page(jh2bh(jh_in
)->b_data
);
314 mapped_data
= kmap_atomic(new_page
, KM_USER0
);
318 if (*((__be32
*)(mapped_data
+ new_offset
)) ==
319 cpu_to_be32(JFS_MAGIC_NUMBER
)) {
323 kunmap_atomic(mapped_data
, KM_USER0
);
326 * Do we need to do a data copy?
328 if (need_copy_out
&& !done_copy_out
) {
331 jbd_unlock_bh_state(bh_in
);
332 tmp
= jbd_slab_alloc(bh_in
->b_size
, GFP_NOFS
);
333 jbd_lock_bh_state(bh_in
);
334 if (jh_in
->b_frozen_data
) {
335 jbd_slab_free(tmp
, bh_in
->b_size
);
339 jh_in
->b_frozen_data
= tmp
;
340 mapped_data
= kmap_atomic(new_page
, KM_USER0
);
341 memcpy(tmp
, mapped_data
+ new_offset
, jh2bh(jh_in
)->b_size
);
342 kunmap_atomic(mapped_data
, KM_USER0
);
344 new_page
= virt_to_page(tmp
);
345 new_offset
= offset_in_page(tmp
);
350 * Did we need to do an escaping? Now we've done all the
351 * copying, we can finally do so.
354 mapped_data
= kmap_atomic(new_page
, KM_USER0
);
355 *((unsigned int *)(mapped_data
+ new_offset
)) = 0;
356 kunmap_atomic(mapped_data
, KM_USER0
);
359 /* keep subsequent assertions sane */
361 init_buffer(new_bh
, NULL
, NULL
);
362 atomic_set(&new_bh
->b_count
, 1);
363 jbd_unlock_bh_state(bh_in
);
365 new_jh
= journal_add_journal_head(new_bh
); /* This sleeps */
367 set_bh_page(new_bh
, new_page
, new_offset
);
368 new_jh
->b_transaction
= NULL
;
369 new_bh
->b_size
= jh2bh(jh_in
)->b_size
;
370 new_bh
->b_bdev
= transaction
->t_journal
->j_dev
;
371 new_bh
->b_blocknr
= blocknr
;
372 set_buffer_mapped(new_bh
);
373 set_buffer_dirty(new_bh
);
378 * The to-be-written buffer needs to get moved to the io queue,
379 * and the original buffer whose contents we are shadowing or
380 * copying is moved to the transaction's shadow queue.
382 JBUFFER_TRACE(jh_in
, "file as BJ_Shadow");
383 journal_file_buffer(jh_in
, transaction
, BJ_Shadow
);
384 JBUFFER_TRACE(new_jh
, "file as BJ_IO");
385 journal_file_buffer(new_jh
, transaction
, BJ_IO
);
387 return do_escape
| (done_copy_out
<< 1);
391 * Allocation code for the journal file. Manage the space left in the
392 * journal, so that we can begin checkpointing when appropriate.
396 * __log_space_left: Return the number of free blocks left in the journal.
398 * Called with the journal already locked.
400 * Called under j_state_lock
403 int __log_space_left(journal_t
*journal
)
405 int left
= journal
->j_free
;
407 assert_spin_locked(&journal
->j_state_lock
);
410 * Be pessimistic here about the number of those free blocks which
411 * might be required for log descriptor control blocks.
414 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
416 left
-= MIN_LOG_RESERVED_BLOCKS
;
425 * Called under j_state_lock. Returns true if a transaction was started.
427 int __log_start_commit(journal_t
*journal
, tid_t target
)
430 * Are we already doing a recent enough commit?
432 if (!tid_geq(journal
->j_commit_request
, target
)) {
434 * We want a new commit: OK, mark the request and wakup the
435 * commit thread. We do _not_ do the commit ourselves.
438 journal
->j_commit_request
= target
;
439 jbd_debug(1, "JBD: requesting commit %d/%d\n",
440 journal
->j_commit_request
,
441 journal
->j_commit_sequence
);
442 wake_up(&journal
->j_wait_commit
);
448 int log_start_commit(journal_t
*journal
, tid_t tid
)
452 spin_lock(&journal
->j_state_lock
);
453 ret
= __log_start_commit(journal
, tid
);
454 spin_unlock(&journal
->j_state_lock
);
459 * Force and wait upon a commit if the calling process is not within
460 * transaction. This is used for forcing out undo-protected data which contains
461 * bitmaps, when the fs is running out of space.
463 * We can only force the running transaction if we don't have an active handle;
464 * otherwise, we will deadlock.
466 * Returns true if a transaction was started.
468 int journal_force_commit_nested(journal_t
*journal
)
470 transaction_t
*transaction
= NULL
;
473 spin_lock(&journal
->j_state_lock
);
474 if (journal
->j_running_transaction
&& !current
->journal_info
) {
475 transaction
= journal
->j_running_transaction
;
476 __log_start_commit(journal
, transaction
->t_tid
);
477 } else if (journal
->j_committing_transaction
)
478 transaction
= journal
->j_committing_transaction
;
481 spin_unlock(&journal
->j_state_lock
);
482 return 0; /* Nothing to retry */
485 tid
= transaction
->t_tid
;
486 spin_unlock(&journal
->j_state_lock
);
487 log_wait_commit(journal
, tid
);
492 * Start a commit of the current running transaction (if any). Returns true
493 * if a transaction was started, and fills its tid in at *ptid
495 int journal_start_commit(journal_t
*journal
, tid_t
*ptid
)
499 spin_lock(&journal
->j_state_lock
);
500 if (journal
->j_running_transaction
) {
501 tid_t tid
= journal
->j_running_transaction
->t_tid
;
503 ret
= __log_start_commit(journal
, tid
);
506 } else if (journal
->j_committing_transaction
&& ptid
) {
508 * If ext3_write_super() recently started a commit, then we
509 * have to wait for completion of that transaction
511 *ptid
= journal
->j_committing_transaction
->t_tid
;
514 spin_unlock(&journal
->j_state_lock
);
519 * Wait for a specified commit to complete.
520 * The caller may not hold the journal lock.
522 int log_wait_commit(journal_t
*journal
, tid_t tid
)
526 #ifdef CONFIG_JBD_DEBUG
527 spin_lock(&journal
->j_state_lock
);
528 if (!tid_geq(journal
->j_commit_request
, tid
)) {
530 "%s: error: j_commit_request=%d, tid=%d\n",
531 __FUNCTION__
, journal
->j_commit_request
, tid
);
533 spin_unlock(&journal
->j_state_lock
);
535 spin_lock(&journal
->j_state_lock
);
536 while (tid_gt(tid
, journal
->j_commit_sequence
)) {
537 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
538 tid
, journal
->j_commit_sequence
);
539 wake_up(&journal
->j_wait_commit
);
540 spin_unlock(&journal
->j_state_lock
);
541 wait_event(journal
->j_wait_done_commit
,
542 !tid_gt(tid
, journal
->j_commit_sequence
));
543 spin_lock(&journal
->j_state_lock
);
545 spin_unlock(&journal
->j_state_lock
);
547 if (unlikely(is_journal_aborted(journal
))) {
548 printk(KERN_EMERG
"journal commit I/O error\n");
555 * Log buffer allocation routines:
558 int journal_next_log_block(journal_t
*journal
, unsigned long *retp
)
560 unsigned long blocknr
;
562 spin_lock(&journal
->j_state_lock
);
563 J_ASSERT(journal
->j_free
> 1);
565 blocknr
= journal
->j_head
;
568 if (journal
->j_head
== journal
->j_last
)
569 journal
->j_head
= journal
->j_first
;
570 spin_unlock(&journal
->j_state_lock
);
571 return journal_bmap(journal
, blocknr
, retp
);
575 * Conversion of logical to physical block numbers for the journal
577 * On external journals the journal blocks are identity-mapped, so
578 * this is a no-op. If needed, we can use j_blk_offset - everything is
581 int journal_bmap(journal_t
*journal
, unsigned long blocknr
,
587 if (journal
->j_inode
) {
588 ret
= bmap(journal
->j_inode
, blocknr
);
592 char b
[BDEVNAME_SIZE
];
594 printk(KERN_ALERT
"%s: journal block not found "
595 "at offset %lu on %s\n",
598 bdevname(journal
->j_dev
, b
));
600 __journal_abort_soft(journal
, err
);
603 *retp
= blocknr
; /* +journal->j_blk_offset */
609 * We play buffer_head aliasing tricks to write data/metadata blocks to
610 * the journal without copying their contents, but for journal
611 * descriptor blocks we do need to generate bona fide buffers.
613 * After the caller of journal_get_descriptor_buffer() has finished modifying
614 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
615 * But we don't bother doing that, so there will be coherency problems with
616 * mmaps of blockdevs which hold live JBD-controlled filesystems.
618 struct journal_head
*journal_get_descriptor_buffer(journal_t
*journal
)
620 struct buffer_head
*bh
;
621 unsigned long blocknr
;
624 err
= journal_next_log_block(journal
, &blocknr
);
629 bh
= __getblk(journal
->j_dev
, blocknr
, journal
->j_blocksize
);
631 memset(bh
->b_data
, 0, journal
->j_blocksize
);
632 set_buffer_uptodate(bh
);
634 BUFFER_TRACE(bh
, "return this buffer");
635 return journal_add_journal_head(bh
);
639 * Management for journal control blocks: functions to create and
640 * destroy journal_t structures, and to initialise and read existing
641 * journal blocks from disk. */
643 /* First: create and setup a journal_t object in memory. We initialise
644 * very few fields yet: that has to wait until we have created the
645 * journal structures from from scratch, or loaded them from disk. */
647 static journal_t
* journal_init_common (void)
652 journal
= jbd_kmalloc(sizeof(*journal
), GFP_KERNEL
);
655 memset(journal
, 0, sizeof(*journal
));
657 init_waitqueue_head(&journal
->j_wait_transaction_locked
);
658 init_waitqueue_head(&journal
->j_wait_logspace
);
659 init_waitqueue_head(&journal
->j_wait_done_commit
);
660 init_waitqueue_head(&journal
->j_wait_checkpoint
);
661 init_waitqueue_head(&journal
->j_wait_commit
);
662 init_waitqueue_head(&journal
->j_wait_updates
);
663 mutex_init(&journal
->j_barrier
);
664 mutex_init(&journal
->j_checkpoint_mutex
);
665 spin_lock_init(&journal
->j_revoke_lock
);
666 spin_lock_init(&journal
->j_list_lock
);
667 spin_lock_init(&journal
->j_state_lock
);
669 journal
->j_commit_interval
= (HZ
* JBD_DEFAULT_MAX_COMMIT_AGE
);
671 /* The journal is marked for error until we succeed with recovery! */
672 journal
->j_flags
= JFS_ABORT
;
674 /* Set up a default-sized revoke table for the new mount. */
675 err
= journal_init_revoke(journal
, JOURNAL_REVOKE_DEFAULT_HASH
);
685 /* journal_init_dev and journal_init_inode:
687 * Create a journal structure assigned some fixed set of disk blocks to
688 * the journal. We don't actually touch those disk blocks yet, but we
689 * need to set up all of the mapping information to tell the journaling
690 * system where the journal blocks are.
695 * journal_t * journal_init_dev() - creates an initialises a journal structure
696 * @bdev: Block device on which to create the journal
697 * @fs_dev: Device which hold journalled filesystem for this journal.
698 * @start: Block nr Start of journal.
699 * @len: Length of the journal in blocks.
700 * @blocksize: blocksize of journalling device
701 * @returns: a newly created journal_t *
703 * journal_init_dev creates a journal which maps a fixed contiguous
704 * range of blocks on an arbitrary block device.
707 journal_t
* journal_init_dev(struct block_device
*bdev
,
708 struct block_device
*fs_dev
,
709 int start
, int len
, int blocksize
)
711 journal_t
*journal
= journal_init_common();
712 struct buffer_head
*bh
;
718 /* journal descriptor can store up to n blocks -bzzz */
719 journal
->j_blocksize
= blocksize
;
720 n
= journal
->j_blocksize
/ sizeof(journal_block_tag_t
);
721 journal
->j_wbufsize
= n
;
722 journal
->j_wbuf
= kmalloc(n
* sizeof(struct buffer_head
*), GFP_KERNEL
);
723 if (!journal
->j_wbuf
) {
724 printk(KERN_ERR
"%s: Cant allocate bhs for commit thread\n",
730 journal
->j_dev
= bdev
;
731 journal
->j_fs_dev
= fs_dev
;
732 journal
->j_blk_offset
= start
;
733 journal
->j_maxlen
= len
;
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
;
744 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
745 * @inode: An inode to create the journal in
747 * journal_init_inode creates a journal which maps an on-disk inode as
748 * the journal. The inode must exist already, must support bmap() and
749 * must have all data blocks preallocated.
751 journal_t
* journal_init_inode (struct inode
*inode
)
753 struct buffer_head
*bh
;
754 journal_t
*journal
= journal_init_common();
757 unsigned long blocknr
;
762 journal
->j_dev
= journal
->j_fs_dev
= inode
->i_sb
->s_bdev
;
763 journal
->j_inode
= inode
;
765 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
766 journal
, inode
->i_sb
->s_id
, inode
->i_ino
,
767 (long long) inode
->i_size
,
768 inode
->i_sb
->s_blocksize_bits
, inode
->i_sb
->s_blocksize
);
770 journal
->j_maxlen
= inode
->i_size
>> inode
->i_sb
->s_blocksize_bits
;
771 journal
->j_blocksize
= inode
->i_sb
->s_blocksize
;
773 /* journal descriptor can store up to n blocks -bzzz */
774 n
= journal
->j_blocksize
/ sizeof(journal_block_tag_t
);
775 journal
->j_wbufsize
= n
;
776 journal
->j_wbuf
= kmalloc(n
* sizeof(struct buffer_head
*), GFP_KERNEL
);
777 if (!journal
->j_wbuf
) {
778 printk(KERN_ERR
"%s: Cant allocate bhs for commit thread\n",
784 err
= journal_bmap(journal
, 0, &blocknr
);
785 /* If that failed, give up */
787 printk(KERN_ERR
"%s: Cannnot locate journal superblock\n",
793 bh
= __getblk(journal
->j_dev
, blocknr
, journal
->j_blocksize
);
794 J_ASSERT(bh
!= NULL
);
795 journal
->j_sb_buffer
= bh
;
796 journal
->j_superblock
= (journal_superblock_t
*)bh
->b_data
;
802 * If the journal init or create aborts, we need to mark the journal
803 * superblock as being NULL to prevent the journal destroy from writing
804 * back a bogus superblock.
806 static void journal_fail_superblock (journal_t
*journal
)
808 struct buffer_head
*bh
= journal
->j_sb_buffer
;
810 journal
->j_sb_buffer
= NULL
;
814 * Given a journal_t structure, initialise the various fields for
815 * startup of a new journaling session. We use this both when creating
816 * a journal, and after recovering an old journal to reset it for
820 static int journal_reset(journal_t
*journal
)
822 journal_superblock_t
*sb
= journal
->j_superblock
;
823 unsigned long first
, last
;
825 first
= be32_to_cpu(sb
->s_first
);
826 last
= be32_to_cpu(sb
->s_maxlen
);
828 journal
->j_first
= first
;
829 journal
->j_last
= last
;
831 journal
->j_head
= first
;
832 journal
->j_tail
= first
;
833 journal
->j_free
= last
- first
;
835 journal
->j_tail_sequence
= journal
->j_transaction_sequence
;
836 journal
->j_commit_sequence
= journal
->j_transaction_sequence
- 1;
837 journal
->j_commit_request
= journal
->j_commit_sequence
;
839 journal
->j_max_transaction_buffers
= journal
->j_maxlen
/ 4;
841 /* Add the dynamic fields and write it to disk. */
842 journal_update_superblock(journal
, 1);
843 journal_start_thread(journal
);
848 * int journal_create() - Initialise the new journal file
849 * @journal: Journal to create. This structure must have been initialised
851 * Given a journal_t structure which tells us which disk blocks we can
852 * use, create a new journal superblock and initialise all of the
853 * journal fields from scratch.
855 int journal_create(journal_t
*journal
)
857 unsigned long blocknr
;
858 struct buffer_head
*bh
;
859 journal_superblock_t
*sb
;
862 if (journal
->j_maxlen
< JFS_MIN_JOURNAL_BLOCKS
) {
863 printk (KERN_ERR
"Journal length (%d blocks) too short.\n",
865 journal_fail_superblock(journal
);
869 if (journal
->j_inode
== NULL
) {
871 * We don't know what block to start at!
874 "%s: creation of journal on external device!\n",
879 /* Zero out the entire journal on disk. We cannot afford to
880 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
881 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
882 for (i
= 0; i
< journal
->j_maxlen
; i
++) {
883 err
= journal_bmap(journal
, i
, &blocknr
);
886 bh
= __getblk(journal
->j_dev
, blocknr
, journal
->j_blocksize
);
888 memset (bh
->b_data
, 0, journal
->j_blocksize
);
889 BUFFER_TRACE(bh
, "marking dirty");
890 mark_buffer_dirty(bh
);
891 BUFFER_TRACE(bh
, "marking uptodate");
892 set_buffer_uptodate(bh
);
897 sync_blockdev(journal
->j_dev
);
898 jbd_debug(1, "JBD: journal cleared.\n");
900 /* OK, fill in the initial static fields in the new superblock */
901 sb
= journal
->j_superblock
;
903 sb
->s_header
.h_magic
= cpu_to_be32(JFS_MAGIC_NUMBER
);
904 sb
->s_header
.h_blocktype
= cpu_to_be32(JFS_SUPERBLOCK_V2
);
906 sb
->s_blocksize
= cpu_to_be32(journal
->j_blocksize
);
907 sb
->s_maxlen
= cpu_to_be32(journal
->j_maxlen
);
908 sb
->s_first
= cpu_to_be32(1);
910 journal
->j_transaction_sequence
= 1;
912 journal
->j_flags
&= ~JFS_ABORT
;
913 journal
->j_format_version
= 2;
915 return journal_reset(journal
);
919 * void journal_update_superblock() - Update journal sb on disk.
920 * @journal: The journal to update.
921 * @wait: Set to '0' if you don't want to wait for IO completion.
923 * Update a journal's dynamic superblock fields and write it to disk,
924 * optionally waiting for the IO to complete.
926 void journal_update_superblock(journal_t
*journal
, int wait
)
928 journal_superblock_t
*sb
= journal
->j_superblock
;
929 struct buffer_head
*bh
= journal
->j_sb_buffer
;
932 * As a special case, if the on-disk copy is already marked as needing
933 * no recovery (s_start == 0) and there are no outstanding transactions
934 * in the filesystem, then we can safely defer the superblock update
935 * until the next commit by setting JFS_FLUSHED. This avoids
936 * attempting a write to a potential-readonly device.
938 if (sb
->s_start
== 0 && journal
->j_tail_sequence
==
939 journal
->j_transaction_sequence
) {
940 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
941 "(start %ld, seq %d, errno %d)\n",
942 journal
->j_tail
, journal
->j_tail_sequence
,
947 spin_lock(&journal
->j_state_lock
);
948 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
949 journal
->j_tail
, journal
->j_tail_sequence
, journal
->j_errno
);
951 sb
->s_sequence
= cpu_to_be32(journal
->j_tail_sequence
);
952 sb
->s_start
= cpu_to_be32(journal
->j_tail
);
953 sb
->s_errno
= cpu_to_be32(journal
->j_errno
);
954 spin_unlock(&journal
->j_state_lock
);
956 BUFFER_TRACE(bh
, "marking dirty");
957 mark_buffer_dirty(bh
);
959 sync_dirty_buffer(bh
);
961 ll_rw_block(SWRITE
, 1, &bh
);
964 /* If we have just flushed the log (by marking s_start==0), then
965 * any future commit will have to be careful to update the
966 * superblock again to re-record the true start of the log. */
968 spin_lock(&journal
->j_state_lock
);
970 journal
->j_flags
&= ~JFS_FLUSHED
;
972 journal
->j_flags
|= JFS_FLUSHED
;
973 spin_unlock(&journal
->j_state_lock
);
977 * Read the superblock for a given journal, performing initial
978 * validation of the format.
981 static int journal_get_superblock(journal_t
*journal
)
983 struct buffer_head
*bh
;
984 journal_superblock_t
*sb
;
987 bh
= journal
->j_sb_buffer
;
989 J_ASSERT(bh
!= NULL
);
990 if (!buffer_uptodate(bh
)) {
991 ll_rw_block(READ
, 1, &bh
);
993 if (!buffer_uptodate(bh
)) {
995 "JBD: IO error reading journal superblock\n");
1000 sb
= journal
->j_superblock
;
1004 if (sb
->s_header
.h_magic
!= cpu_to_be32(JFS_MAGIC_NUMBER
) ||
1005 sb
->s_blocksize
!= cpu_to_be32(journal
->j_blocksize
)) {
1006 printk(KERN_WARNING
"JBD: no valid journal superblock found\n");
1010 switch(be32_to_cpu(sb
->s_header
.h_blocktype
)) {
1011 case JFS_SUPERBLOCK_V1
:
1012 journal
->j_format_version
= 1;
1014 case JFS_SUPERBLOCK_V2
:
1015 journal
->j_format_version
= 2;
1018 printk(KERN_WARNING
"JBD: unrecognised superblock format ID\n");
1022 if (be32_to_cpu(sb
->s_maxlen
) < journal
->j_maxlen
)
1023 journal
->j_maxlen
= be32_to_cpu(sb
->s_maxlen
);
1024 else if (be32_to_cpu(sb
->s_maxlen
) > journal
->j_maxlen
) {
1025 printk (KERN_WARNING
"JBD: journal file too short\n");
1032 journal_fail_superblock(journal
);
1037 * Load the on-disk journal superblock and read the key fields into the
1041 static int load_superblock(journal_t
*journal
)
1044 journal_superblock_t
*sb
;
1046 err
= journal_get_superblock(journal
);
1050 sb
= journal
->j_superblock
;
1052 journal
->j_tail_sequence
= be32_to_cpu(sb
->s_sequence
);
1053 journal
->j_tail
= be32_to_cpu(sb
->s_start
);
1054 journal
->j_first
= be32_to_cpu(sb
->s_first
);
1055 journal
->j_last
= be32_to_cpu(sb
->s_maxlen
);
1056 journal
->j_errno
= be32_to_cpu(sb
->s_errno
);
1063 * int journal_load() - Read journal from disk.
1064 * @journal: Journal to act on.
1066 * Given a journal_t structure which tells us which disk blocks contain
1067 * a journal, read the journal from disk to initialise the in-memory
1070 int journal_load(journal_t
*journal
)
1073 journal_superblock_t
*sb
;
1075 err
= load_superblock(journal
);
1079 sb
= journal
->j_superblock
;
1080 /* If this is a V2 superblock, then we have to check the
1081 * features flags on it. */
1083 if (journal
->j_format_version
>= 2) {
1084 if ((sb
->s_feature_ro_compat
&
1085 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES
)) ||
1086 (sb
->s_feature_incompat
&
1087 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES
))) {
1088 printk (KERN_WARNING
1089 "JBD: Unrecognised features on journal\n");
1095 * Create a slab for this blocksize
1097 err
= journal_create_jbd_slab(be32_to_cpu(sb
->s_blocksize
));
1101 /* Let the recovery code check whether it needs to recover any
1102 * data from the journal. */
1103 if (journal_recover(journal
))
1104 goto recovery_error
;
1106 /* OK, we've finished with the dynamic journal bits:
1107 * reinitialise the dynamic contents of the superblock in memory
1108 * and reset them on disk. */
1109 if (journal_reset(journal
))
1110 goto recovery_error
;
1112 journal
->j_flags
&= ~JFS_ABORT
;
1113 journal
->j_flags
|= JFS_LOADED
;
1117 printk (KERN_WARNING
"JBD: recovery failed\n");
1122 * void journal_destroy() - Release a journal_t structure.
1123 * @journal: Journal to act on.
1125 * Release a journal_t structure once it is no longer in use by the
1128 void journal_destroy(journal_t
*journal
)
1130 /* Wait for the commit thread to wake up and die. */
1131 journal_kill_thread(journal
);
1133 /* Force a final log commit */
1134 if (journal
->j_running_transaction
)
1135 journal_commit_transaction(journal
);
1137 /* Force any old transactions to disk */
1139 /* Totally anal locking here... */
1140 spin_lock(&journal
->j_list_lock
);
1141 while (journal
->j_checkpoint_transactions
!= NULL
) {
1142 spin_unlock(&journal
->j_list_lock
);
1143 log_do_checkpoint(journal
);
1144 spin_lock(&journal
->j_list_lock
);
1147 J_ASSERT(journal
->j_running_transaction
== NULL
);
1148 J_ASSERT(journal
->j_committing_transaction
== NULL
);
1149 J_ASSERT(journal
->j_checkpoint_transactions
== NULL
);
1150 spin_unlock(&journal
->j_list_lock
);
1152 /* We can now mark the journal as empty. */
1153 journal
->j_tail
= 0;
1154 journal
->j_tail_sequence
= ++journal
->j_transaction_sequence
;
1155 if (journal
->j_sb_buffer
) {
1156 journal_update_superblock(journal
, 1);
1157 brelse(journal
->j_sb_buffer
);
1160 if (journal
->j_inode
)
1161 iput(journal
->j_inode
);
1162 if (journal
->j_revoke
)
1163 journal_destroy_revoke(journal
);
1164 kfree(journal
->j_wbuf
);
1170 *int journal_check_used_features () - Check if features specified are used.
1171 * @journal: Journal to check.
1172 * @compat: bitmask of compatible features
1173 * @ro: bitmask of features that force read-only mount
1174 * @incompat: bitmask of incompatible features
1176 * Check whether the journal uses all of a given set of
1177 * features. Return true (non-zero) if it does.
1180 int journal_check_used_features (journal_t
*journal
, unsigned long compat
,
1181 unsigned long ro
, unsigned long incompat
)
1183 journal_superblock_t
*sb
;
1185 if (!compat
&& !ro
&& !incompat
)
1187 if (journal
->j_format_version
== 1)
1190 sb
= journal
->j_superblock
;
1192 if (((be32_to_cpu(sb
->s_feature_compat
) & compat
) == compat
) &&
1193 ((be32_to_cpu(sb
->s_feature_ro_compat
) & ro
) == ro
) &&
1194 ((be32_to_cpu(sb
->s_feature_incompat
) & incompat
) == incompat
))
1201 * int journal_check_available_features() - Check feature set in journalling layer
1202 * @journal: Journal to check.
1203 * @compat: bitmask of compatible features
1204 * @ro: bitmask of features that force read-only mount
1205 * @incompat: bitmask of incompatible features
1207 * Check whether the journaling code supports the use of
1208 * all of a given set of features on this journal. Return true
1209 * (non-zero) if it can. */
1211 int journal_check_available_features (journal_t
*journal
, unsigned long compat
,
1212 unsigned long ro
, unsigned long incompat
)
1214 journal_superblock_t
*sb
;
1216 if (!compat
&& !ro
&& !incompat
)
1219 sb
= journal
->j_superblock
;
1221 /* We can support any known requested features iff the
1222 * superblock is in version 2. Otherwise we fail to support any
1223 * extended sb features. */
1225 if (journal
->j_format_version
!= 2)
1228 if ((compat
& JFS_KNOWN_COMPAT_FEATURES
) == compat
&&
1229 (ro
& JFS_KNOWN_ROCOMPAT_FEATURES
) == ro
&&
1230 (incompat
& JFS_KNOWN_INCOMPAT_FEATURES
) == incompat
)
1237 * int journal_set_features () - Mark a given journal feature in the superblock
1238 * @journal: Journal to act on.
1239 * @compat: bitmask of compatible features
1240 * @ro: bitmask of features that force read-only mount
1241 * @incompat: bitmask of incompatible features
1243 * Mark a given journal feature as present on the
1244 * superblock. Returns true if the requested features could be set.
1248 int journal_set_features (journal_t
*journal
, unsigned long compat
,
1249 unsigned long ro
, unsigned long incompat
)
1251 journal_superblock_t
*sb
;
1253 if (journal_check_used_features(journal
, compat
, ro
, incompat
))
1256 if (!journal_check_available_features(journal
, compat
, ro
, incompat
))
1259 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1260 compat
, ro
, incompat
);
1262 sb
= journal
->j_superblock
;
1264 sb
->s_feature_compat
|= cpu_to_be32(compat
);
1265 sb
->s_feature_ro_compat
|= cpu_to_be32(ro
);
1266 sb
->s_feature_incompat
|= cpu_to_be32(incompat
);
1273 * int journal_update_format () - Update on-disk journal structure.
1274 * @journal: Journal to act on.
1276 * Given an initialised but unloaded journal struct, poke about in the
1277 * on-disk structure to update it to the most recent supported version.
1279 int journal_update_format (journal_t
*journal
)
1281 journal_superblock_t
*sb
;
1284 err
= journal_get_superblock(journal
);
1288 sb
= journal
->j_superblock
;
1290 switch (be32_to_cpu(sb
->s_header
.h_blocktype
)) {
1291 case JFS_SUPERBLOCK_V2
:
1293 case JFS_SUPERBLOCK_V1
:
1294 return journal_convert_superblock_v1(journal
, sb
);
1301 static int journal_convert_superblock_v1(journal_t
*journal
,
1302 journal_superblock_t
*sb
)
1304 int offset
, blocksize
;
1305 struct buffer_head
*bh
;
1308 "JBD: Converting superblock from version 1 to 2.\n");
1310 /* Pre-initialise new fields to zero */
1311 offset
= ((char *) &(sb
->s_feature_compat
)) - ((char *) sb
);
1312 blocksize
= be32_to_cpu(sb
->s_blocksize
);
1313 memset(&sb
->s_feature_compat
, 0, blocksize
-offset
);
1315 sb
->s_nr_users
= cpu_to_be32(1);
1316 sb
->s_header
.h_blocktype
= cpu_to_be32(JFS_SUPERBLOCK_V2
);
1317 journal
->j_format_version
= 2;
1319 bh
= journal
->j_sb_buffer
;
1320 BUFFER_TRACE(bh
, "marking dirty");
1321 mark_buffer_dirty(bh
);
1322 sync_dirty_buffer(bh
);
1328 * int journal_flush () - Flush journal
1329 * @journal: Journal to act on.
1331 * Flush all data for a given journal to disk and empty the journal.
1332 * Filesystems can use this when remounting readonly to ensure that
1333 * recovery does not need to happen on remount.
1336 int journal_flush(journal_t
*journal
)
1339 transaction_t
*transaction
= NULL
;
1340 unsigned long old_tail
;
1342 spin_lock(&journal
->j_state_lock
);
1344 /* Force everything buffered to the log... */
1345 if (journal
->j_running_transaction
) {
1346 transaction
= journal
->j_running_transaction
;
1347 __log_start_commit(journal
, transaction
->t_tid
);
1348 } else if (journal
->j_committing_transaction
)
1349 transaction
= journal
->j_committing_transaction
;
1351 /* Wait for the log commit to complete... */
1353 tid_t tid
= transaction
->t_tid
;
1355 spin_unlock(&journal
->j_state_lock
);
1356 log_wait_commit(journal
, tid
);
1358 spin_unlock(&journal
->j_state_lock
);
1361 /* ...and flush everything in the log out to disk. */
1362 spin_lock(&journal
->j_list_lock
);
1363 while (!err
&& journal
->j_checkpoint_transactions
!= NULL
) {
1364 spin_unlock(&journal
->j_list_lock
);
1365 err
= log_do_checkpoint(journal
);
1366 spin_lock(&journal
->j_list_lock
);
1368 spin_unlock(&journal
->j_list_lock
);
1369 cleanup_journal_tail(journal
);
1371 /* Finally, mark the journal as really needing no recovery.
1372 * This sets s_start==0 in the underlying superblock, which is
1373 * the magic code for a fully-recovered superblock. Any future
1374 * commits of data to the journal will restore the current
1376 spin_lock(&journal
->j_state_lock
);
1377 old_tail
= journal
->j_tail
;
1378 journal
->j_tail
= 0;
1379 spin_unlock(&journal
->j_state_lock
);
1380 journal_update_superblock(journal
, 1);
1381 spin_lock(&journal
->j_state_lock
);
1382 journal
->j_tail
= old_tail
;
1384 J_ASSERT(!journal
->j_running_transaction
);
1385 J_ASSERT(!journal
->j_committing_transaction
);
1386 J_ASSERT(!journal
->j_checkpoint_transactions
);
1387 J_ASSERT(journal
->j_head
== journal
->j_tail
);
1388 J_ASSERT(journal
->j_tail_sequence
== journal
->j_transaction_sequence
);
1389 spin_unlock(&journal
->j_state_lock
);
1394 * int journal_wipe() - Wipe journal contents
1395 * @journal: Journal to act on.
1396 * @write: flag (see below)
1398 * Wipe out all of the contents of a journal, safely. This will produce
1399 * a warning if the journal contains any valid recovery information.
1400 * Must be called between journal_init_*() and journal_load().
1402 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1403 * we merely suppress recovery.
1406 int journal_wipe(journal_t
*journal
, int write
)
1408 journal_superblock_t
*sb
;
1411 J_ASSERT (!(journal
->j_flags
& JFS_LOADED
));
1413 err
= load_superblock(journal
);
1417 sb
= journal
->j_superblock
;
1419 if (!journal
->j_tail
)
1422 printk (KERN_WARNING
"JBD: %s recovery information on journal\n",
1423 write
? "Clearing" : "Ignoring");
1425 err
= journal_skip_recovery(journal
);
1427 journal_update_superblock(journal
, 1);
1434 * journal_dev_name: format a character string to describe on what
1435 * device this journal is present.
1438 static const char *journal_dev_name(journal_t
*journal
, char *buffer
)
1440 struct block_device
*bdev
;
1442 if (journal
->j_inode
)
1443 bdev
= journal
->j_inode
->i_sb
->s_bdev
;
1445 bdev
= journal
->j_dev
;
1447 return bdevname(bdev
, buffer
);
1451 * Journal abort has very specific semantics, which we describe
1452 * for journal abort.
1454 * Two internal function, which provide abort to te jbd layer
1459 * Quick version for internal journal use (doesn't lock the journal).
1460 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1461 * and don't attempt to make any other journal updates.
1463 void __journal_abort_hard(journal_t
*journal
)
1465 transaction_t
*transaction
;
1466 char b
[BDEVNAME_SIZE
];
1468 if (journal
->j_flags
& JFS_ABORT
)
1471 printk(KERN_ERR
"Aborting journal on device %s.\n",
1472 journal_dev_name(journal
, b
));
1474 spin_lock(&journal
->j_state_lock
);
1475 journal
->j_flags
|= JFS_ABORT
;
1476 transaction
= journal
->j_running_transaction
;
1478 __log_start_commit(journal
, transaction
->t_tid
);
1479 spin_unlock(&journal
->j_state_lock
);
1482 /* Soft abort: record the abort error status in the journal superblock,
1483 * but don't do any other IO. */
1484 static void __journal_abort_soft (journal_t
*journal
, int errno
)
1486 if (journal
->j_flags
& JFS_ABORT
)
1489 if (!journal
->j_errno
)
1490 journal
->j_errno
= errno
;
1492 __journal_abort_hard(journal
);
1495 journal_update_superblock(journal
, 1);
1499 * void journal_abort () - Shutdown the journal immediately.
1500 * @journal: the journal to shutdown.
1501 * @errno: an error number to record in the journal indicating
1502 * the reason for the shutdown.
1504 * Perform a complete, immediate shutdown of the ENTIRE
1505 * journal (not of a single transaction). This operation cannot be
1506 * undone without closing and reopening the journal.
1508 * The journal_abort function is intended to support higher level error
1509 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1512 * Journal abort has very specific semantics. Any existing dirty,
1513 * unjournaled buffers in the main filesystem will still be written to
1514 * disk by bdflush, but the journaling mechanism will be suspended
1515 * immediately and no further transaction commits will be honoured.
1517 * Any dirty, journaled buffers will be written back to disk without
1518 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1519 * filesystem, but we _do_ attempt to leave as much data as possible
1520 * behind for fsck to use for cleanup.
1522 * Any attempt to get a new transaction handle on a journal which is in
1523 * ABORT state will just result in an -EROFS error return. A
1524 * journal_stop on an existing handle will return -EIO if we have
1525 * entered abort state during the update.
1527 * Recursive transactions are not disturbed by journal abort until the
1528 * final journal_stop, which will receive the -EIO error.
1530 * Finally, the journal_abort call allows the caller to supply an errno
1531 * which will be recorded (if possible) in the journal superblock. This
1532 * allows a client to record failure conditions in the middle of a
1533 * transaction without having to complete the transaction to record the
1534 * failure to disk. ext3_error, for example, now uses this
1537 * Errors which originate from within the journaling layer will NOT
1538 * supply an errno; a null errno implies that absolutely no further
1539 * writes are done to the journal (unless there are any already in
1544 void journal_abort(journal_t
*journal
, int errno
)
1546 __journal_abort_soft(journal
, errno
);
1550 * int journal_errno () - returns the journal's error state.
1551 * @journal: journal to examine.
1553 * This is the errno numbet set with journal_abort(), the last
1554 * time the journal was mounted - if the journal was stopped
1555 * without calling abort this will be 0.
1557 * If the journal has been aborted on this mount time -EROFS will
1560 int journal_errno(journal_t
*journal
)
1564 spin_lock(&journal
->j_state_lock
);
1565 if (journal
->j_flags
& JFS_ABORT
)
1568 err
= journal
->j_errno
;
1569 spin_unlock(&journal
->j_state_lock
);
1574 * int journal_clear_err () - clears the journal's error state
1575 * @journal: journal to act on.
1577 * An error must be cleared or Acked to take a FS out of readonly
1580 int journal_clear_err(journal_t
*journal
)
1584 spin_lock(&journal
->j_state_lock
);
1585 if (journal
->j_flags
& JFS_ABORT
)
1588 journal
->j_errno
= 0;
1589 spin_unlock(&journal
->j_state_lock
);
1594 * void journal_ack_err() - Ack journal err.
1595 * @journal: journal to act on.
1597 * An error must be cleared or Acked to take a FS out of readonly
1600 void journal_ack_err(journal_t
*journal
)
1602 spin_lock(&journal
->j_state_lock
);
1603 if (journal
->j_errno
)
1604 journal
->j_flags
|= JFS_ACK_ERR
;
1605 spin_unlock(&journal
->j_state_lock
);
1608 int journal_blocks_per_page(struct inode
*inode
)
1610 return 1 << (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
1614 * Simple support for retrying memory allocations. Introduced to help to
1615 * debug different VM deadlock avoidance strategies.
1617 void * __jbd_kmalloc (const char *where
, size_t size
, gfp_t flags
, int retry
)
1619 return kmalloc(size
, flags
| (retry
? __GFP_NOFAIL
: 0));
1623 * jbd slab management: create 1k, 2k, 4k, 8k slabs as needed
1624 * and allocate frozen and commit buffers from these slabs.
1626 * Reason for doing this is to avoid, SLAB_DEBUG - since it could
1627 * cause bh to cross page boundary.
1630 #define JBD_MAX_SLABS 5
1631 #define JBD_SLAB_INDEX(size) (size >> 11)
1633 static struct kmem_cache
*jbd_slab
[JBD_MAX_SLABS
];
1634 static const char *jbd_slab_names
[JBD_MAX_SLABS
] = {
1635 "jbd_1k", "jbd_2k", "jbd_4k", NULL
, "jbd_8k"
1638 static void journal_destroy_jbd_slabs(void)
1642 for (i
= 0; i
< JBD_MAX_SLABS
; i
++) {
1644 kmem_cache_destroy(jbd_slab
[i
]);
1649 static int journal_create_jbd_slab(size_t slab_size
)
1651 int i
= JBD_SLAB_INDEX(slab_size
);
1653 BUG_ON(i
>= JBD_MAX_SLABS
);
1656 * Check if we already have a slab created for this size
1662 * Create a slab and force alignment to be same as slabsize -
1663 * this will make sure that allocations won't cross the page
1666 jbd_slab
[i
] = kmem_cache_create(jbd_slab_names
[i
],
1667 slab_size
, slab_size
, 0, NULL
, NULL
);
1669 printk(KERN_EMERG
"JBD: no memory for jbd_slab cache\n");
1675 void * jbd_slab_alloc(size_t size
, gfp_t flags
)
1679 idx
= JBD_SLAB_INDEX(size
);
1680 BUG_ON(jbd_slab
[idx
] == NULL
);
1681 return kmem_cache_alloc(jbd_slab
[idx
], flags
| __GFP_NOFAIL
);
1684 void jbd_slab_free(void *ptr
, size_t size
)
1688 idx
= JBD_SLAB_INDEX(size
);
1689 BUG_ON(jbd_slab
[idx
] == NULL
);
1690 kmem_cache_free(jbd_slab
[idx
], ptr
);
1694 * Journal_head storage management
1696 static struct kmem_cache
*journal_head_cache
;
1697 #ifdef CONFIG_JBD_DEBUG
1698 static atomic_t nr_journal_heads
= ATOMIC_INIT(0);
1701 static int journal_init_journal_head_cache(void)
1705 J_ASSERT(journal_head_cache
== 0);
1706 journal_head_cache
= kmem_cache_create("journal_head",
1707 sizeof(struct journal_head
),
1713 if (journal_head_cache
== 0) {
1715 printk(KERN_EMERG
"JBD: no memory for journal_head cache\n");
1720 static void journal_destroy_journal_head_cache(void)
1722 J_ASSERT(journal_head_cache
!= NULL
);
1723 kmem_cache_destroy(journal_head_cache
);
1724 journal_head_cache
= NULL
;
1728 * journal_head splicing and dicing
1730 static struct journal_head
*journal_alloc_journal_head(void)
1732 struct journal_head
*ret
;
1733 static unsigned long last_warning
;
1735 #ifdef CONFIG_JBD_DEBUG
1736 atomic_inc(&nr_journal_heads
);
1738 ret
= kmem_cache_alloc(journal_head_cache
, GFP_NOFS
);
1740 jbd_debug(1, "out of memory for journal_head\n");
1741 if (time_after(jiffies
, last_warning
+ 5*HZ
)) {
1742 printk(KERN_NOTICE
"ENOMEM in %s, retrying.\n",
1744 last_warning
= jiffies
;
1748 ret
= kmem_cache_alloc(journal_head_cache
, GFP_NOFS
);
1754 static void journal_free_journal_head(struct journal_head
*jh
)
1756 #ifdef CONFIG_JBD_DEBUG
1757 atomic_dec(&nr_journal_heads
);
1758 memset(jh
, JBD_POISON_FREE
, sizeof(*jh
));
1760 kmem_cache_free(journal_head_cache
, jh
);
1764 * A journal_head is attached to a buffer_head whenever JBD has an
1765 * interest in the buffer.
1767 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1768 * is set. This bit is tested in core kernel code where we need to take
1769 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1772 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1774 * When a buffer has its BH_JBD bit set it is immune from being released by
1775 * core kernel code, mainly via ->b_count.
1777 * A journal_head may be detached from its buffer_head when the journal_head's
1778 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1779 * Various places in JBD call journal_remove_journal_head() to indicate that the
1780 * journal_head can be dropped if needed.
1782 * Various places in the kernel want to attach a journal_head to a buffer_head
1783 * _before_ attaching the journal_head to a transaction. To protect the
1784 * journal_head in this situation, journal_add_journal_head elevates the
1785 * journal_head's b_jcount refcount by one. The caller must call
1786 * journal_put_journal_head() to undo this.
1788 * So the typical usage would be:
1790 * (Attach a journal_head if needed. Increments b_jcount)
1791 * struct journal_head *jh = journal_add_journal_head(bh);
1793 * jh->b_transaction = xxx;
1794 * journal_put_journal_head(jh);
1796 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1797 * because it has a non-zero b_transaction.
1801 * Give a buffer_head a journal_head.
1803 * Doesn't need the journal lock.
1806 struct journal_head
*journal_add_journal_head(struct buffer_head
*bh
)
1808 struct journal_head
*jh
;
1809 struct journal_head
*new_jh
= NULL
;
1812 if (!buffer_jbd(bh
)) {
1813 new_jh
= journal_alloc_journal_head();
1814 memset(new_jh
, 0, sizeof(*new_jh
));
1817 jbd_lock_bh_journal_head(bh
);
1818 if (buffer_jbd(bh
)) {
1822 (atomic_read(&bh
->b_count
) > 0) ||
1823 (bh
->b_page
&& bh
->b_page
->mapping
));
1826 jbd_unlock_bh_journal_head(bh
);
1831 new_jh
= NULL
; /* We consumed it */
1836 BUFFER_TRACE(bh
, "added journal_head");
1839 jbd_unlock_bh_journal_head(bh
);
1841 journal_free_journal_head(new_jh
);
1842 return bh
->b_private
;
1846 * Grab a ref against this buffer_head's journal_head. If it ended up not
1847 * having a journal_head, return NULL
1849 struct journal_head
*journal_grab_journal_head(struct buffer_head
*bh
)
1851 struct journal_head
*jh
= NULL
;
1853 jbd_lock_bh_journal_head(bh
);
1854 if (buffer_jbd(bh
)) {
1858 jbd_unlock_bh_journal_head(bh
);
1862 static void __journal_remove_journal_head(struct buffer_head
*bh
)
1864 struct journal_head
*jh
= bh2jh(bh
);
1866 J_ASSERT_JH(jh
, jh
->b_jcount
>= 0);
1869 if (jh
->b_jcount
== 0) {
1870 if (jh
->b_transaction
== NULL
&&
1871 jh
->b_next_transaction
== NULL
&&
1872 jh
->b_cp_transaction
== NULL
) {
1873 J_ASSERT_JH(jh
, jh
->b_jlist
== BJ_None
);
1874 J_ASSERT_BH(bh
, buffer_jbd(bh
));
1875 J_ASSERT_BH(bh
, jh2bh(jh
) == bh
);
1876 BUFFER_TRACE(bh
, "remove journal_head");
1877 if (jh
->b_frozen_data
) {
1878 printk(KERN_WARNING
"%s: freeing "
1881 jbd_slab_free(jh
->b_frozen_data
, bh
->b_size
);
1883 if (jh
->b_committed_data
) {
1884 printk(KERN_WARNING
"%s: freeing "
1885 "b_committed_data\n",
1887 jbd_slab_free(jh
->b_committed_data
, bh
->b_size
);
1889 bh
->b_private
= NULL
;
1890 jh
->b_bh
= NULL
; /* debug, really */
1891 clear_buffer_jbd(bh
);
1893 journal_free_journal_head(jh
);
1895 BUFFER_TRACE(bh
, "journal_head was locked");
1901 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1902 * and has a zero b_jcount then remove and release its journal_head. If we did
1903 * see that the buffer is not used by any transaction we also "logically"
1904 * decrement ->b_count.
1906 * We in fact take an additional increment on ->b_count as a convenience,
1907 * because the caller usually wants to do additional things with the bh
1908 * after calling here.
1909 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1910 * time. Once the caller has run __brelse(), the buffer is eligible for
1911 * reaping by try_to_free_buffers().
1913 void journal_remove_journal_head(struct buffer_head
*bh
)
1915 jbd_lock_bh_journal_head(bh
);
1916 __journal_remove_journal_head(bh
);
1917 jbd_unlock_bh_journal_head(bh
);
1921 * Drop a reference on the passed journal_head. If it fell to zero then try to
1922 * release the journal_head from the buffer_head.
1924 void journal_put_journal_head(struct journal_head
*jh
)
1926 struct buffer_head
*bh
= jh2bh(jh
);
1928 jbd_lock_bh_journal_head(bh
);
1929 J_ASSERT_JH(jh
, jh
->b_jcount
> 0);
1931 if (!jh
->b_jcount
&& !jh
->b_transaction
) {
1932 __journal_remove_journal_head(bh
);
1935 jbd_unlock_bh_journal_head(bh
);
1941 #if defined(CONFIG_JBD_DEBUG)
1942 int journal_enable_debug
;
1943 EXPORT_SYMBOL(journal_enable_debug
);
1946 #if defined(CONFIG_JBD_DEBUG) && defined(CONFIG_PROC_FS)
1948 static struct proc_dir_entry
*proc_jbd_debug
;
1950 static int read_jbd_debug(char *page
, char **start
, off_t off
,
1951 int count
, int *eof
, void *data
)
1955 ret
= sprintf(page
+ off
, "%d\n", journal_enable_debug
);
1960 static int write_jbd_debug(struct file
*file
, const char __user
*buffer
,
1961 unsigned long count
, void *data
)
1965 if (count
> ARRAY_SIZE(buf
) - 1)
1966 count
= ARRAY_SIZE(buf
) - 1;
1967 if (copy_from_user(buf
, buffer
, count
))
1969 buf
[ARRAY_SIZE(buf
) - 1] = '\0';
1970 journal_enable_debug
= simple_strtoul(buf
, NULL
, 10);
1974 #define JBD_PROC_NAME "sys/fs/jbd-debug"
1976 static void __init
create_jbd_proc_entry(void)
1978 proc_jbd_debug
= create_proc_entry(JBD_PROC_NAME
, 0644, NULL
);
1979 if (proc_jbd_debug
) {
1980 /* Why is this so hard? */
1981 proc_jbd_debug
->read_proc
= read_jbd_debug
;
1982 proc_jbd_debug
->write_proc
= write_jbd_debug
;
1986 static void __exit
remove_jbd_proc_entry(void)
1989 remove_proc_entry(JBD_PROC_NAME
, NULL
);
1994 #define create_jbd_proc_entry() do {} while (0)
1995 #define remove_jbd_proc_entry() do {} while (0)
1999 struct kmem_cache
*jbd_handle_cache
;
2001 static int __init
journal_init_handle_cache(void)
2003 jbd_handle_cache
= kmem_cache_create("journal_handle",
2009 if (jbd_handle_cache
== NULL
) {
2010 printk(KERN_EMERG
"JBD: failed to create handle cache\n");
2016 static void journal_destroy_handle_cache(void)
2018 if (jbd_handle_cache
)
2019 kmem_cache_destroy(jbd_handle_cache
);
2023 * Module startup and shutdown
2026 static int __init
journal_init_caches(void)
2030 ret
= journal_init_revoke_caches();
2032 ret
= journal_init_journal_head_cache();
2034 ret
= journal_init_handle_cache();
2038 static void journal_destroy_caches(void)
2040 journal_destroy_revoke_caches();
2041 journal_destroy_journal_head_cache();
2042 journal_destroy_handle_cache();
2043 journal_destroy_jbd_slabs();
2046 static int __init
journal_init(void)
2050 BUILD_BUG_ON(sizeof(struct journal_superblock_s
) != 1024);
2052 ret
= journal_init_caches();
2054 journal_destroy_caches();
2055 create_jbd_proc_entry();
2059 static void __exit
journal_exit(void)
2061 #ifdef CONFIG_JBD_DEBUG
2062 int n
= atomic_read(&nr_journal_heads
);
2064 printk(KERN_EMERG
"JBD: leaked %d journal_heads!\n", n
);
2066 remove_jbd_proc_entry();
2067 journal_destroy_caches();
2070 MODULE_LICENSE("GPL");
2071 module_init(journal_init
);
2072 module_exit(journal_exit
);