Skip to content

Commit fb86acc

Browse files
harshadjsgregkh
authored andcommitted
ext4: fix fast commit alignment issues
commit a7ba36b upstream. Fast commit recovery data on disk may not be aligned. So, when the recovery code reads it, this patch makes sure that fast commit info found on-disk is first memcpy-ed into an aligned variable before accessing it. As a consequence of it, we also remove some macros that could resulted in unaligned accesses. Cc: stable@kernel.org Fixes: 8016e29 ("ext4: fast commit recovery path") Signed-off-by: Harshad Shirwadkar <harshadshirwadkar@gmail.com> Link: https://lore.kernel.org/r/20210519215920.2037527-1-harshads@google.com Signed-off-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d3b668b commit fb86acc

2 files changed

Lines changed: 90 additions & 99 deletions

File tree

‎fs/ext4/fast_commit.c‎

Lines changed: 90 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,47 +1227,36 @@ static void ext4_fc_cleanup(journal_t *journal, int full)
12271227

12281228
/* Ext4 Replay Path Routines */
12291229

1230-
/* Get length of a particular tlv */
1231-
static inline int ext4_fc_tag_len(struct ext4_fc_tl *tl)
1232-
{
1233-
return le16_to_cpu(tl->fc_len);
1234-
}
1235-
1236-
/* Get a pointer to "value" of a tlv */
1237-
static inline u8 *ext4_fc_tag_val(struct ext4_fc_tl *tl)
1238-
{
1239-
return (u8 *)tl + sizeof(*tl);
1240-
}
1241-
12421230
/* Helper struct for dentry replay routines */
12431231
struct dentry_info_args {
12441232
int parent_ino, dname_len, ino, inode_len;
12451233
char *dname;
12461234
};
12471235

12481236
static inline void tl_to_darg(struct dentry_info_args *darg,
1249-
struct ext4_fc_tl *tl)
1237+
struct ext4_fc_tl *tl, u8 *val)
12501238
{
1251-
struct ext4_fc_dentry_info *fcd;
1239+
struct ext4_fc_dentry_info fcd;
12521240

1253-
fcd = (struct ext4_fc_dentry_info *)ext4_fc_tag_val(tl);
1241+
memcpy(&fcd, val, sizeof(fcd));
12541242

1255-
darg->parent_ino = le32_to_cpu(fcd->fc_parent_ino);
1256-
darg->ino = le32_to_cpu(fcd->fc_ino);
1257-
darg->dname = fcd->fc_dname;
1258-
darg->dname_len = ext4_fc_tag_len(tl) -
1259-
sizeof(struct ext4_fc_dentry_info);
1243+
darg->parent_ino = le32_to_cpu(fcd.fc_parent_ino);
1244+
darg->ino = le32_to_cpu(fcd.fc_ino);
1245+
darg->dname = val + offsetof(struct ext4_fc_dentry_info, fc_dname);
1246+
darg->dname_len = le16_to_cpu(tl->fc_len) -
1247+
sizeof(struct ext4_fc_dentry_info);
12601248
}
12611249

12621250
/* Unlink replay function */
1263-
static int ext4_fc_replay_unlink(struct super_block *sb, struct ext4_fc_tl *tl)
1251+
static int ext4_fc_replay_unlink(struct super_block *sb, struct ext4_fc_tl *tl,
1252+
u8 *val)
12641253
{
12651254
struct inode *inode, *old_parent;
12661255
struct qstr entry;
12671256
struct dentry_info_args darg;
12681257
int ret = 0;
12691258

1270-
tl_to_darg(&darg, tl);
1259+
tl_to_darg(&darg, tl, val);
12711260

12721261
trace_ext4_fc_replay(sb, EXT4_FC_TAG_UNLINK, darg.ino,
12731262
darg.parent_ino, darg.dname_len);
@@ -1357,13 +1346,14 @@ static int ext4_fc_replay_link_internal(struct super_block *sb,
13571346
}
13581347

13591348
/* Link replay function */
1360-
static int ext4_fc_replay_link(struct super_block *sb, struct ext4_fc_tl *tl)
1349+
static int ext4_fc_replay_link(struct super_block *sb, struct ext4_fc_tl *tl,
1350+
u8 *val)
13611351
{
13621352
struct inode *inode;
13631353
struct dentry_info_args darg;
13641354
int ret = 0;
13651355

1366-
tl_to_darg(&darg, tl);
1356+
tl_to_darg(&darg, tl, val);
13671357
trace_ext4_fc_replay(sb, EXT4_FC_TAG_LINK, darg.ino,
13681358
darg.parent_ino, darg.dname_len);
13691359

@@ -1408,19 +1398,20 @@ static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)
14081398
/*
14091399
* Inode replay function
14101400
*/
1411-
static int ext4_fc_replay_inode(struct super_block *sb, struct ext4_fc_tl *tl)
1401+
static int ext4_fc_replay_inode(struct super_block *sb, struct ext4_fc_tl *tl,
1402+
u8 *val)
14121403
{
1413-
struct ext4_fc_inode *fc_inode;
1404+
struct ext4_fc_inode fc_inode;
14141405
struct ext4_inode *raw_inode;
14151406
struct ext4_inode *raw_fc_inode;
14161407
struct inode *inode = NULL;
14171408
struct ext4_iloc iloc;
14181409
int inode_len, ino, ret, tag = le16_to_cpu(tl->fc_tag);
14191410
struct ext4_extent_header *eh;
14201411

1421-
fc_inode = (struct ext4_fc_inode *)ext4_fc_tag_val(tl);
1412+
memcpy(&fc_inode, val, sizeof(fc_inode));
14221413

1423-
ino = le32_to_cpu(fc_inode->fc_ino);
1414+
ino = le32_to_cpu(fc_inode.fc_ino);
14241415
trace_ext4_fc_replay(sb, tag, ino, 0, 0);
14251416

14261417
inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
@@ -1432,12 +1423,13 @@ static int ext4_fc_replay_inode(struct super_block *sb, struct ext4_fc_tl *tl)
14321423

14331424
ext4_fc_record_modified_inode(sb, ino);
14341425

1435-
raw_fc_inode = (struct ext4_inode *)fc_inode->fc_raw_inode;
1426+
raw_fc_inode = (struct ext4_inode *)
1427+
(val + offsetof(struct ext4_fc_inode, fc_raw_inode));
14361428
ret = ext4_get_fc_inode_loc(sb, ino, &iloc);
14371429
if (ret)
14381430
goto out;
14391431

1440-
inode_len = ext4_fc_tag_len(tl) - sizeof(struct ext4_fc_inode);
1432+
inode_len = le16_to_cpu(tl->fc_len) - sizeof(struct ext4_fc_inode);
14411433
raw_inode = ext4_raw_inode(&iloc);
14421434

14431435
memcpy(raw_inode, raw_fc_inode, offsetof(struct ext4_inode, i_block));
@@ -1505,14 +1497,15 @@ static int ext4_fc_replay_inode(struct super_block *sb, struct ext4_fc_tl *tl)
15051497
* inode for which we are trying to create a dentry here, should already have
15061498
* been replayed before we start here.
15071499
*/
1508-
static int ext4_fc_replay_create(struct super_block *sb, struct ext4_fc_tl *tl)
1500+
static int ext4_fc_replay_create(struct super_block *sb, struct ext4_fc_tl *tl,
1501+
u8 *val)
15091502
{
15101503
int ret = 0;
15111504
struct inode *inode = NULL;
15121505
struct inode *dir = NULL;
15131506
struct dentry_info_args darg;
15141507

1515-
tl_to_darg(&darg, tl);
1508+
tl_to_darg(&darg, tl, val);
15161509

15171510
trace_ext4_fc_replay(sb, EXT4_FC_TAG_CREAT, darg.ino,
15181511
darg.parent_ino, darg.dname_len);
@@ -1591,9 +1584,9 @@ static int ext4_fc_record_regions(struct super_block *sb, int ino,
15911584

15921585
/* Replay add range tag */
15931586
static int ext4_fc_replay_add_range(struct super_block *sb,
1594-
struct ext4_fc_tl *tl)
1587+
struct ext4_fc_tl *tl, u8 *val)
15951588
{
1596-
struct ext4_fc_add_range *fc_add_ex;
1589+
struct ext4_fc_add_range fc_add_ex;
15971590
struct ext4_extent newex, *ex;
15981591
struct inode *inode;
15991592
ext4_lblk_t start, cur;
@@ -1603,15 +1596,14 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
16031596
struct ext4_ext_path *path = NULL;
16041597
int ret;
16051598

1606-
fc_add_ex = (struct ext4_fc_add_range *)ext4_fc_tag_val(tl);
1607-
ex = (struct ext4_extent *)&fc_add_ex->fc_ex;
1599+
memcpy(&fc_add_ex, val, sizeof(fc_add_ex));
1600+
ex = (struct ext4_extent *)&fc_add_ex.fc_ex;
16081601

16091602
trace_ext4_fc_replay(sb, EXT4_FC_TAG_ADD_RANGE,
1610-
le32_to_cpu(fc_add_ex->fc_ino), le32_to_cpu(ex->ee_block),
1603+
le32_to_cpu(fc_add_ex.fc_ino), le32_to_cpu(ex->ee_block),
16111604
ext4_ext_get_actual_len(ex));
16121605

1613-
inode = ext4_iget(sb, le32_to_cpu(fc_add_ex->fc_ino),
1614-
EXT4_IGET_NORMAL);
1606+
inode = ext4_iget(sb, le32_to_cpu(fc_add_ex.fc_ino), EXT4_IGET_NORMAL);
16151607
if (IS_ERR(inode)) {
16161608
jbd_debug(1, "Inode not found.");
16171609
return 0;
@@ -1720,32 +1712,33 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
17201712

17211713
/* Replay DEL_RANGE tag */
17221714
static int
1723-
ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl)
1715+
ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl,
1716+
u8 *val)
17241717
{
17251718
struct inode *inode;
1726-
struct ext4_fc_del_range *lrange;
1719+
struct ext4_fc_del_range lrange;
17271720
struct ext4_map_blocks map;
17281721
ext4_lblk_t cur, remaining;
17291722
int ret;
17301723

1731-
lrange = (struct ext4_fc_del_range *)ext4_fc_tag_val(tl);
1732-
cur = le32_to_cpu(lrange->fc_lblk);
1733-
remaining = le32_to_cpu(lrange->fc_len);
1724+
memcpy(&lrange, val, sizeof(lrange));
1725+
cur = le32_to_cpu(lrange.fc_lblk);
1726+
remaining = le32_to_cpu(lrange.fc_len);
17341727

17351728
trace_ext4_fc_replay(sb, EXT4_FC_TAG_DEL_RANGE,
1736-
le32_to_cpu(lrange->fc_ino), cur, remaining);
1729+
le32_to_cpu(lrange.fc_ino), cur, remaining);
17371730

1738-
inode = ext4_iget(sb, le32_to_cpu(lrange->fc_ino), EXT4_IGET_NORMAL);
1731+
inode = ext4_iget(sb, le32_to_cpu(lrange.fc_ino), EXT4_IGET_NORMAL);
17391732
if (IS_ERR(inode)) {
1740-
jbd_debug(1, "Inode %d not found", le32_to_cpu(lrange->fc_ino));
1733+
jbd_debug(1, "Inode %d not found", le32_to_cpu(lrange.fc_ino));
17411734
return 0;
17421735
}
17431736

17441737
ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
17451738

17461739
jbd_debug(1, "DEL_RANGE, inode %ld, lblk %d, len %d\n",
1747-
inode->i_ino, le32_to_cpu(lrange->fc_lblk),
1748-
le32_to_cpu(lrange->fc_len));
1740+
inode->i_ino, le32_to_cpu(lrange.fc_lblk),
1741+
le32_to_cpu(lrange.fc_len));
17491742
while (remaining > 0) {
17501743
map.m_lblk = cur;
17511744
map.m_len = remaining;
@@ -1766,8 +1759,8 @@ ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl)
17661759
}
17671760

17681761
ret = ext4_punch_hole(inode,
1769-
le32_to_cpu(lrange->fc_lblk) << sb->s_blocksize_bits,
1770-
le32_to_cpu(lrange->fc_len) << sb->s_blocksize_bits);
1762+
le32_to_cpu(lrange.fc_lblk) << sb->s_blocksize_bits,
1763+
le32_to_cpu(lrange.fc_len) << sb->s_blocksize_bits);
17711764
if (ret)
17721765
jbd_debug(1, "ext4_punch_hole returned %d", ret);
17731766
ext4_ext_replay_shrink_inode(inode,
@@ -1909,11 +1902,11 @@ static int ext4_fc_replay_scan(journal_t *journal,
19091902
struct ext4_sb_info *sbi = EXT4_SB(sb);
19101903
struct ext4_fc_replay_state *state;
19111904
int ret = JBD2_FC_REPLAY_CONTINUE;
1912-
struct ext4_fc_add_range *ext;
1913-
struct ext4_fc_tl *tl;
1914-
struct ext4_fc_tail *tail;
1915-
__u8 *start, *end;
1916-
struct ext4_fc_head *head;
1905+
struct ext4_fc_add_range ext;
1906+
struct ext4_fc_tl tl;
1907+
struct ext4_fc_tail tail;
1908+
__u8 *start, *end, *cur, *val;
1909+
struct ext4_fc_head head;
19171910
struct ext4_extent *ex;
19181911

19191912
state = &sbi->s_fc_replay_state;
@@ -1940,15 +1933,17 @@ static int ext4_fc_replay_scan(journal_t *journal,
19401933
}
19411934

19421935
state->fc_replay_expected_off++;
1943-
fc_for_each_tl(start, end, tl) {
1936+
for (cur = start; cur < end; cur = cur + sizeof(tl) + le16_to_cpu(tl.fc_len)) {
1937+
memcpy(&tl, cur, sizeof(tl));
1938+
val = cur + sizeof(tl);
19441939
jbd_debug(3, "Scan phase, tag:%s, blk %lld\n",
1945-
tag2str(le16_to_cpu(tl->fc_tag)), bh->b_blocknr);
1946-
switch (le16_to_cpu(tl->fc_tag)) {
1940+
tag2str(le16_to_cpu(tl.fc_tag)), bh->b_blocknr);
1941+
switch (le16_to_cpu(tl.fc_tag)) {
19471942
case EXT4_FC_TAG_ADD_RANGE:
1948-
ext = (struct ext4_fc_add_range *)ext4_fc_tag_val(tl);
1949-
ex = (struct ext4_extent *)&ext->fc_ex;
1943+
memcpy(&ext, val, sizeof(ext));
1944+
ex = (struct ext4_extent *)&ext.fc_ex;
19501945
ret = ext4_fc_record_regions(sb,
1951-
le32_to_cpu(ext->fc_ino),
1946+
le32_to_cpu(ext.fc_ino),
19521947
le32_to_cpu(ex->ee_block), ext4_ext_pblock(ex),
19531948
ext4_ext_get_actual_len(ex));
19541949
if (ret < 0)
@@ -1962,18 +1957,18 @@ static int ext4_fc_replay_scan(journal_t *journal,
19621957
case EXT4_FC_TAG_INODE:
19631958
case EXT4_FC_TAG_PAD:
19641959
state->fc_cur_tag++;
1965-
state->fc_crc = ext4_chksum(sbi, state->fc_crc, tl,
1966-
sizeof(*tl) + ext4_fc_tag_len(tl));
1960+
state->fc_crc = ext4_chksum(sbi, state->fc_crc, cur,
1961+
sizeof(tl) + le16_to_cpu(tl.fc_len));
19671962
break;
19681963
case EXT4_FC_TAG_TAIL:
19691964
state->fc_cur_tag++;
1970-
tail = (struct ext4_fc_tail *)ext4_fc_tag_val(tl);
1971-
state->fc_crc = ext4_chksum(sbi, state->fc_crc, tl,
1972-
sizeof(*tl) +
1965+
memcpy(&tail, val, sizeof(tail));
1966+
state->fc_crc = ext4_chksum(sbi, state->fc_crc, cur,
1967+
sizeof(tl) +
19731968
offsetof(struct ext4_fc_tail,
19741969
fc_crc));
1975-
if (le32_to_cpu(tail->fc_tid) == expected_tid &&
1976-
le32_to_cpu(tail->fc_crc) == state->fc_crc) {
1970+
if (le32_to_cpu(tail.fc_tid) == expected_tid &&
1971+
le32_to_cpu(tail.fc_crc) == state->fc_crc) {
19771972
state->fc_replay_num_tags = state->fc_cur_tag;
19781973
state->fc_regions_valid =
19791974
state->fc_regions_used;
@@ -1984,19 +1979,19 @@ static int ext4_fc_replay_scan(journal_t *journal,
19841979
state->fc_crc = 0;
19851980
break;
19861981
case EXT4_FC_TAG_HEAD:
1987-
head = (struct ext4_fc_head *)ext4_fc_tag_val(tl);
1988-
if (le32_to_cpu(head->fc_features) &
1982+
memcpy(&head, val, sizeof(head));
1983+
if (le32_to_cpu(head.fc_features) &
19891984
~EXT4_FC_SUPPORTED_FEATURES) {
19901985
ret = -EOPNOTSUPP;
19911986
break;
19921987
}
1993-
if (le32_to_cpu(head->fc_tid) != expected_tid) {
1988+
if (le32_to_cpu(head.fc_tid) != expected_tid) {
19941989
ret = JBD2_FC_REPLAY_STOP;
19951990
break;
19961991
}
19971992
state->fc_cur_tag++;
1998-
state->fc_crc = ext4_chksum(sbi, state->fc_crc, tl,
1999-
sizeof(*tl) + ext4_fc_tag_len(tl));
1993+
state->fc_crc = ext4_chksum(sbi, state->fc_crc, cur,
1994+
sizeof(tl) + le16_to_cpu(tl.fc_len));
20001995
break;
20011996
default:
20021997
ret = state->fc_replay_num_tags ?
@@ -2020,11 +2015,11 @@ static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
20202015
{
20212016
struct super_block *sb = journal->j_private;
20222017
struct ext4_sb_info *sbi = EXT4_SB(sb);
2023-
struct ext4_fc_tl *tl;
2024-
__u8 *start, *end;
2018+
struct ext4_fc_tl tl;
2019+
__u8 *start, *end, *cur, *val;
20252020
int ret = JBD2_FC_REPLAY_CONTINUE;
20262021
struct ext4_fc_replay_state *state = &sbi->s_fc_replay_state;
2027-
struct ext4_fc_tail *tail;
2022+
struct ext4_fc_tail tail;
20282023

20292024
if (pass == PASS_SCAN) {
20302025
state->fc_current_pass = PASS_SCAN;
@@ -2051,49 +2046,52 @@ static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
20512046
start = (u8 *)bh->b_data;
20522047
end = (__u8 *)bh->b_data + journal->j_blocksize - 1;
20532048

2054-
fc_for_each_tl(start, end, tl) {
2049+
for (cur = start; cur < end; cur = cur + sizeof(tl) + le16_to_cpu(tl.fc_len)) {
2050+
memcpy(&tl, cur, sizeof(tl));
2051+
val = cur + sizeof(tl);
2052+
20552053
if (state->fc_replay_num_tags == 0) {
20562054
ret = JBD2_FC_REPLAY_STOP;
20572055
ext4_fc_set_bitmaps_and_counters(sb);
20582056
break;
20592057
}
20602058
jbd_debug(3, "Replay phase, tag:%s\n",
2061-
tag2str(le16_to_cpu(tl->fc_tag)));
2059+
tag2str(le16_to_cpu(tl.fc_tag)));
20622060
state->fc_replay_num_tags--;
2063-
switch (le16_to_cpu(tl->fc_tag)) {
2061+
switch (le16_to_cpu(tl.fc_tag)) {
20642062
case EXT4_FC_TAG_LINK:
2065-
ret = ext4_fc_replay_link(sb, tl);
2063+
ret = ext4_fc_replay_link(sb, &tl, val);
20662064
break;
20672065
case EXT4_FC_TAG_UNLINK:
2068-
ret = ext4_fc_replay_unlink(sb, tl);
2066+
ret = ext4_fc_replay_unlink(sb, &tl, val);
20692067
break;
20702068
case EXT4_FC_TAG_ADD_RANGE:
2071-
ret = ext4_fc_replay_add_range(sb, tl);
2069+
ret = ext4_fc_replay_add_range(sb, &tl, val);
20722070
break;
20732071
case EXT4_FC_TAG_CREAT:
2074-
ret = ext4_fc_replay_create(sb, tl);
2072+
ret = ext4_fc_replay_create(sb, &tl, val);
20752073
break;
20762074
case EXT4_FC_TAG_DEL_RANGE:
2077-
ret = ext4_fc_replay_del_range(sb, tl);
2075+
ret = ext4_fc_replay_del_range(sb, &tl, val);
20782076
break;
20792077
case EXT4_FC_TAG_INODE:
2080-
ret = ext4_fc_replay_inode(sb, tl);
2078+
ret = ext4_fc_replay_inode(sb, &tl, val);
20812079
break;
20822080
case EXT4_FC_TAG_PAD:
20832081
trace_ext4_fc_replay(sb, EXT4_FC_TAG_PAD, 0,
2084-
ext4_fc_tag_len(tl), 0);
2082+
le16_to_cpu(tl.fc_len), 0);
20852083
break;
20862084
case EXT4_FC_TAG_TAIL:
20872085
trace_ext4_fc_replay(sb, EXT4_FC_TAG_TAIL, 0,
2088-
ext4_fc_tag_len(tl), 0);
2089-
tail = (struct ext4_fc_tail *)ext4_fc_tag_val(tl);
2090-
WARN_ON(le32_to_cpu(tail->fc_tid) != expected_tid);
2086+
le16_to_cpu(tl.fc_len), 0);
2087+
memcpy(&tail, val, sizeof(tail));
2088+
WARN_ON(le32_to_cpu(tail.fc_tid) != expected_tid);
20912089
break;
20922090
case EXT4_FC_TAG_HEAD:
20932091
break;
20942092
default:
2095-
trace_ext4_fc_replay(sb, le16_to_cpu(tl->fc_tag), 0,
2096-
ext4_fc_tag_len(tl), 0);
2093+
trace_ext4_fc_replay(sb, le16_to_cpu(tl.fc_tag), 0,
2094+
le16_to_cpu(tl.fc_len), 0);
20972095
ret = -ECANCELED;
20982096
break;
20992097
}

0 commit comments

Comments
 (0)