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/init.h>
33 #include <linux/freezer.h>
34 #include <linux/pagemap.h>
35 #include <linux/kthread.h>
36 #include <linux/poison.h>
37 #include <linux/proc_fs.h>
38 #include <linux/debugfs.h>
39 #include <linux/ratelimit.h>
41 #include <asm/uaccess.h>
44 EXPORT_SYMBOL(journal_start
);
45 EXPORT_SYMBOL(journal_restart
);
46 EXPORT_SYMBOL(journal_extend
);
47 EXPORT_SYMBOL(journal_stop
);
48 EXPORT_SYMBOL(journal_lock_updates
);
49 EXPORT_SYMBOL(journal_unlock_updates
);
50 EXPORT_SYMBOL(journal_get_write_access
);
51 EXPORT_SYMBOL(journal_get_create_access
);
52 EXPORT_SYMBOL(journal_get_undo_access
);
53 EXPORT_SYMBOL(journal_dirty_data
);
54 EXPORT_SYMBOL(journal_dirty_metadata
);
55 EXPORT_SYMBOL(journal_release_buffer
);
56 EXPORT_SYMBOL(journal_forget
);
58 EXPORT_SYMBOL(journal_sync_buffer
);
60 EXPORT_SYMBOL(journal_flush
);
61 EXPORT_SYMBOL(journal_revoke
);
63 EXPORT_SYMBOL(journal_init_dev
);
64 EXPORT_SYMBOL(journal_init_inode
);
65 EXPORT_SYMBOL(journal_update_format
);
66 EXPORT_SYMBOL(journal_check_used_features
);
67 EXPORT_SYMBOL(journal_check_available_features
);
68 EXPORT_SYMBOL(journal_set_features
);
69 EXPORT_SYMBOL(journal_create
);
70 EXPORT_SYMBOL(journal_load
);
71 EXPORT_SYMBOL(journal_destroy
);
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(log_start_commit
);
78 EXPORT_SYMBOL(journal_start_commit
);
79 EXPORT_SYMBOL(journal_force_commit_nested
);
80 EXPORT_SYMBOL(journal_wipe
);
81 EXPORT_SYMBOL(journal_blocks_per_page
);
82 EXPORT_SYMBOL(journal_invalidatepage
);
83 EXPORT_SYMBOL(journal_try_to_free_buffers
);
84 EXPORT_SYMBOL(journal_force_commit
);
86 static int journal_convert_superblock_v1(journal_t
*, journal_superblock_t
*);
87 static void __journal_abort_soft (journal_t
*journal
, int errno
);
88 static const char *journal_dev_name(journal_t
*journal
, char *buffer
);
91 * Helper function used to manage commit timeouts
94 static void commit_timeout(unsigned long __data
)
96 struct task_struct
* p
= (struct task_struct
*) __data
;
102 * kjournald: The main thread function used to manage a logging device
105 * This kernel thread is responsible for two things:
107 * 1) COMMIT: Every so often we need to commit the current state of the
108 * filesystem to disk. The journal thread is responsible for writing
109 * all of the metadata buffers to disk.
111 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
112 * of the data in that part of the log has been rewritten elsewhere on
113 * the disk. Flushing these old buffers to reclaim space in the log is
114 * known as checkpointing, and this thread is responsible for that job.
117 static int kjournald(void *arg
)
119 journal_t
*journal
= arg
;
120 transaction_t
*transaction
;
123 * Set up an interval timer which can be used to trigger a commit wakeup
124 * after the commit interval expires
126 setup_timer(&journal
->j_commit_timer
, commit_timeout
,
127 (unsigned long)current
);
129 /* Record that the journal thread is running */
130 journal
->j_task
= current
;
131 wake_up(&journal
->j_wait_done_commit
);
133 printk(KERN_INFO
"kjournald starting. Commit interval %ld seconds\n",
134 journal
->j_commit_interval
/ HZ
);
137 * And now, wait forever for commit wakeup events.
139 spin_lock(&journal
->j_state_lock
);
142 if (journal
->j_flags
& JFS_UNMOUNT
)
145 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
146 journal
->j_commit_sequence
, journal
->j_commit_request
);
148 if (journal
->j_commit_sequence
!= journal
->j_commit_request
) {
149 jbd_debug(1, "OK, requests differ\n");
150 spin_unlock(&journal
->j_state_lock
);
151 del_timer_sync(&journal
->j_commit_timer
);
152 journal_commit_transaction(journal
);
153 spin_lock(&journal
->j_state_lock
);
157 wake_up(&journal
->j_wait_done_commit
);
158 if (freezing(current
)) {
160 * The simpler the better. Flushing journal isn't a
161 * good idea, because that depends on threads that may
162 * be already stopped.
164 jbd_debug(1, "Now suspending kjournald\n");
165 spin_unlock(&journal
->j_state_lock
);
167 spin_lock(&journal
->j_state_lock
);
170 * We assume on resume that commits are already there,
174 int should_sleep
= 1;
176 prepare_to_wait(&journal
->j_wait_commit
, &wait
,
178 if (journal
->j_commit_sequence
!= journal
->j_commit_request
)
180 transaction
= journal
->j_running_transaction
;
181 if (transaction
&& time_after_eq(jiffies
,
182 transaction
->t_expires
))
184 if (journal
->j_flags
& JFS_UNMOUNT
)
187 spin_unlock(&journal
->j_state_lock
);
189 spin_lock(&journal
->j_state_lock
);
191 finish_wait(&journal
->j_wait_commit
, &wait
);
194 jbd_debug(1, "kjournald wakes\n");
197 * Were we woken up by a commit wakeup event?
199 transaction
= journal
->j_running_transaction
;
200 if (transaction
&& time_after_eq(jiffies
, transaction
->t_expires
)) {
201 journal
->j_commit_request
= transaction
->t_tid
;
202 jbd_debug(1, "woke because of timeout\n");
207 spin_unlock(&journal
->j_state_lock
);
208 del_timer_sync(&journal
->j_commit_timer
);
209 journal
->j_task
= NULL
;
210 wake_up(&journal
->j_wait_done_commit
);
211 jbd_debug(1, "Journal thread exiting.\n");
215 static int journal_start_thread(journal_t
*journal
)
217 struct task_struct
*t
;
219 t
= kthread_run(kjournald
, journal
, "kjournald");
223 wait_event(journal
->j_wait_done_commit
, journal
->j_task
!= NULL
);
227 static void journal_kill_thread(journal_t
*journal
)
229 spin_lock(&journal
->j_state_lock
);
230 journal
->j_flags
|= JFS_UNMOUNT
;
232 while (journal
->j_task
) {
233 wake_up(&journal
->j_wait_commit
);
234 spin_unlock(&journal
->j_state_lock
);
235 wait_event(journal
->j_wait_done_commit
,
236 journal
->j_task
== NULL
);
237 spin_lock(&journal
->j_state_lock
);
239 spin_unlock(&journal
->j_state_lock
);
243 * journal_write_metadata_buffer: write a metadata buffer to the journal.
245 * Writes a metadata buffer to a given disk block. The actual IO is not
246 * performed but a new buffer_head is constructed which labels the data
247 * to be written with the correct destination disk block.
249 * Any magic-number escaping which needs to be done will cause a
250 * copy-out here. If the buffer happens to start with the
251 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
252 * magic number is only written to the log for descripter blocks. In
253 * this case, we copy the data and replace the first word with 0, and we
254 * return a result code which indicates that this buffer needs to be
255 * marked as an escaped buffer in the corresponding log descriptor
256 * block. The missing word can then be restored when the block is read
259 * If the source buffer has already been modified by a new transaction
260 * since we took the last commit snapshot, we use the frozen copy of
261 * that data for IO. If we end up using the existing buffer_head's data
262 * for the write, then we *have* to lock the buffer to prevent anyone
263 * else from using and possibly modifying it while the IO is in
266 * The function returns a pointer to the buffer_heads to be used for IO.
268 * We assume that the journal has already been locked in this function.
275 * Bit 0 set == escape performed on the data
276 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
279 int journal_write_metadata_buffer(transaction_t
*transaction
,
280 struct journal_head
*jh_in
,
281 struct journal_head
**jh_out
,
282 unsigned int blocknr
)
284 int need_copy_out
= 0;
285 int done_copy_out
= 0;
288 struct buffer_head
*new_bh
;
289 struct journal_head
*new_jh
;
290 struct page
*new_page
;
291 unsigned int new_offset
;
292 struct buffer_head
*bh_in
= jh2bh(jh_in
);
293 journal_t
*journal
= transaction
->t_journal
;
296 * The buffer really shouldn't be locked: only the current committing
297 * transaction is allowed to write it, so nobody else is allowed
300 * akpm: except if we're journalling data, and write() output is
301 * also part of a shared mapping, and another thread has
302 * decided to launch a writepage() against this buffer.
304 J_ASSERT_BH(bh_in
, buffer_jbddirty(bh_in
));
306 new_bh
= alloc_buffer_head(GFP_NOFS
|__GFP_NOFAIL
);
307 /* keep subsequent assertions sane */
309 init_buffer(new_bh
, NULL
, NULL
);
310 atomic_set(&new_bh
->b_count
, 1);
311 new_jh
= journal_add_journal_head(new_bh
); /* This sleeps */
314 * If a new transaction has already done a buffer copy-out, then
315 * we use that version of the data for the commit.
317 jbd_lock_bh_state(bh_in
);
319 if (jh_in
->b_frozen_data
) {
321 new_page
= virt_to_page(jh_in
->b_frozen_data
);
322 new_offset
= offset_in_page(jh_in
->b_frozen_data
);
324 new_page
= jh2bh(jh_in
)->b_page
;
325 new_offset
= offset_in_page(jh2bh(jh_in
)->b_data
);
328 mapped_data
= kmap_atomic(new_page
, KM_USER0
);
332 if (*((__be32
*)(mapped_data
+ new_offset
)) ==
333 cpu_to_be32(JFS_MAGIC_NUMBER
)) {
337 kunmap_atomic(mapped_data
, KM_USER0
);
340 * Do we need to do a data copy?
342 if (need_copy_out
&& !done_copy_out
) {
345 jbd_unlock_bh_state(bh_in
);
346 tmp
= jbd_alloc(bh_in
->b_size
, GFP_NOFS
);
347 jbd_lock_bh_state(bh_in
);
348 if (jh_in
->b_frozen_data
) {
349 jbd_free(tmp
, bh_in
->b_size
);
353 jh_in
->b_frozen_data
= tmp
;
354 mapped_data
= kmap_atomic(new_page
, KM_USER0
);
355 memcpy(tmp
, mapped_data
+ new_offset
, jh2bh(jh_in
)->b_size
);
356 kunmap_atomic(mapped_data
, KM_USER0
);
358 new_page
= virt_to_page(tmp
);
359 new_offset
= offset_in_page(tmp
);
364 * Did we need to do an escaping? Now we've done all the
365 * copying, we can finally do so.
368 mapped_data
= kmap_atomic(new_page
, KM_USER0
);
369 *((unsigned int *)(mapped_data
+ new_offset
)) = 0;
370 kunmap_atomic(mapped_data
, KM_USER0
);
373 set_bh_page(new_bh
, new_page
, new_offset
);
374 new_jh
->b_transaction
= NULL
;
375 new_bh
->b_size
= jh2bh(jh_in
)->b_size
;
376 new_bh
->b_bdev
= transaction
->t_journal
->j_dev
;
377 new_bh
->b_blocknr
= blocknr
;
378 set_buffer_mapped(new_bh
);
379 set_buffer_dirty(new_bh
);
384 * The to-be-written buffer needs to get moved to the io queue,
385 * and the original buffer whose contents we are shadowing or
386 * copying is moved to the transaction's shadow queue.
388 JBUFFER_TRACE(jh_in
, "file as BJ_Shadow");
389 spin_lock(&journal
->j_list_lock
);
390 __journal_file_buffer(jh_in
, transaction
, BJ_Shadow
);
391 spin_unlock(&journal
->j_list_lock
);
392 jbd_unlock_bh_state(bh_in
);
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 commit 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 wakeup 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 is going to be committed (or is currently already
504 * committing), and fills its tid in at *ptid
506 int journal_start_commit(journal_t
*journal
, tid_t
*ptid
)
510 spin_lock(&journal
->j_state_lock
);
511 if (journal
->j_running_transaction
) {
512 tid_t tid
= journal
->j_running_transaction
->t_tid
;
514 __log_start_commit(journal
, tid
);
515 /* There's a running transaction and we've just made sure
516 * it's commit has been scheduled. */
520 } else if (journal
->j_committing_transaction
) {
522 * If ext3_write_super() recently started a commit, then we
523 * have to wait for completion of that transaction
526 *ptid
= journal
->j_committing_transaction
->t_tid
;
529 spin_unlock(&journal
->j_state_lock
);
534 * Wait for a specified commit to complete.
535 * The caller may not hold the journal lock.
537 int log_wait_commit(journal_t
*journal
, tid_t tid
)
541 #ifdef CONFIG_JBD_DEBUG
542 spin_lock(&journal
->j_state_lock
);
543 if (!tid_geq(journal
->j_commit_request
, tid
)) {
545 "%s: error: j_commit_request=%d, tid=%d\n",
546 __func__
, journal
->j_commit_request
, tid
);
548 spin_unlock(&journal
->j_state_lock
);
550 spin_lock(&journal
->j_state_lock
);
551 while (tid_gt(tid
, journal
->j_commit_sequence
)) {
552 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
553 tid
, journal
->j_commit_sequence
);
554 wake_up(&journal
->j_wait_commit
);
555 spin_unlock(&journal
->j_state_lock
);
556 wait_event(journal
->j_wait_done_commit
,
557 !tid_gt(tid
, journal
->j_commit_sequence
));
558 spin_lock(&journal
->j_state_lock
);
560 spin_unlock(&journal
->j_state_lock
);
562 if (unlikely(is_journal_aborted(journal
))) {
563 printk(KERN_EMERG
"journal commit I/O error\n");
570 * Return 1 if a given transaction has not yet sent barrier request
571 * connected with a transaction commit. If 0 is returned, transaction
572 * may or may not have sent the barrier. Used to avoid sending barrier
573 * twice in common cases.
575 int journal_trans_will_send_data_barrier(journal_t
*journal
, tid_t tid
)
578 transaction_t
*commit_trans
;
580 if (!(journal
->j_flags
& JFS_BARRIER
))
582 spin_lock(&journal
->j_state_lock
);
583 /* Transaction already committed? */
584 if (tid_geq(journal
->j_commit_sequence
, tid
))
587 * Transaction is being committed and we already proceeded to
588 * writing commit record?
590 commit_trans
= journal
->j_committing_transaction
;
591 if (commit_trans
&& commit_trans
->t_tid
== tid
&&
592 commit_trans
->t_state
>= T_COMMIT_RECORD
)
596 spin_unlock(&journal
->j_state_lock
);
599 EXPORT_SYMBOL(journal_trans_will_send_data_barrier
);
602 * Log buffer allocation routines:
605 int journal_next_log_block(journal_t
*journal
, unsigned int *retp
)
607 unsigned int blocknr
;
609 spin_lock(&journal
->j_state_lock
);
610 J_ASSERT(journal
->j_free
> 1);
612 blocknr
= journal
->j_head
;
615 if (journal
->j_head
== journal
->j_last
)
616 journal
->j_head
= journal
->j_first
;
617 spin_unlock(&journal
->j_state_lock
);
618 return journal_bmap(journal
, blocknr
, retp
);
622 * Conversion of logical to physical block numbers for the journal
624 * On external journals the journal blocks are identity-mapped, so
625 * this is a no-op. If needed, we can use j_blk_offset - everything is
628 int journal_bmap(journal_t
*journal
, unsigned int blocknr
,
634 if (journal
->j_inode
) {
635 ret
= bmap(journal
->j_inode
, blocknr
);
639 char b
[BDEVNAME_SIZE
];
641 printk(KERN_ALERT
"%s: journal block not found "
642 "at offset %u on %s\n",
645 bdevname(journal
->j_dev
, b
));
647 __journal_abort_soft(journal
, err
);
650 *retp
= blocknr
; /* +journal->j_blk_offset */
656 * We play buffer_head aliasing tricks to write data/metadata blocks to
657 * the journal without copying their contents, but for journal
658 * descriptor blocks we do need to generate bona fide buffers.
660 * After the caller of journal_get_descriptor_buffer() has finished modifying
661 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
662 * But we don't bother doing that, so there will be coherency problems with
663 * mmaps of blockdevs which hold live JBD-controlled filesystems.
665 struct journal_head
*journal_get_descriptor_buffer(journal_t
*journal
)
667 struct buffer_head
*bh
;
668 unsigned int blocknr
;
671 err
= journal_next_log_block(journal
, &blocknr
);
676 bh
= __getblk(journal
->j_dev
, blocknr
, journal
->j_blocksize
);
680 memset(bh
->b_data
, 0, journal
->j_blocksize
);
681 set_buffer_uptodate(bh
);
683 BUFFER_TRACE(bh
, "return this buffer");
684 return journal_add_journal_head(bh
);
688 * Management for journal control blocks: functions to create and
689 * destroy journal_t structures, and to initialise and read existing
690 * journal blocks from disk. */
692 /* First: create and setup a journal_t object in memory. We initialise
693 * very few fields yet: that has to wait until we have created the
694 * journal structures from from scratch, or loaded them from disk. */
696 static journal_t
* journal_init_common (void)
701 journal
= kzalloc(sizeof(*journal
), GFP_KERNEL
);
705 init_waitqueue_head(&journal
->j_wait_transaction_locked
);
706 init_waitqueue_head(&journal
->j_wait_logspace
);
707 init_waitqueue_head(&journal
->j_wait_done_commit
);
708 init_waitqueue_head(&journal
->j_wait_checkpoint
);
709 init_waitqueue_head(&journal
->j_wait_commit
);
710 init_waitqueue_head(&journal
->j_wait_updates
);
711 mutex_init(&journal
->j_barrier
);
712 mutex_init(&journal
->j_checkpoint_mutex
);
713 spin_lock_init(&journal
->j_revoke_lock
);
714 spin_lock_init(&journal
->j_list_lock
);
715 spin_lock_init(&journal
->j_state_lock
);
717 journal
->j_commit_interval
= (HZ
* JBD_DEFAULT_MAX_COMMIT_AGE
);
719 /* The journal is marked for error until we succeed with recovery! */
720 journal
->j_flags
= JFS_ABORT
;
722 /* Set up a default-sized revoke table for the new mount. */
723 err
= journal_init_revoke(journal
, JOURNAL_REVOKE_DEFAULT_HASH
);
733 /* journal_init_dev and journal_init_inode:
735 * Create a journal structure assigned some fixed set of disk blocks to
736 * the journal. We don't actually touch those disk blocks yet, but we
737 * need to set up all of the mapping information to tell the journaling
738 * system where the journal blocks are.
743 * journal_t * journal_init_dev() - creates and initialises a journal structure
744 * @bdev: Block device on which to create the journal
745 * @fs_dev: Device which hold journalled filesystem for this journal.
746 * @start: Block nr Start of journal.
747 * @len: Length of the journal in blocks.
748 * @blocksize: blocksize of journalling device
750 * Returns: a newly created journal_t *
752 * journal_init_dev creates a journal which maps a fixed contiguous
753 * range of blocks on an arbitrary block device.
756 journal_t
* journal_init_dev(struct block_device
*bdev
,
757 struct block_device
*fs_dev
,
758 int start
, int len
, int blocksize
)
760 journal_t
*journal
= journal_init_common();
761 struct buffer_head
*bh
;
767 /* journal descriptor can store up to n blocks -bzzz */
768 journal
->j_blocksize
= blocksize
;
769 n
= journal
->j_blocksize
/ sizeof(journal_block_tag_t
);
770 journal
->j_wbufsize
= n
;
771 journal
->j_wbuf
= kmalloc(n
* sizeof(struct buffer_head
*), GFP_KERNEL
);
772 if (!journal
->j_wbuf
) {
773 printk(KERN_ERR
"%s: Cant allocate bhs for commit thread\n",
777 journal
->j_dev
= bdev
;
778 journal
->j_fs_dev
= fs_dev
;
779 journal
->j_blk_offset
= start
;
780 journal
->j_maxlen
= len
;
782 bh
= __getblk(journal
->j_dev
, start
, journal
->j_blocksize
);
785 "%s: Cannot get buffer for journal superblock\n",
789 journal
->j_sb_buffer
= bh
;
790 journal
->j_superblock
= (journal_superblock_t
*)bh
->b_data
;
794 kfree(journal
->j_wbuf
);
800 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
801 * @inode: An inode to create the journal in
803 * journal_init_inode creates a journal which maps an on-disk inode as
804 * the journal. The inode must exist already, must support bmap() and
805 * must have all data blocks preallocated.
807 journal_t
* journal_init_inode (struct inode
*inode
)
809 struct buffer_head
*bh
;
810 journal_t
*journal
= journal_init_common();
813 unsigned int blocknr
;
818 journal
->j_dev
= journal
->j_fs_dev
= inode
->i_sb
->s_bdev
;
819 journal
->j_inode
= inode
;
821 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
822 journal
, inode
->i_sb
->s_id
, inode
->i_ino
,
823 (long long) inode
->i_size
,
824 inode
->i_sb
->s_blocksize_bits
, inode
->i_sb
->s_blocksize
);
826 journal
->j_maxlen
= inode
->i_size
>> inode
->i_sb
->s_blocksize_bits
;
827 journal
->j_blocksize
= inode
->i_sb
->s_blocksize
;
829 /* journal descriptor can store up to n blocks -bzzz */
830 n
= journal
->j_blocksize
/ sizeof(journal_block_tag_t
);
831 journal
->j_wbufsize
= n
;
832 journal
->j_wbuf
= kmalloc(n
* sizeof(struct buffer_head
*), GFP_KERNEL
);
833 if (!journal
->j_wbuf
) {
834 printk(KERN_ERR
"%s: Cant allocate bhs for commit thread\n",
839 err
= journal_bmap(journal
, 0, &blocknr
);
840 /* If that failed, give up */
842 printk(KERN_ERR
"%s: Cannnot locate journal superblock\n",
847 bh
= __getblk(journal
->j_dev
, blocknr
, journal
->j_blocksize
);
850 "%s: Cannot get buffer for journal superblock\n",
854 journal
->j_sb_buffer
= bh
;
855 journal
->j_superblock
= (journal_superblock_t
*)bh
->b_data
;
859 kfree(journal
->j_wbuf
);
865 * If the journal init or create aborts, we need to mark the journal
866 * superblock as being NULL to prevent the journal destroy from writing
867 * back a bogus superblock.
869 static void journal_fail_superblock (journal_t
*journal
)
871 struct buffer_head
*bh
= journal
->j_sb_buffer
;
873 journal
->j_sb_buffer
= NULL
;
877 * Given a journal_t structure, initialise the various fields for
878 * startup of a new journaling session. We use this both when creating
879 * a journal, and after recovering an old journal to reset it for
883 static int journal_reset(journal_t
*journal
)
885 journal_superblock_t
*sb
= journal
->j_superblock
;
886 unsigned int first
, last
;
888 first
= be32_to_cpu(sb
->s_first
);
889 last
= be32_to_cpu(sb
->s_maxlen
);
890 if (first
+ JFS_MIN_JOURNAL_BLOCKS
> last
+ 1) {
891 printk(KERN_ERR
"JBD: Journal too short (blocks %u-%u).\n",
893 journal_fail_superblock(journal
);
897 journal
->j_first
= first
;
898 journal
->j_last
= last
;
900 journal
->j_head
= first
;
901 journal
->j_tail
= first
;
902 journal
->j_free
= last
- first
;
904 journal
->j_tail_sequence
= journal
->j_transaction_sequence
;
905 journal
->j_commit_sequence
= journal
->j_transaction_sequence
- 1;
906 journal
->j_commit_request
= journal
->j_commit_sequence
;
908 journal
->j_max_transaction_buffers
= journal
->j_maxlen
/ 4;
910 /* Add the dynamic fields and write it to disk. */
911 journal_update_superblock(journal
, 1);
912 return journal_start_thread(journal
);
916 * int journal_create() - Initialise the new journal file
917 * @journal: Journal to create. This structure must have been initialised
919 * Given a journal_t structure which tells us which disk blocks we can
920 * use, create a new journal superblock and initialise all of the
921 * journal fields from scratch.
923 int journal_create(journal_t
*journal
)
925 unsigned int blocknr
;
926 struct buffer_head
*bh
;
927 journal_superblock_t
*sb
;
930 if (journal
->j_maxlen
< JFS_MIN_JOURNAL_BLOCKS
) {
931 printk (KERN_ERR
"Journal length (%d blocks) too short.\n",
933 journal_fail_superblock(journal
);
937 if (journal
->j_inode
== NULL
) {
939 * We don't know what block to start at!
942 "%s: creation of journal on external device!\n",
947 /* Zero out the entire journal on disk. We cannot afford to
948 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
949 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
950 for (i
= 0; i
< journal
->j_maxlen
; i
++) {
951 err
= journal_bmap(journal
, i
, &blocknr
);
954 bh
= __getblk(journal
->j_dev
, blocknr
, journal
->j_blocksize
);
958 memset (bh
->b_data
, 0, journal
->j_blocksize
);
959 BUFFER_TRACE(bh
, "marking dirty");
960 mark_buffer_dirty(bh
);
961 BUFFER_TRACE(bh
, "marking uptodate");
962 set_buffer_uptodate(bh
);
967 sync_blockdev(journal
->j_dev
);
968 jbd_debug(1, "JBD: journal cleared.\n");
970 /* OK, fill in the initial static fields in the new superblock */
971 sb
= journal
->j_superblock
;
973 sb
->s_header
.h_magic
= cpu_to_be32(JFS_MAGIC_NUMBER
);
974 sb
->s_header
.h_blocktype
= cpu_to_be32(JFS_SUPERBLOCK_V2
);
976 sb
->s_blocksize
= cpu_to_be32(journal
->j_blocksize
);
977 sb
->s_maxlen
= cpu_to_be32(journal
->j_maxlen
);
978 sb
->s_first
= cpu_to_be32(1);
980 journal
->j_transaction_sequence
= 1;
982 journal
->j_flags
&= ~JFS_ABORT
;
983 journal
->j_format_version
= 2;
985 return journal_reset(journal
);
989 * void journal_update_superblock() - Update journal sb on disk.
990 * @journal: The journal to update.
991 * @wait: Set to '0' if you don't want to wait for IO completion.
993 * Update a journal's dynamic superblock fields and write it to disk,
994 * optionally waiting for the IO to complete.
996 void journal_update_superblock(journal_t
*journal
, int wait
)
998 journal_superblock_t
*sb
= journal
->j_superblock
;
999 struct buffer_head
*bh
= journal
->j_sb_buffer
;
1002 * As a special case, if the on-disk copy is already marked as needing
1003 * no recovery (s_start == 0) and there are no outstanding transactions
1004 * in the filesystem, then we can safely defer the superblock update
1005 * until the next commit by setting JFS_FLUSHED. This avoids
1006 * attempting a write to a potential-readonly device.
1008 if (sb
->s_start
== 0 && journal
->j_tail_sequence
==
1009 journal
->j_transaction_sequence
) {
1010 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1011 "(start %u, seq %d, errno %d)\n",
1012 journal
->j_tail
, journal
->j_tail_sequence
,
1017 if (buffer_write_io_error(bh
)) {
1018 char b
[BDEVNAME_SIZE
];
1020 * Oh, dear. A previous attempt to write the journal
1021 * superblock failed. This could happen because the
1022 * USB device was yanked out. Or it could happen to
1023 * be a transient write error and maybe the block will
1024 * be remapped. Nothing we can do but to retry the
1025 * write and hope for the best.
1027 printk(KERN_ERR
"JBD: previous I/O error detected "
1028 "for journal superblock update for %s.\n",
1029 journal_dev_name(journal
, b
));
1030 clear_buffer_write_io_error(bh
);
1031 set_buffer_uptodate(bh
);
1034 spin_lock(&journal
->j_state_lock
);
1035 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
1036 journal
->j_tail
, journal
->j_tail_sequence
, journal
->j_errno
);
1038 sb
->s_sequence
= cpu_to_be32(journal
->j_tail_sequence
);
1039 sb
->s_start
= cpu_to_be32(journal
->j_tail
);
1040 sb
->s_errno
= cpu_to_be32(journal
->j_errno
);
1041 spin_unlock(&journal
->j_state_lock
);
1043 BUFFER_TRACE(bh
, "marking dirty");
1044 mark_buffer_dirty(bh
);
1046 sync_dirty_buffer(bh
);
1047 if (buffer_write_io_error(bh
)) {
1048 char b
[BDEVNAME_SIZE
];
1049 printk(KERN_ERR
"JBD: I/O error detected "
1050 "when updating journal superblock for %s.\n",
1051 journal_dev_name(journal
, b
));
1052 clear_buffer_write_io_error(bh
);
1053 set_buffer_uptodate(bh
);
1056 write_dirty_buffer(bh
, WRITE
);
1059 /* If we have just flushed the log (by marking s_start==0), then
1060 * any future commit will have to be careful to update the
1061 * superblock again to re-record the true start of the log. */
1063 spin_lock(&journal
->j_state_lock
);
1065 journal
->j_flags
&= ~JFS_FLUSHED
;
1067 journal
->j_flags
|= JFS_FLUSHED
;
1068 spin_unlock(&journal
->j_state_lock
);
1072 * Read the superblock for a given journal, performing initial
1073 * validation of the format.
1076 static int journal_get_superblock(journal_t
*journal
)
1078 struct buffer_head
*bh
;
1079 journal_superblock_t
*sb
;
1082 bh
= journal
->j_sb_buffer
;
1084 J_ASSERT(bh
!= NULL
);
1085 if (!buffer_uptodate(bh
)) {
1086 ll_rw_block(READ
, 1, &bh
);
1088 if (!buffer_uptodate(bh
)) {
1090 "JBD: IO error reading journal superblock\n");
1095 sb
= journal
->j_superblock
;
1099 if (sb
->s_header
.h_magic
!= cpu_to_be32(JFS_MAGIC_NUMBER
) ||
1100 sb
->s_blocksize
!= cpu_to_be32(journal
->j_blocksize
)) {
1101 printk(KERN_WARNING
"JBD: no valid journal superblock found\n");
1105 switch(be32_to_cpu(sb
->s_header
.h_blocktype
)) {
1106 case JFS_SUPERBLOCK_V1
:
1107 journal
->j_format_version
= 1;
1109 case JFS_SUPERBLOCK_V2
:
1110 journal
->j_format_version
= 2;
1113 printk(KERN_WARNING
"JBD: unrecognised superblock format ID\n");
1117 if (be32_to_cpu(sb
->s_maxlen
) < journal
->j_maxlen
)
1118 journal
->j_maxlen
= be32_to_cpu(sb
->s_maxlen
);
1119 else if (be32_to_cpu(sb
->s_maxlen
) > journal
->j_maxlen
) {
1120 printk (KERN_WARNING
"JBD: journal file too short\n");
1127 journal_fail_superblock(journal
);
1132 * Load the on-disk journal superblock and read the key fields into the
1136 static int load_superblock(journal_t
*journal
)
1139 journal_superblock_t
*sb
;
1141 err
= journal_get_superblock(journal
);
1145 sb
= journal
->j_superblock
;
1147 journal
->j_tail_sequence
= be32_to_cpu(sb
->s_sequence
);
1148 journal
->j_tail
= be32_to_cpu(sb
->s_start
);
1149 journal
->j_first
= be32_to_cpu(sb
->s_first
);
1150 journal
->j_last
= be32_to_cpu(sb
->s_maxlen
);
1151 journal
->j_errno
= be32_to_cpu(sb
->s_errno
);
1158 * int journal_load() - Read journal from disk.
1159 * @journal: Journal to act on.
1161 * Given a journal_t structure which tells us which disk blocks contain
1162 * a journal, read the journal from disk to initialise the in-memory
1165 int journal_load(journal_t
*journal
)
1168 journal_superblock_t
*sb
;
1170 err
= load_superblock(journal
);
1174 sb
= journal
->j_superblock
;
1175 /* If this is a V2 superblock, then we have to check the
1176 * features flags on it. */
1178 if (journal
->j_format_version
>= 2) {
1179 if ((sb
->s_feature_ro_compat
&
1180 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES
)) ||
1181 (sb
->s_feature_incompat
&
1182 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES
))) {
1183 printk (KERN_WARNING
1184 "JBD: Unrecognised features on journal\n");
1189 /* Let the recovery code check whether it needs to recover any
1190 * data from the journal. */
1191 if (journal_recover(journal
))
1192 goto recovery_error
;
1194 /* OK, we've finished with the dynamic journal bits:
1195 * reinitialise the dynamic contents of the superblock in memory
1196 * and reset them on disk. */
1197 if (journal_reset(journal
))
1198 goto recovery_error
;
1200 journal
->j_flags
&= ~JFS_ABORT
;
1201 journal
->j_flags
|= JFS_LOADED
;
1205 printk (KERN_WARNING
"JBD: recovery failed\n");
1210 * void journal_destroy() - Release a journal_t structure.
1211 * @journal: Journal to act on.
1213 * Release a journal_t structure once it is no longer in use by the
1215 * Return <0 if we couldn't clean up the journal.
1217 int journal_destroy(journal_t
*journal
)
1222 /* Wait for the commit thread to wake up and die. */
1223 journal_kill_thread(journal
);
1225 /* Force a final log commit */
1226 if (journal
->j_running_transaction
)
1227 journal_commit_transaction(journal
);
1229 /* Force any old transactions to disk */
1231 /* Totally anal locking here... */
1232 spin_lock(&journal
->j_list_lock
);
1233 while (journal
->j_checkpoint_transactions
!= NULL
) {
1234 spin_unlock(&journal
->j_list_lock
);
1235 log_do_checkpoint(journal
);
1236 spin_lock(&journal
->j_list_lock
);
1239 J_ASSERT(journal
->j_running_transaction
== NULL
);
1240 J_ASSERT(journal
->j_committing_transaction
== NULL
);
1241 J_ASSERT(journal
->j_checkpoint_transactions
== NULL
);
1242 spin_unlock(&journal
->j_list_lock
);
1244 if (journal
->j_sb_buffer
) {
1245 if (!is_journal_aborted(journal
)) {
1246 /* We can now mark the journal as empty. */
1247 journal
->j_tail
= 0;
1248 journal
->j_tail_sequence
=
1249 ++journal
->j_transaction_sequence
;
1250 journal_update_superblock(journal
, 1);
1254 brelse(journal
->j_sb_buffer
);
1257 if (journal
->j_inode
)
1258 iput(journal
->j_inode
);
1259 if (journal
->j_revoke
)
1260 journal_destroy_revoke(journal
);
1261 kfree(journal
->j_wbuf
);
1269 *int journal_check_used_features () - Check if features specified are used.
1270 * @journal: Journal to check.
1271 * @compat: bitmask of compatible features
1272 * @ro: bitmask of features that force read-only mount
1273 * @incompat: bitmask of incompatible features
1275 * Check whether the journal uses all of a given set of
1276 * features. Return true (non-zero) if it does.
1279 int journal_check_used_features (journal_t
*journal
, unsigned long compat
,
1280 unsigned long ro
, unsigned long incompat
)
1282 journal_superblock_t
*sb
;
1284 if (!compat
&& !ro
&& !incompat
)
1286 if (journal
->j_format_version
== 1)
1289 sb
= journal
->j_superblock
;
1291 if (((be32_to_cpu(sb
->s_feature_compat
) & compat
) == compat
) &&
1292 ((be32_to_cpu(sb
->s_feature_ro_compat
) & ro
) == ro
) &&
1293 ((be32_to_cpu(sb
->s_feature_incompat
) & incompat
) == incompat
))
1300 * int journal_check_available_features() - Check feature set in journalling layer
1301 * @journal: Journal to check.
1302 * @compat: bitmask of compatible features
1303 * @ro: bitmask of features that force read-only mount
1304 * @incompat: bitmask of incompatible features
1306 * Check whether the journaling code supports the use of
1307 * all of a given set of features on this journal. Return true
1308 * (non-zero) if it can. */
1310 int journal_check_available_features (journal_t
*journal
, unsigned long compat
,
1311 unsigned long ro
, unsigned long incompat
)
1313 if (!compat
&& !ro
&& !incompat
)
1316 /* We can support any known requested features iff the
1317 * superblock is in version 2. Otherwise we fail to support any
1318 * extended sb features. */
1320 if (journal
->j_format_version
!= 2)
1323 if ((compat
& JFS_KNOWN_COMPAT_FEATURES
) == compat
&&
1324 (ro
& JFS_KNOWN_ROCOMPAT_FEATURES
) == ro
&&
1325 (incompat
& JFS_KNOWN_INCOMPAT_FEATURES
) == incompat
)
1332 * int journal_set_features () - Mark a given journal feature in the superblock
1333 * @journal: Journal to act on.
1334 * @compat: bitmask of compatible features
1335 * @ro: bitmask of features that force read-only mount
1336 * @incompat: bitmask of incompatible features
1338 * Mark a given journal feature as present on the
1339 * superblock. Returns true if the requested features could be set.
1343 int journal_set_features (journal_t
*journal
, unsigned long compat
,
1344 unsigned long ro
, unsigned long incompat
)
1346 journal_superblock_t
*sb
;
1348 if (journal_check_used_features(journal
, compat
, ro
, incompat
))
1351 if (!journal_check_available_features(journal
, compat
, ro
, incompat
))
1354 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1355 compat
, ro
, incompat
);
1357 sb
= journal
->j_superblock
;
1359 sb
->s_feature_compat
|= cpu_to_be32(compat
);
1360 sb
->s_feature_ro_compat
|= cpu_to_be32(ro
);
1361 sb
->s_feature_incompat
|= cpu_to_be32(incompat
);
1368 * int journal_update_format () - Update on-disk journal structure.
1369 * @journal: Journal to act on.
1371 * Given an initialised but unloaded journal struct, poke about in the
1372 * on-disk structure to update it to the most recent supported version.
1374 int journal_update_format (journal_t
*journal
)
1376 journal_superblock_t
*sb
;
1379 err
= journal_get_superblock(journal
);
1383 sb
= journal
->j_superblock
;
1385 switch (be32_to_cpu(sb
->s_header
.h_blocktype
)) {
1386 case JFS_SUPERBLOCK_V2
:
1388 case JFS_SUPERBLOCK_V1
:
1389 return journal_convert_superblock_v1(journal
, sb
);
1396 static int journal_convert_superblock_v1(journal_t
*journal
,
1397 journal_superblock_t
*sb
)
1399 int offset
, blocksize
;
1400 struct buffer_head
*bh
;
1403 "JBD: Converting superblock from version 1 to 2.\n");
1405 /* Pre-initialise new fields to zero */
1406 offset
= ((char *) &(sb
->s_feature_compat
)) - ((char *) sb
);
1407 blocksize
= be32_to_cpu(sb
->s_blocksize
);
1408 memset(&sb
->s_feature_compat
, 0, blocksize
-offset
);
1410 sb
->s_nr_users
= cpu_to_be32(1);
1411 sb
->s_header
.h_blocktype
= cpu_to_be32(JFS_SUPERBLOCK_V2
);
1412 journal
->j_format_version
= 2;
1414 bh
= journal
->j_sb_buffer
;
1415 BUFFER_TRACE(bh
, "marking dirty");
1416 mark_buffer_dirty(bh
);
1417 sync_dirty_buffer(bh
);
1423 * int journal_flush () - Flush journal
1424 * @journal: Journal to act on.
1426 * Flush all data for a given journal to disk and empty the journal.
1427 * Filesystems can use this when remounting readonly to ensure that
1428 * recovery does not need to happen on remount.
1431 int journal_flush(journal_t
*journal
)
1434 transaction_t
*transaction
= NULL
;
1435 unsigned int old_tail
;
1437 spin_lock(&journal
->j_state_lock
);
1439 /* Force everything buffered to the log... */
1440 if (journal
->j_running_transaction
) {
1441 transaction
= journal
->j_running_transaction
;
1442 __log_start_commit(journal
, transaction
->t_tid
);
1443 } else if (journal
->j_committing_transaction
)
1444 transaction
= journal
->j_committing_transaction
;
1446 /* Wait for the log commit to complete... */
1448 tid_t tid
= transaction
->t_tid
;
1450 spin_unlock(&journal
->j_state_lock
);
1451 log_wait_commit(journal
, tid
);
1453 spin_unlock(&journal
->j_state_lock
);
1456 /* ...and flush everything in the log out to disk. */
1457 spin_lock(&journal
->j_list_lock
);
1458 while (!err
&& journal
->j_checkpoint_transactions
!= NULL
) {
1459 spin_unlock(&journal
->j_list_lock
);
1460 mutex_lock(&journal
->j_checkpoint_mutex
);
1461 err
= log_do_checkpoint(journal
);
1462 mutex_unlock(&journal
->j_checkpoint_mutex
);
1463 spin_lock(&journal
->j_list_lock
);
1465 spin_unlock(&journal
->j_list_lock
);
1467 if (is_journal_aborted(journal
))
1470 cleanup_journal_tail(journal
);
1472 /* Finally, mark the journal as really needing no recovery.
1473 * This sets s_start==0 in the underlying superblock, which is
1474 * the magic code for a fully-recovered superblock. Any future
1475 * commits of data to the journal will restore the current
1477 spin_lock(&journal
->j_state_lock
);
1478 old_tail
= journal
->j_tail
;
1479 journal
->j_tail
= 0;
1480 spin_unlock(&journal
->j_state_lock
);
1481 journal_update_superblock(journal
, 1);
1482 spin_lock(&journal
->j_state_lock
);
1483 journal
->j_tail
= old_tail
;
1485 J_ASSERT(!journal
->j_running_transaction
);
1486 J_ASSERT(!journal
->j_committing_transaction
);
1487 J_ASSERT(!journal
->j_checkpoint_transactions
);
1488 J_ASSERT(journal
->j_head
== journal
->j_tail
);
1489 J_ASSERT(journal
->j_tail_sequence
== journal
->j_transaction_sequence
);
1490 spin_unlock(&journal
->j_state_lock
);
1495 * int journal_wipe() - Wipe journal contents
1496 * @journal: Journal to act on.
1497 * @write: flag (see below)
1499 * Wipe out all of the contents of a journal, safely. This will produce
1500 * a warning if the journal contains any valid recovery information.
1501 * Must be called between journal_init_*() and journal_load().
1503 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1504 * we merely suppress recovery.
1507 int journal_wipe(journal_t
*journal
, int write
)
1511 J_ASSERT (!(journal
->j_flags
& JFS_LOADED
));
1513 err
= load_superblock(journal
);
1517 if (!journal
->j_tail
)
1520 printk (KERN_WARNING
"JBD: %s recovery information on journal\n",
1521 write
? "Clearing" : "Ignoring");
1523 err
= journal_skip_recovery(journal
);
1525 journal_update_superblock(journal
, 1);
1532 * journal_dev_name: format a character string to describe on what
1533 * device this journal is present.
1536 static const char *journal_dev_name(journal_t
*journal
, char *buffer
)
1538 struct block_device
*bdev
;
1540 if (journal
->j_inode
)
1541 bdev
= journal
->j_inode
->i_sb
->s_bdev
;
1543 bdev
= journal
->j_dev
;
1545 return bdevname(bdev
, buffer
);
1549 * Journal abort has very specific semantics, which we describe
1550 * for journal abort.
1552 * Two internal function, which provide abort to te jbd layer
1557 * Quick version for internal journal use (doesn't lock the journal).
1558 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1559 * and don't attempt to make any other journal updates.
1561 static void __journal_abort_hard(journal_t
*journal
)
1563 transaction_t
*transaction
;
1564 char b
[BDEVNAME_SIZE
];
1566 if (journal
->j_flags
& JFS_ABORT
)
1569 printk(KERN_ERR
"Aborting journal on device %s.\n",
1570 journal_dev_name(journal
, b
));
1572 spin_lock(&journal
->j_state_lock
);
1573 journal
->j_flags
|= JFS_ABORT
;
1574 transaction
= journal
->j_running_transaction
;
1576 __log_start_commit(journal
, transaction
->t_tid
);
1577 spin_unlock(&journal
->j_state_lock
);
1580 /* Soft abort: record the abort error status in the journal superblock,
1581 * but don't do any other IO. */
1582 static void __journal_abort_soft (journal_t
*journal
, int errno
)
1584 if (journal
->j_flags
& JFS_ABORT
)
1587 if (!journal
->j_errno
)
1588 journal
->j_errno
= errno
;
1590 __journal_abort_hard(journal
);
1593 journal_update_superblock(journal
, 1);
1597 * void journal_abort () - Shutdown the journal immediately.
1598 * @journal: the journal to shutdown.
1599 * @errno: an error number to record in the journal indicating
1600 * the reason for the shutdown.
1602 * Perform a complete, immediate shutdown of the ENTIRE
1603 * journal (not of a single transaction). This operation cannot be
1604 * undone without closing and reopening the journal.
1606 * The journal_abort function is intended to support higher level error
1607 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1610 * Journal abort has very specific semantics. Any existing dirty,
1611 * unjournaled buffers in the main filesystem will still be written to
1612 * disk by bdflush, but the journaling mechanism will be suspended
1613 * immediately and no further transaction commits will be honoured.
1615 * Any dirty, journaled buffers will be written back to disk without
1616 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1617 * filesystem, but we _do_ attempt to leave as much data as possible
1618 * behind for fsck to use for cleanup.
1620 * Any attempt to get a new transaction handle on a journal which is in
1621 * ABORT state will just result in an -EROFS error return. A
1622 * journal_stop on an existing handle will return -EIO if we have
1623 * entered abort state during the update.
1625 * Recursive transactions are not disturbed by journal abort until the
1626 * final journal_stop, which will receive the -EIO error.
1628 * Finally, the journal_abort call allows the caller to supply an errno
1629 * which will be recorded (if possible) in the journal superblock. This
1630 * allows a client to record failure conditions in the middle of a
1631 * transaction without having to complete the transaction to record the
1632 * failure to disk. ext3_error, for example, now uses this
1635 * Errors which originate from within the journaling layer will NOT
1636 * supply an errno; a null errno implies that absolutely no further
1637 * writes are done to the journal (unless there are any already in
1642 void journal_abort(journal_t
*journal
, int errno
)
1644 __journal_abort_soft(journal
, errno
);
1648 * int journal_errno () - returns the journal's error state.
1649 * @journal: journal to examine.
1651 * This is the errno numbet set with journal_abort(), the last
1652 * time the journal was mounted - if the journal was stopped
1653 * without calling abort this will be 0.
1655 * If the journal has been aborted on this mount time -EROFS will
1658 int journal_errno(journal_t
*journal
)
1662 spin_lock(&journal
->j_state_lock
);
1663 if (journal
->j_flags
& JFS_ABORT
)
1666 err
= journal
->j_errno
;
1667 spin_unlock(&journal
->j_state_lock
);
1672 * int journal_clear_err () - clears the journal's error state
1673 * @journal: journal to act on.
1675 * An error must be cleared or Acked to take a FS out of readonly
1678 int journal_clear_err(journal_t
*journal
)
1682 spin_lock(&journal
->j_state_lock
);
1683 if (journal
->j_flags
& JFS_ABORT
)
1686 journal
->j_errno
= 0;
1687 spin_unlock(&journal
->j_state_lock
);
1692 * void journal_ack_err() - Ack journal err.
1693 * @journal: journal to act on.
1695 * An error must be cleared or Acked to take a FS out of readonly
1698 void journal_ack_err(journal_t
*journal
)
1700 spin_lock(&journal
->j_state_lock
);
1701 if (journal
->j_errno
)
1702 journal
->j_flags
|= JFS_ACK_ERR
;
1703 spin_unlock(&journal
->j_state_lock
);
1706 int journal_blocks_per_page(struct inode
*inode
)
1708 return 1 << (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
1712 * Journal_head storage management
1714 static struct kmem_cache
*journal_head_cache
;
1715 #ifdef CONFIG_JBD_DEBUG
1716 static atomic_t nr_journal_heads
= ATOMIC_INIT(0);
1719 static int journal_init_journal_head_cache(void)
1723 J_ASSERT(journal_head_cache
== NULL
);
1724 journal_head_cache
= kmem_cache_create("journal_head",
1725 sizeof(struct journal_head
),
1727 SLAB_TEMPORARY
, /* flags */
1730 if (!journal_head_cache
) {
1732 printk(KERN_EMERG
"JBD: no memory for journal_head cache\n");
1737 static void journal_destroy_journal_head_cache(void)
1739 if (journal_head_cache
) {
1740 kmem_cache_destroy(journal_head_cache
);
1741 journal_head_cache
= NULL
;
1746 * journal_head splicing and dicing
1748 static struct journal_head
*journal_alloc_journal_head(void)
1750 struct journal_head
*ret
;
1752 #ifdef CONFIG_JBD_DEBUG
1753 atomic_inc(&nr_journal_heads
);
1755 ret
= kmem_cache_alloc(journal_head_cache
, GFP_NOFS
);
1757 jbd_debug(1, "out of memory for journal_head\n");
1758 printk_ratelimited(KERN_NOTICE
"ENOMEM in %s, retrying.\n",
1761 while (ret
== NULL
) {
1763 ret
= kmem_cache_alloc(journal_head_cache
, GFP_NOFS
);
1769 static void journal_free_journal_head(struct journal_head
*jh
)
1771 #ifdef CONFIG_JBD_DEBUG
1772 atomic_dec(&nr_journal_heads
);
1773 memset(jh
, JBD_POISON_FREE
, sizeof(*jh
));
1775 kmem_cache_free(journal_head_cache
, jh
);
1779 * A journal_head is attached to a buffer_head whenever JBD has an
1780 * interest in the buffer.
1782 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1783 * is set. This bit is tested in core kernel code where we need to take
1784 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1787 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1789 * When a buffer has its BH_JBD bit set it is immune from being released by
1790 * core kernel code, mainly via ->b_count.
1792 * A journal_head may be detached from its buffer_head when the journal_head's
1793 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1794 * Various places in JBD call journal_remove_journal_head() to indicate that the
1795 * journal_head can be dropped if needed.
1797 * Various places in the kernel want to attach a journal_head to a buffer_head
1798 * _before_ attaching the journal_head to a transaction. To protect the
1799 * journal_head in this situation, journal_add_journal_head elevates the
1800 * journal_head's b_jcount refcount by one. The caller must call
1801 * journal_put_journal_head() to undo this.
1803 * So the typical usage would be:
1805 * (Attach a journal_head if needed. Increments b_jcount)
1806 * struct journal_head *jh = journal_add_journal_head(bh);
1808 * jh->b_transaction = xxx;
1809 * journal_put_journal_head(jh);
1811 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1812 * because it has a non-zero b_transaction.
1816 * Give a buffer_head a journal_head.
1818 * Doesn't need the journal lock.
1821 struct journal_head
*journal_add_journal_head(struct buffer_head
*bh
)
1823 struct journal_head
*jh
;
1824 struct journal_head
*new_jh
= NULL
;
1827 if (!buffer_jbd(bh
)) {
1828 new_jh
= journal_alloc_journal_head();
1829 memset(new_jh
, 0, sizeof(*new_jh
));
1832 jbd_lock_bh_journal_head(bh
);
1833 if (buffer_jbd(bh
)) {
1837 (atomic_read(&bh
->b_count
) > 0) ||
1838 (bh
->b_page
&& bh
->b_page
->mapping
));
1841 jbd_unlock_bh_journal_head(bh
);
1846 new_jh
= NULL
; /* We consumed it */
1851 BUFFER_TRACE(bh
, "added journal_head");
1854 jbd_unlock_bh_journal_head(bh
);
1856 journal_free_journal_head(new_jh
);
1857 return bh
->b_private
;
1861 * Grab a ref against this buffer_head's journal_head. If it ended up not
1862 * having a journal_head, return NULL
1864 struct journal_head
*journal_grab_journal_head(struct buffer_head
*bh
)
1866 struct journal_head
*jh
= NULL
;
1868 jbd_lock_bh_journal_head(bh
);
1869 if (buffer_jbd(bh
)) {
1873 jbd_unlock_bh_journal_head(bh
);
1877 static void __journal_remove_journal_head(struct buffer_head
*bh
)
1879 struct journal_head
*jh
= bh2jh(bh
);
1881 J_ASSERT_JH(jh
, jh
->b_jcount
>= 0);
1884 if (jh
->b_jcount
== 0) {
1885 if (jh
->b_transaction
== NULL
&&
1886 jh
->b_next_transaction
== NULL
&&
1887 jh
->b_cp_transaction
== NULL
) {
1888 J_ASSERT_JH(jh
, jh
->b_jlist
== BJ_None
);
1889 J_ASSERT_BH(bh
, buffer_jbd(bh
));
1890 J_ASSERT_BH(bh
, jh2bh(jh
) == bh
);
1891 BUFFER_TRACE(bh
, "remove journal_head");
1892 if (jh
->b_frozen_data
) {
1893 printk(KERN_WARNING
"%s: freeing "
1896 jbd_free(jh
->b_frozen_data
, bh
->b_size
);
1898 if (jh
->b_committed_data
) {
1899 printk(KERN_WARNING
"%s: freeing "
1900 "b_committed_data\n",
1902 jbd_free(jh
->b_committed_data
, bh
->b_size
);
1904 bh
->b_private
= NULL
;
1905 jh
->b_bh
= NULL
; /* debug, really */
1906 clear_buffer_jbd(bh
);
1908 journal_free_journal_head(jh
);
1910 BUFFER_TRACE(bh
, "journal_head was locked");
1916 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1917 * and has a zero b_jcount then remove and release its journal_head. If we did
1918 * see that the buffer is not used by any transaction we also "logically"
1919 * decrement ->b_count.
1921 * We in fact take an additional increment on ->b_count as a convenience,
1922 * because the caller usually wants to do additional things with the bh
1923 * after calling here.
1924 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1925 * time. Once the caller has run __brelse(), the buffer is eligible for
1926 * reaping by try_to_free_buffers().
1928 void journal_remove_journal_head(struct buffer_head
*bh
)
1930 jbd_lock_bh_journal_head(bh
);
1931 __journal_remove_journal_head(bh
);
1932 jbd_unlock_bh_journal_head(bh
);
1936 * Drop a reference on the passed journal_head. If it fell to zero then try to
1937 * release the journal_head from the buffer_head.
1939 void journal_put_journal_head(struct journal_head
*jh
)
1941 struct buffer_head
*bh
= jh2bh(jh
);
1943 jbd_lock_bh_journal_head(bh
);
1944 J_ASSERT_JH(jh
, jh
->b_jcount
> 0);
1946 if (!jh
->b_jcount
&& !jh
->b_transaction
) {
1947 __journal_remove_journal_head(bh
);
1950 jbd_unlock_bh_journal_head(bh
);
1956 #ifdef CONFIG_JBD_DEBUG
1958 u8 journal_enable_debug __read_mostly
;
1959 EXPORT_SYMBOL(journal_enable_debug
);
1961 static struct dentry
*jbd_debugfs_dir
;
1962 static struct dentry
*jbd_debug
;
1964 static void __init
jbd_create_debugfs_entry(void)
1966 jbd_debugfs_dir
= debugfs_create_dir("jbd", NULL
);
1967 if (jbd_debugfs_dir
)
1968 jbd_debug
= debugfs_create_u8("jbd-debug", S_IRUGO
| S_IWUSR
,
1970 &journal_enable_debug
);
1973 static void __exit
jbd_remove_debugfs_entry(void)
1975 debugfs_remove(jbd_debug
);
1976 debugfs_remove(jbd_debugfs_dir
);
1981 static inline void jbd_create_debugfs_entry(void)
1985 static inline void jbd_remove_debugfs_entry(void)
1991 struct kmem_cache
*jbd_handle_cache
;
1993 static int __init
journal_init_handle_cache(void)
1995 jbd_handle_cache
= kmem_cache_create("journal_handle",
1998 SLAB_TEMPORARY
, /* flags */
2000 if (jbd_handle_cache
== NULL
) {
2001 printk(KERN_EMERG
"JBD: failed to create handle cache\n");
2007 static void journal_destroy_handle_cache(void)
2009 if (jbd_handle_cache
)
2010 kmem_cache_destroy(jbd_handle_cache
);
2014 * Module startup and shutdown
2017 static int __init
journal_init_caches(void)
2021 ret
= journal_init_revoke_caches();
2023 ret
= journal_init_journal_head_cache();
2025 ret
= journal_init_handle_cache();
2029 static void journal_destroy_caches(void)
2031 journal_destroy_revoke_caches();
2032 journal_destroy_journal_head_cache();
2033 journal_destroy_handle_cache();
2036 static int __init
journal_init(void)
2040 BUILD_BUG_ON(sizeof(struct journal_superblock_s
) != 1024);
2042 ret
= journal_init_caches();
2044 journal_destroy_caches();
2045 jbd_create_debugfs_entry();
2049 static void __exit
journal_exit(void)
2051 #ifdef CONFIG_JBD_DEBUG
2052 int n
= atomic_read(&nr_journal_heads
);
2054 printk(KERN_EMERG
"JBD: leaked %d journal_heads!\n", n
);
2056 jbd_remove_debugfs_entry();
2057 journal_destroy_caches();
2060 MODULE_LICENSE("GPL");
2061 module_init(journal_init
);
2062 module_exit(journal_exit
);