journal.c (2612e3bbc0386368a850140a6c9b990cd496a5ec) journal.c (29a511e49f33426c8d24700db4842234a84678b2)
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * linux/fs/jbd2/journal.c
4 *
5 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 *
7 * Copyright 1998 Red Hat corp --- All Rights Reserved
8 *

--- 1323 unchanged lines hidden (view full) ---

1332
1333 count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count);
1334 trace_jbd2_shrink_count(journal, sc->nr_to_scan, count);
1335
1336 return count;
1337}
1338
1339/*
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * linux/fs/jbd2/journal.c
4 *
5 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 *
7 * Copyright 1998 Red Hat corp --- All Rights Reserved
8 *

--- 1323 unchanged lines hidden (view full) ---

1332
1333 count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count);
1334 trace_jbd2_shrink_count(journal, sc->nr_to_scan, count);
1335
1336 return count;
1337}
1338
1339/*
1340 * If the journal init or create aborts, we need to mark the journal
1341 * superblock as being NULL to prevent the journal destroy from writing
1342 * back a bogus superblock.
1343 */
1344static void journal_fail_superblock(journal_t *journal)
1345{
1346 struct buffer_head *bh = journal->j_sb_buffer;
1347 brelse(bh);
1348 journal->j_sb_buffer = NULL;
1349}
1350
1351/*
1352 * Read the superblock for a given journal, performing initial
1353 * validation of the format.
1354 */
1355static int journal_get_superblock(journal_t *journal)
1356{
1357 struct buffer_head *bh;
1358 journal_superblock_t *sb;
1359 int err;
1360
1361 bh = journal->j_sb_buffer;
1362
1363 J_ASSERT(bh != NULL);
1364 if (buffer_verified(bh))
1365 return 0;
1366
1367 err = bh_read(bh, 0);
1368 if (err < 0) {
1369 printk(KERN_ERR
1370 "JBD2: IO error reading journal superblock\n");
1371 goto out;
1372 }
1373
1374 sb = journal->j_superblock;
1375
1376 err = -EINVAL;
1377
1378 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1379 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1380 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1381 goto out;
1382 }
1383
1384 if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 &&
1385 be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) {
1386 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1387 goto out;
1388 }
1389
1390 if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
1391 printk(KERN_WARNING "JBD2: journal file too short\n");
1392 goto out;
1393 }
1394
1395 if (be32_to_cpu(sb->s_first) == 0 ||
1396 be32_to_cpu(sb->s_first) >= journal->j_total_len) {
1397 printk(KERN_WARNING
1398 "JBD2: Invalid start block of journal: %u\n",
1399 be32_to_cpu(sb->s_first));
1400 goto out;
1401 }
1402
1403 if (jbd2_has_feature_csum2(journal) &&
1404 jbd2_has_feature_csum3(journal)) {
1405 /* Can't have checksum v2 and v3 at the same time! */
1406 printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
1407 "at the same time!\n");
1408 goto out;
1409 }
1410
1411 if (jbd2_journal_has_csum_v2or3_feature(journal) &&
1412 jbd2_has_feature_checksum(journal)) {
1413 /* Can't have checksum v1 and v2 on at the same time! */
1414 printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
1415 "at the same time!\n");
1416 goto out;
1417 }
1418
1419 if (!jbd2_verify_csum_type(journal, sb)) {
1420 printk(KERN_ERR "JBD2: Unknown checksum type\n");
1421 goto out;
1422 }
1423
1424 /* Load the checksum driver */
1425 if (jbd2_journal_has_csum_v2or3_feature(journal)) {
1426 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1427 if (IS_ERR(journal->j_chksum_driver)) {
1428 printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
1429 err = PTR_ERR(journal->j_chksum_driver);
1430 journal->j_chksum_driver = NULL;
1431 goto out;
1432 }
1433 /* Check superblock checksum */
1434 if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
1435 printk(KERN_ERR "JBD2: journal checksum error\n");
1436 err = -EFSBADCRC;
1437 goto out;
1438 }
1439 }
1440 set_buffer_verified(bh);
1441 return 0;
1442
1443out:
1444 journal_fail_superblock(journal);
1445 return err;
1446}
1447
1448static int journal_revoke_records_per_block(journal_t *journal)
1449{
1450 int record_size;
1451 int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t);
1452
1453 if (jbd2_has_feature_64bit(journal))
1454 record_size = 8;
1455 else
1456 record_size = 4;
1457
1458 if (jbd2_journal_has_csum_v2or3(journal))
1459 space -= sizeof(struct jbd2_journal_block_tail);
1460 return space / record_size;
1461}
1462
1463/*
1464 * Load the on-disk journal superblock and read the key fields into the
1465 * journal_t.
1466 */
1467static int load_superblock(journal_t *journal)
1468{
1469 int err;
1470 journal_superblock_t *sb;
1471 int num_fc_blocks;
1472
1473 err = journal_get_superblock(journal);
1474 if (err)
1475 return err;
1476
1477 sb = journal->j_superblock;
1478
1479 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1480 journal->j_tail = be32_to_cpu(sb->s_start);
1481 journal->j_first = be32_to_cpu(sb->s_first);
1482 journal->j_errno = be32_to_cpu(sb->s_errno);
1483 journal->j_last = be32_to_cpu(sb->s_maxlen);
1484
1485 if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len)
1486 journal->j_total_len = be32_to_cpu(sb->s_maxlen);
1487 /* Precompute checksum seed for all metadata */
1488 if (jbd2_journal_has_csum_v2or3(journal))
1489 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1490 sizeof(sb->s_uuid));
1491 journal->j_revoke_records_per_block =
1492 journal_revoke_records_per_block(journal);
1493
1494 if (jbd2_has_feature_fast_commit(journal)) {
1495 journal->j_fc_last = be32_to_cpu(sb->s_maxlen);
1496 num_fc_blocks = jbd2_journal_get_num_fc_blks(sb);
1497 if (journal->j_last - num_fc_blocks >= JBD2_MIN_JOURNAL_BLOCKS)
1498 journal->j_last = journal->j_fc_last - num_fc_blocks;
1499 journal->j_fc_first = journal->j_last + 1;
1500 journal->j_fc_off = 0;
1501 }
1502
1503 return 0;
1504}
1505
1506
1507/*
1340 * Management for journal control blocks: functions to create and
1341 * destroy journal_t structures, and to initialise and read existing
1342 * journal blocks from disk. */
1343
1344/* First: create and setup a journal_t object in memory. We initialise
1345 * very few fields yet: that has to wait until we have created the
1346 * journal structures from from scratch, or loaded them from disk. */
1347

--- 169 unchanged lines hidden (view full) ---

1517 "%pg-%lu", journal->j_dev, journal->j_inode->i_ino);
1518 strreplace(journal->j_devname, '/', '!');
1519 jbd2_stats_proc_init(journal);
1520
1521 return journal;
1522}
1523
1524/*
1508 * Management for journal control blocks: functions to create and
1509 * destroy journal_t structures, and to initialise and read existing
1510 * journal blocks from disk. */
1511
1512/* First: create and setup a journal_t object in memory. We initialise
1513 * very few fields yet: that has to wait until we have created the
1514 * journal structures from from scratch, or loaded them from disk. */
1515

--- 169 unchanged lines hidden (view full) ---

1685 "%pg-%lu", journal->j_dev, journal->j_inode->i_ino);
1686 strreplace(journal->j_devname, '/', '!');
1687 jbd2_stats_proc_init(journal);
1688
1689 return journal;
1690}
1691
1692/*
1525 * If the journal init or create aborts, we need to mark the journal
1526 * superblock as being NULL to prevent the journal destroy from writing
1527 * back a bogus superblock.
1528 */
1529static void journal_fail_superblock(journal_t *journal)
1530{
1531 struct buffer_head *bh = journal->j_sb_buffer;
1532 brelse(bh);
1533 journal->j_sb_buffer = NULL;
1534}
1535
1536/*
1537 * Given a journal_t structure, initialise the various fields for
1538 * startup of a new journaling session. We use this both when creating
1539 * a journal, and after recovering an old journal to reset it for
1540 * subsequent use.
1541 */
1542
1543static int journal_reset(journal_t *journal)
1544{

--- 339 unchanged lines hidden (view full) ---

1884 errcode = 0;
1885 jbd2_debug(1, "JBD2: updating superblock error (errno %d)\n", errcode);
1886 sb->s_errno = cpu_to_be32(errcode);
1887
1888 jbd2_write_superblock(journal, REQ_SYNC | REQ_FUA);
1889}
1890EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
1891
1693 * Given a journal_t structure, initialise the various fields for
1694 * startup of a new journaling session. We use this both when creating
1695 * a journal, and after recovering an old journal to reset it for
1696 * subsequent use.
1697 */
1698
1699static int journal_reset(journal_t *journal)
1700{

--- 339 unchanged lines hidden (view full) ---

2040 errcode = 0;
2041 jbd2_debug(1, "JBD2: updating superblock error (errno %d)\n", errcode);
2042 sb->s_errno = cpu_to_be32(errcode);
2043
2044 jbd2_write_superblock(journal, REQ_SYNC | REQ_FUA);
2045}
2046EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
2047
1892static int journal_revoke_records_per_block(journal_t *journal)
1893{
1894 int record_size;
1895 int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t);
1896
1897 if (jbd2_has_feature_64bit(journal))
1898 record_size = 8;
1899 else
1900 record_size = 4;
1901
1902 if (jbd2_journal_has_csum_v2or3(journal))
1903 space -= sizeof(struct jbd2_journal_block_tail);
1904 return space / record_size;
1905}
1906
1907/*
1908 * Read the superblock for a given journal, performing initial
1909 * validation of the format.
1910 */
1911static int journal_get_superblock(journal_t *journal)
1912{
1913 struct buffer_head *bh;
1914 journal_superblock_t *sb;
1915 int err;
1916
1917 bh = journal->j_sb_buffer;
1918
1919 J_ASSERT(bh != NULL);
1920 if (buffer_verified(bh))
1921 return 0;
1922
1923 err = bh_read(bh, 0);
1924 if (err < 0) {
1925 printk(KERN_ERR
1926 "JBD2: IO error reading journal superblock\n");
1927 goto out;
1928 }
1929
1930 sb = journal->j_superblock;
1931
1932 err = -EINVAL;
1933
1934 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1935 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1936 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1937 goto out;
1938 }
1939
1940 if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 &&
1941 be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) {
1942 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1943 goto out;
1944 }
1945
1946 if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
1947 printk(KERN_WARNING "JBD2: journal file too short\n");
1948 goto out;
1949 }
1950
1951 if (be32_to_cpu(sb->s_first) == 0 ||
1952 be32_to_cpu(sb->s_first) >= journal->j_total_len) {
1953 printk(KERN_WARNING
1954 "JBD2: Invalid start block of journal: %u\n",
1955 be32_to_cpu(sb->s_first));
1956 goto out;
1957 }
1958
1959 if (jbd2_has_feature_csum2(journal) &&
1960 jbd2_has_feature_csum3(journal)) {
1961 /* Can't have checksum v2 and v3 at the same time! */
1962 printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
1963 "at the same time!\n");
1964 goto out;
1965 }
1966
1967 if (jbd2_journal_has_csum_v2or3_feature(journal) &&
1968 jbd2_has_feature_checksum(journal)) {
1969 /* Can't have checksum v1 and v2 on at the same time! */
1970 printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
1971 "at the same time!\n");
1972 goto out;
1973 }
1974
1975 if (!jbd2_verify_csum_type(journal, sb)) {
1976 printk(KERN_ERR "JBD2: Unknown checksum type\n");
1977 goto out;
1978 }
1979
1980 /* Load the checksum driver */
1981 if (jbd2_journal_has_csum_v2or3_feature(journal)) {
1982 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1983 if (IS_ERR(journal->j_chksum_driver)) {
1984 printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
1985 err = PTR_ERR(journal->j_chksum_driver);
1986 journal->j_chksum_driver = NULL;
1987 goto out;
1988 }
1989 /* Check superblock checksum */
1990 if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
1991 printk(KERN_ERR "JBD2: journal checksum error\n");
1992 err = -EFSBADCRC;
1993 goto out;
1994 }
1995 }
1996 set_buffer_verified(bh);
1997 return 0;
1998
1999out:
2000 journal_fail_superblock(journal);
2001 return err;
2002}
2003
2004/*
2005 * Load the on-disk journal superblock and read the key fields into the
2006 * journal_t.
2007 */
2008
2009static int load_superblock(journal_t *journal)
2010{
2011 int err;
2012 journal_superblock_t *sb;
2013 int num_fc_blocks;
2014
2015 err = journal_get_superblock(journal);
2016 if (err)
2017 return err;
2018
2019 sb = journal->j_superblock;
2020
2021 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
2022 journal->j_tail = be32_to_cpu(sb->s_start);
2023 journal->j_first = be32_to_cpu(sb->s_first);
2024 journal->j_errno = be32_to_cpu(sb->s_errno);
2025 journal->j_last = be32_to_cpu(sb->s_maxlen);
2026
2027 if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len)
2028 journal->j_total_len = be32_to_cpu(sb->s_maxlen);
2029 /* Precompute checksum seed for all metadata */
2030 if (jbd2_journal_has_csum_v2or3(journal))
2031 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
2032 sizeof(sb->s_uuid));
2033 journal->j_revoke_records_per_block =
2034 journal_revoke_records_per_block(journal);
2035
2036 if (jbd2_has_feature_fast_commit(journal)) {
2037 journal->j_fc_last = be32_to_cpu(sb->s_maxlen);
2038 num_fc_blocks = jbd2_journal_get_num_fc_blks(sb);
2039 if (journal->j_last - num_fc_blocks >= JBD2_MIN_JOURNAL_BLOCKS)
2040 journal->j_last = journal->j_fc_last - num_fc_blocks;
2041 journal->j_fc_first = journal->j_last + 1;
2042 journal->j_fc_off = 0;
2043 }
2044
2045 return 0;
2046}
2047
2048
2049/**
2050 * jbd2_journal_load() - Read journal from disk.
2051 * @journal: Journal to act on.
2052 *
2053 * Given a journal_t structure which tells us which disk blocks contain
2054 * a journal, read the journal from disk to initialise the in-memory
2055 * structures.
2056 */

--- 27 unchanged lines hidden (view full) ---

2084 * Create a slab for this blocksize
2085 */
2086 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
2087 if (err)
2088 return err;
2089
2090 /* Let the recovery code check whether it needs to recover any
2091 * data from the journal. */
2048/**
2049 * jbd2_journal_load() - Read journal from disk.
2050 * @journal: Journal to act on.
2051 *
2052 * Given a journal_t structure which tells us which disk blocks contain
2053 * a journal, read the journal from disk to initialise the in-memory
2054 * structures.
2055 */

--- 27 unchanged lines hidden (view full) ---

2083 * Create a slab for this blocksize
2084 */
2085 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
2086 if (err)
2087 return err;
2088
2089 /* Let the recovery code check whether it needs to recover any
2090 * data from the journal. */
2092 if (jbd2_journal_recover(journal))
2093 goto recovery_error;
2091 err = jbd2_journal_recover(journal);
2092 if (err) {
2093 pr_warn("JBD2: journal recovery failed\n");
2094 return err;
2095 }
2094
2095 if (journal->j_failed_commit) {
2096 printk(KERN_ERR "JBD2: journal transaction %u on %s "
2097 "is corrupt.\n", journal->j_failed_commit,
2098 journal->j_devname);
2099 return -EFSCORRUPTED;
2100 }
2101 /*
2102 * clear JBD2_ABORT flag initialized in journal_init_common
2103 * here to update log tail information with the newest seq.
2104 */
2105 journal->j_flags &= ~JBD2_ABORT;
2106
2107 /* OK, we've finished with the dynamic journal bits:
2108 * reinitialise the dynamic contents of the superblock in memory
2109 * and reset them on disk. */
2096
2097 if (journal->j_failed_commit) {
2098 printk(KERN_ERR "JBD2: journal transaction %u on %s "
2099 "is corrupt.\n", journal->j_failed_commit,
2100 journal->j_devname);
2101 return -EFSCORRUPTED;
2102 }
2103 /*
2104 * clear JBD2_ABORT flag initialized in journal_init_common
2105 * here to update log tail information with the newest seq.
2106 */
2107 journal->j_flags &= ~JBD2_ABORT;
2108
2109 /* OK, we've finished with the dynamic journal bits:
2110 * reinitialise the dynamic contents of the superblock in memory
2111 * and reset them on disk. */
2110 if (journal_reset(journal))
2111 goto recovery_error;
2112 err = journal_reset(journal);
2113 if (err) {
2114 pr_warn("JBD2: journal reset failed\n");
2115 return err;
2116 }
2112
2113 journal->j_flags |= JBD2_LOADED;
2114 return 0;
2117
2118 journal->j_flags |= JBD2_LOADED;
2119 return 0;
2115
2116recovery_error:
2117 printk(KERN_WARNING "JBD2: recovery failed\n");
2118 return -EIO;
2119}
2120
2121/**
2122 * jbd2_journal_destroy() - Release a journal_t structure.
2123 * @journal: Journal to act on.
2124 *
2125 * Release a journal_t structure once it is no longer in use by the
2126 * journaled object.

--- 1074 unchanged lines hidden ---
2120}
2121
2122/**
2123 * jbd2_journal_destroy() - Release a journal_t structure.
2124 * @journal: Journal to act on.
2125 *
2126 * Release a journal_t structure once it is no longer in use by the
2127 * journaled object.

--- 1074 unchanged lines hidden ---