25 #include <linux/module.h>
26 #include <linux/time.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
36 #include <linux/poison.h>
41 #define CREATE_TRACE_POINTS
44 #include <asm/uaccess.h>
90 static void __journal_abort_soft (journal_t *journal,
int errno);
91 static const char *journal_dev_name(journal_t *journal,
char *
buffer);
97 static void commit_timeout(
unsigned long __data)
120 static int kjournald(
void *
arg)
122 journal_t *journal =
arg;
129 setup_timer(&journal->j_commit_timer, commit_timeout,
136 wake_up(&journal->j_wait_done_commit);
139 journal->j_commit_interval /
HZ);
144 spin_lock(&journal->j_state_lock);
147 if (journal->j_flags & JFS_UNMOUNT)
150 jbd_debug(1,
"commit_sequence=%d, commit_request=%d\n",
151 journal->j_commit_sequence, journal->j_commit_request);
153 if (journal->j_commit_sequence != journal->j_commit_request) {
155 spin_unlock(&journal->j_state_lock);
158 spin_lock(&journal->j_state_lock);
162 wake_up(&journal->j_wait_done_commit);
169 jbd_debug(1,
"Now suspending kjournald\n");
170 spin_unlock(&journal->j_state_lock);
172 spin_lock(&journal->j_state_lock);
179 int should_sleep = 1;
183 if (journal->j_commit_sequence != journal->j_commit_request)
185 transaction = journal->j_running_transaction;
187 transaction->t_expires))
189 if (journal->j_flags & JFS_UNMOUNT)
192 spin_unlock(&journal->j_state_lock);
194 spin_lock(&journal->j_state_lock);
204 transaction = journal->j_running_transaction;
205 if (transaction &&
time_after_eq(jiffies, transaction->t_expires)) {
206 journal->j_commit_request = transaction->t_tid;
207 jbd_debug(1,
"woke because of timeout\n");
212 spin_unlock(&journal->j_state_lock);
214 journal->j_task =
NULL;
215 wake_up(&journal->j_wait_done_commit);
216 jbd_debug(1,
"Journal thread exiting.\n");
220 static int journal_start_thread(journal_t *journal)
232 static void journal_kill_thread(journal_t *journal)
234 spin_lock(&journal->j_state_lock);
235 journal->j_flags |= JFS_UNMOUNT;
237 while (journal->j_task) {
238 wake_up(&journal->j_wait_commit);
239 spin_unlock(&journal->j_state_lock);
241 journal->j_task ==
NULL);
242 spin_lock(&journal->j_state_lock);
244 spin_unlock(&journal->j_state_lock);
289 int need_copy_out = 0;
290 int done_copy_out = 0;
293 struct buffer_head *new_bh;
295 struct page *new_page;
296 unsigned int new_offset;
297 struct buffer_head *bh_in = jh2bh(jh_in);
298 journal_t *journal = transaction->t_journal;
309 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
322 jbd_lock_bh_state(bh_in);
329 new_page = jh2bh(jh_in)->b_page;
337 if (*((
__be32 *)(mapped_data + new_offset)) ==
347 if (need_copy_out && !done_copy_out) {
350 jbd_unlock_bh_state(bh_in);
351 tmp = jbd_alloc(bh_in->b_size,
GFP_NOFS);
352 jbd_lock_bh_state(bh_in);
354 jbd_free(tmp, bh_in->b_size);
360 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
374 *((
unsigned int *)(mapped_data + new_offset)) = 0;
380 new_bh->b_size = jh2bh(jh_in)->b_size;
381 new_bh->b_bdev = transaction->t_journal->j_dev;
383 set_buffer_mapped(new_bh);
384 set_buffer_dirty(new_bh);
393 JBUFFER_TRACE(jh_in,
"file as BJ_Shadow");
394 spin_lock(&journal->j_list_lock);
396 spin_unlock(&journal->j_list_lock);
397 jbd_unlock_bh_state(bh_in);
399 JBUFFER_TRACE(new_jh,
"file as BJ_IO");
402 return do_escape | (done_copy_out << 1);
420 int left = journal->j_free;
429 #define MIN_LOG_RESERVED_BLOCKS 32
449 if (journal->j_running_transaction &&
450 journal->j_running_transaction->t_tid == target) {
456 journal->j_commit_request =
target;
457 jbd_debug(1,
"JBD: requesting commit %d/%d\n",
458 journal->j_commit_request,
459 journal->j_commit_sequence);
460 wake_up(&journal->j_wait_commit);
462 }
else if (!tid_geq(journal->j_commit_request, target))
466 WARN_ONCE(1,
"jbd: bad log_start_commit: %u %u %u %u\n",
467 journal->j_commit_request, journal->j_commit_sequence,
468 target, journal->j_running_transaction ?
469 journal->j_running_transaction->t_tid : 0);
477 spin_lock(&journal->j_state_lock);
479 spin_unlock(&journal->j_state_lock);
498 spin_lock(&journal->j_state_lock);
499 if (journal->j_running_transaction && !
current->journal_info) {
500 transaction = journal->j_running_transaction;
502 }
else if (journal->j_committing_transaction)
503 transaction = journal->j_committing_transaction;
506 spin_unlock(&journal->j_state_lock);
510 tid = transaction->t_tid;
511 spin_unlock(&journal->j_state_lock);
525 spin_lock(&journal->j_state_lock);
526 if (journal->j_running_transaction) {
527 tid_t tid = journal->j_running_transaction->t_tid;
535 }
else if (journal->j_committing_transaction) {
541 *ptid = journal->j_committing_transaction->t_tid;
544 spin_unlock(&journal->j_state_lock);
556 #ifdef CONFIG_JBD_DEBUG
557 spin_lock(&journal->j_state_lock);
558 if (!tid_geq(journal->j_commit_request, tid)) {
560 "%s: error: j_commit_request=%d, tid=%d\n",
561 __func__, journal->j_commit_request, tid);
563 spin_unlock(&journal->j_state_lock);
565 spin_lock(&journal->j_state_lock);
566 if (!tid_geq(journal->j_commit_waited, tid))
567 journal->j_commit_waited = tid;
568 while (tid_gt(tid, journal->j_commit_sequence)) {
569 jbd_debug(1,
"JBD: want %d, j_commit_sequence=%d\n",
570 tid, journal->j_commit_sequence);
571 wake_up(&journal->j_wait_commit);
572 spin_unlock(&journal->j_state_lock);
574 !tid_gt(tid, journal->j_commit_sequence));
575 spin_lock(&journal->j_state_lock);
577 spin_unlock(&journal->j_state_lock);
579 if (
unlikely(is_journal_aborted(journal))) {
597 if (!(journal->j_flags & JFS_BARRIER))
599 spin_lock(&journal->j_state_lock);
601 if (tid_geq(journal->j_commit_sequence, tid))
607 commit_trans = journal->j_committing_transaction;
608 if (commit_trans && commit_trans->t_tid == tid &&
609 commit_trans->t_state >= T_COMMIT_RECORD)
613 spin_unlock(&journal->j_state_lock);
626 spin_lock(&journal->j_state_lock);
627 J_ASSERT(journal->j_free > 1);
629 blocknr = journal->j_head;
632 if (journal->j_head == journal->j_last)
633 journal->j_head = journal->j_first;
634 spin_unlock(&journal->j_state_lock);
651 if (journal->j_inode) {
652 ret =
bmap(journal->j_inode, blocknr);
659 "at offset %u on %s\n",
664 __journal_abort_soft(journal, err);
684 struct buffer_head *bh;
693 bh =
__getblk(journal->j_dev, blocknr, journal->j_blocksize);
697 memset(bh->b_data, 0, journal->j_blocksize);
698 set_buffer_uptodate(bh);
700 BUFFER_TRACE(bh,
"return this buffer");
713 static journal_t * journal_init_common (
void)
718 journal = kzalloc(
sizeof(*journal),
GFP_KERNEL);
733 journal->j_commit_interval = (
HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
736 journal->j_flags = JFS_ABORT;
774 int start,
int len,
int blocksize)
776 journal_t *journal = journal_init_common();
777 struct buffer_head *bh;
784 journal->j_blocksize = blocksize;
786 journal->j_wbufsize =
n;
788 if (!journal->j_wbuf) {
793 journal->j_dev = bdev;
794 journal->j_fs_dev = fs_dev;
795 journal->j_blk_offset =
start;
796 journal->j_maxlen = len;
798 bh =
__getblk(journal->j_dev, start, journal->j_blocksize);
801 "%s: Cannot get buffer for journal superblock\n",
805 journal->j_sb_buffer = bh;
810 kfree(journal->j_wbuf);
825 struct buffer_head *bh;
826 journal_t *journal = journal_init_common();
834 journal->j_dev = journal->j_fs_dev = inode->
i_sb->s_bdev;
835 journal->j_inode =
inode;
837 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
838 journal, inode->
i_sb->s_id, inode->
i_ino,
839 (
long long) inode->
i_size,
840 inode->
i_sb->s_blocksize_bits, inode->
i_sb->s_blocksize);
842 journal->j_maxlen = inode->
i_size >> inode->
i_sb->s_blocksize_bits;
843 journal->j_blocksize = inode->
i_sb->s_blocksize;
847 journal->j_wbufsize =
n;
849 if (!journal->j_wbuf) {
863 bh =
__getblk(journal->j_dev, blocknr, journal->j_blocksize);
866 "%s: Cannot get buffer for journal superblock\n",
870 journal->j_sb_buffer = bh;
875 kfree(journal->j_wbuf);
885 static void journal_fail_superblock (journal_t *journal)
887 struct buffer_head *bh = journal->j_sb_buffer;
889 journal->j_sb_buffer =
NULL;
899 static int journal_reset(journal_t *journal)
906 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
909 journal_fail_superblock(journal);
913 journal->j_first =
first;
914 journal->j_last = last;
916 journal->j_head =
first;
917 journal->j_tail =
first;
918 journal->j_free = last -
first;
920 journal->j_tail_sequence = journal->j_transaction_sequence;
921 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
922 journal->j_commit_request = journal->j_commit_sequence;
924 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
933 jbd_debug(1,
"JBD: Skipping superblock update on recovered sb "
934 "(start %u, seq %d, errno %d)\n",
935 journal->j_tail, journal->j_tail_sequence,
937 journal->j_flags |= JFS_FLUSHED;
948 journal->j_tail_sequence,
953 return journal_start_thread(journal);
967 struct buffer_head *bh;
971 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
974 journal_fail_superblock(journal);
978 if (journal->j_inode ==
NULL) {
983 "%s: creation of journal on external device!\n",
990 jbd_debug(1,
"JBD: Zeroing out journal blocks...\n");
991 for (i = 0; i < journal->j_maxlen; i++) {
995 bh =
__getblk(journal->j_dev, blocknr, journal->j_blocksize);
999 memset (bh->b_data, 0, journal->j_blocksize);
1000 BUFFER_TRACE(bh,
"marking dirty");
1002 BUFFER_TRACE(bh,
"marking uptodate");
1003 set_buffer_uptodate(bh);
1009 jbd_debug(1,
"JBD: journal cleared.\n");
1012 sb = journal->j_superblock;
1021 journal->j_transaction_sequence = 1;
1023 journal->j_flags &= ~JFS_ABORT;
1024 journal->j_format_version = 2;
1026 return journal_reset(journal);
1029 static void journal_write_superblock(journal_t *journal,
int write_op)
1031 struct buffer_head *bh = journal->j_sb_buffer;
1034 trace_journal_write_superblock(journal, write_op);
1035 if (!(journal->j_flags & JFS_BARRIER))
1038 if (buffer_write_io_error(bh)) {
1049 "for journal superblock update for %s.\n",
1050 journal_dev_name(journal, b));
1051 clear_buffer_write_io_error(bh);
1052 set_buffer_uptodate(bh);
1059 if (buffer_write_io_error(bh)) {
1060 clear_buffer_write_io_error(bh);
1061 set_buffer_uptodate(bh);
1067 "when updating journal superblock for %s.\n",
1068 ret, journal_dev_name(journal, b));
1083 unsigned int tail_block,
int write_op)
1087 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1088 jbd_debug(1,
"JBD: updating superblock (start %u, seq %u)\n",
1089 tail_block, tail_tid);
1094 journal_write_superblock(journal, write_op);
1097 spin_lock(&journal->j_state_lock);
1099 journal->j_flags &= ~JFS_FLUSHED;
1100 spin_unlock(&journal->j_state_lock);
1110 static void mark_journal_empty(journal_t *journal)
1114 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1115 spin_lock(&journal->j_state_lock);
1118 spin_unlock(&journal->j_state_lock);
1121 jbd_debug(1,
"JBD: Marking journal as empty (seq %d)\n",
1122 journal->j_tail_sequence);
1126 spin_unlock(&journal->j_state_lock);
1128 journal_write_superblock(journal,
WRITE_FUA);
1130 spin_lock(&journal->j_state_lock);
1132 journal->j_flags |= JFS_FLUSHED;
1133 spin_unlock(&journal->j_state_lock);
1143 static void journal_update_sb_errno(journal_t *journal)
1147 spin_lock(&journal->j_state_lock);
1148 jbd_debug(1,
"JBD: updating superblock error (errno %d)\n",
1151 spin_unlock(&journal->j_state_lock);
1153 journal_write_superblock(journal,
WRITE_SYNC);
1161 static int journal_get_superblock(journal_t *journal)
1163 struct buffer_head *bh;
1167 bh = journal->j_sb_buffer;
1169 J_ASSERT(bh !=
NULL);
1170 if (!buffer_uptodate(bh)) {
1173 if (!buffer_uptodate(bh)) {
1175 "JBD: IO error reading journal superblock\n");
1180 sb = journal->j_superblock;
1192 journal->j_format_version = 1;
1195 journal->j_format_version = 2;
1212 "JBD: Invalid start block of journal: %u\n",
1220 journal_fail_superblock(journal);
1229 static int load_superblock(journal_t *journal)
1234 err = journal_get_superblock(journal);
1238 sb = journal->j_superblock;
1263 err = load_superblock(journal);
1267 sb = journal->j_superblock;
1271 if (journal->j_format_version >= 2) {
1277 "JBD: Unrecognised features on journal\n");
1285 goto recovery_error;
1290 if (journal_reset(journal))
1291 goto recovery_error;
1293 journal->j_flags &= ~JFS_ABORT;
1294 journal->j_flags |= JFS_LOADED;
1316 journal_kill_thread(journal);
1319 if (journal->j_running_transaction)
1327 spin_lock(&journal->j_list_lock);
1328 while (journal->j_checkpoint_transactions !=
NULL) {
1329 spin_unlock(&journal->j_list_lock);
1331 spin_lock(&journal->j_list_lock);
1334 J_ASSERT(journal->j_running_transaction ==
NULL);
1335 J_ASSERT(journal->j_committing_transaction ==
NULL);
1336 J_ASSERT(journal->j_checkpoint_transactions ==
NULL);
1337 spin_unlock(&journal->j_list_lock);
1339 if (journal->j_sb_buffer) {
1340 if (!is_journal_aborted(journal)) {
1341 journal->j_tail_sequence =
1342 ++journal->j_transaction_sequence;
1343 mark_journal_empty(journal);
1346 brelse(journal->j_sb_buffer);
1350 if (journal->j_inode)
1351 iput(journal->j_inode);
1352 if (journal->j_revoke)
1354 kfree(journal->j_wbuf);
1373 unsigned long ro,
unsigned long incompat)
1377 if (!compat && !ro && !incompat)
1379 if (journal->j_format_version == 1)
1382 sb = journal->j_superblock;
1404 unsigned long ro,
unsigned long incompat)
1406 if (!compat && !ro && !incompat)
1413 if (journal->j_format_version != 2)
1437 unsigned long ro,
unsigned long incompat)
1447 jbd_debug(1,
"Setting new features 0x%lx/0x%lx/0x%lx\n",
1448 compat, ro, incompat);
1450 sb = journal->j_superblock;
1472 err = journal_get_superblock(journal);
1476 sb = journal->j_superblock;
1482 return journal_convert_superblock_v1(journal, sb);
1489 static int journal_convert_superblock_v1(journal_t *journal,
1493 struct buffer_head *bh;
1496 "JBD: Converting superblock from version 1 to 2.\n");
1505 journal->j_format_version = 2;
1507 bh = journal->j_sb_buffer;
1508 BUFFER_TRACE(bh,
"marking dirty");
1529 spin_lock(&journal->j_state_lock);
1532 if (journal->j_running_transaction) {
1533 transaction = journal->j_running_transaction;
1535 }
else if (journal->j_committing_transaction)
1536 transaction = journal->j_committing_transaction;
1542 spin_unlock(&journal->j_state_lock);
1545 spin_unlock(&journal->j_state_lock);
1549 spin_lock(&journal->j_list_lock);
1550 while (!err && journal->j_checkpoint_transactions !=
NULL) {
1551 spin_unlock(&journal->j_list_lock);
1555 spin_lock(&journal->j_list_lock);
1557 spin_unlock(&journal->j_list_lock);
1559 if (is_journal_aborted(journal))
1570 mark_journal_empty(journal);
1572 spin_lock(&journal->j_state_lock);
1573 J_ASSERT(!journal->j_running_transaction);
1574 J_ASSERT(!journal->j_committing_transaction);
1575 J_ASSERT(!journal->j_checkpoint_transactions);
1576 J_ASSERT(journal->j_head == journal->j_tail);
1577 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1578 spin_unlock(&journal->j_state_lock);
1599 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1601 err = load_superblock(journal);
1605 if (!journal->j_tail)
1609 write ?
"Clearing" :
"Ignoring");
1615 mark_journal_empty(journal);
1628 static const char *journal_dev_name(journal_t *journal,
char *
buffer)
1632 if (journal->j_inode)
1633 bdev = journal->j_inode->i_sb->s_bdev;
1635 bdev = journal->j_dev;
1653 static void __journal_abort_hard(journal_t *journal)
1658 if (journal->j_flags & JFS_ABORT)
1662 journal_dev_name(journal, b));
1664 spin_lock(&journal->j_state_lock);
1665 journal->j_flags |= JFS_ABORT;
1666 transaction = journal->j_running_transaction;
1669 spin_unlock(&journal->j_state_lock);
1674 static void __journal_abort_soft (journal_t *journal,
int errno)
1676 if (journal->j_flags & JFS_ABORT)
1679 if (!journal->j_errno)
1680 journal->j_errno = errno;
1682 __journal_abort_hard(journal);
1685 journal_update_sb_errno(journal);
1736 __journal_abort_soft(journal, errno);
1754 spin_lock(&journal->j_state_lock);
1755 if (journal->j_flags & JFS_ABORT)
1758 err = journal->j_errno;
1759 spin_unlock(&journal->j_state_lock);
1774 spin_lock(&journal->j_state_lock);
1775 if (journal->j_flags & JFS_ABORT)
1778 journal->j_errno = 0;
1779 spin_unlock(&journal->j_state_lock);
1792 spin_lock(&journal->j_state_lock);
1793 if (journal->j_errno)
1794 journal->j_flags |= JFS_ACK_ERR;
1795 spin_unlock(&journal->j_state_lock);
1806 static struct kmem_cache *journal_head_cache;
1807 #ifdef CONFIG_JBD_DEBUG
1811 static int journal_init_journal_head_cache(
void)
1815 J_ASSERT(journal_head_cache ==
NULL);
1822 if (!journal_head_cache) {
1829 static void journal_destroy_journal_head_cache(
void)
1831 if (journal_head_cache) {
1833 journal_head_cache =
NULL;
1840 static struct journal_head *journal_alloc_journal_head(
void)
1844 #ifdef CONFIG_JBD_DEBUG
1849 jbd_debug(1,
"out of memory for journal_head\n");
1853 while (ret ==
NULL) {
1861 static void journal_free_journal_head(
struct journal_head *jh)
1863 #ifdef CONFIG_JBD_DEBUG
1917 if (!buffer_jbd(bh)) {
1918 new_jh = journal_alloc_journal_head();
1919 memset(new_jh, 0,
sizeof(*new_jh));
1922 jbd_lock_bh_journal_head(bh);
1923 if (buffer_jbd(bh)) {
1928 (bh->b_page && bh->b_page->mapping));
1931 jbd_unlock_bh_journal_head(bh);
1941 BUFFER_TRACE(bh,
"added journal_head");
1944 jbd_unlock_bh_journal_head(bh);
1946 journal_free_journal_head(new_jh);
1947 return bh->b_private;
1958 jbd_lock_bh_journal_head(bh);
1959 if (buffer_jbd(bh)) {
1963 jbd_unlock_bh_journal_head(bh);
1967 static void __journal_remove_journal_head(
struct buffer_head *bh)
1971 J_ASSERT_JH(jh, jh->
b_jcount >= 0);
1976 J_ASSERT_BH(bh, buffer_jbd(bh));
1977 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1978 BUFFER_TRACE(bh,
"remove journal_head");
1987 bh->b_private =
NULL;
1989 clear_buffer_jbd(bh);
1990 journal_free_journal_head(jh);
1999 struct buffer_head *bh = jh2bh(jh);
2001 jbd_lock_bh_journal_head(bh);
2005 __journal_remove_journal_head(bh);
2006 jbd_unlock_bh_journal_head(bh);
2009 jbd_unlock_bh_journal_head(bh);
2015 #ifdef CONFIG_JBD_DEBUG
2020 static struct dentry *jbd_debugfs_dir;
2023 static void __init jbd_create_debugfs_entry(
void)
2026 if (jbd_debugfs_dir)
2029 &journal_enable_debug);
2032 static void __exit jbd_remove_debugfs_entry(
void)
2040 static inline void jbd_create_debugfs_entry(
void)
2044 static inline void jbd_remove_debugfs_entry(
void)
2052 static int __init journal_init_handle_cache(
void)
2059 if (jbd_handle_cache ==
NULL) {
2066 static void journal_destroy_handle_cache(
void)
2068 if (jbd_handle_cache)
2076 static int __init journal_init_caches(
void)
2082 ret = journal_init_journal_head_cache();
2084 ret = journal_init_handle_cache();
2088 static void journal_destroy_caches(
void)
2091 journal_destroy_journal_head_cache();
2092 journal_destroy_handle_cache();
2095 static int __init journal_init(
void)
2101 ret = journal_init_caches();
2103 journal_destroy_caches();
2104 jbd_create_debugfs_entry();
2108 static void __exit journal_exit(
void)
2110 #ifdef CONFIG_JBD_DEBUG
2115 jbd_remove_debugfs_entry();
2116 journal_destroy_caches();