Skip to content

Commit 5eaa4d2

Browse files
konisgregkh
authored andcommitted
nilfs2: prevent WARNING in nilfs_sufile_set_segment_usage()
commit 675abf8df1353e0e3bde314993e0796c524cfbf0 upstream. If nilfs2 reads a disk image with corrupted segment usage metadata, and its segment usage information is marked as an error for the segment at the write location, nilfs_sufile_set_segment_usage() can trigger WARN_ONs during log writing. Segments newly allocated for writing with nilfs_sufile_alloc() will not have this error flag set, but this unexpected situation will occur if the segment indexed by either nilfs->ns_segnum or nilfs->ns_nextnum (active segment) was marked in error. Fix this issue by inserting a sanity check to treat it as a file system corruption. Since error returns are not allowed during the execution phase where nilfs_sufile_set_segment_usage() is used, this inserts the sanity check into nilfs_sufile_mark_dirty() which pre-reads the buffer containing the segment usage record to be updated and sets it up in a dirty state for writing. In addition, nilfs_sufile_set_segment_usage() is also called when canceling log writing and undoing segment usage update, so in order to avoid issuing the same kernel warning in that case, in case of cancellation, avoid checking the error flag in nilfs_sufile_set_segment_usage(). Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ryusuke Konishi <[email protected]> Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=14e9f834f6ddecece094 Tested-by: Ryusuke Konishi <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent f7fc9d4 commit 5eaa4d2

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

fs/nilfs2/sufile.c

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,38 @@ int nilfs_sufile_mark_dirty(struct inode *sufile, __u64 segnum)
504504

505505
down_write(&NILFS_MDT(sufile)->mi_sem);
506506
ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
507-
if (!ret) {
508-
mark_buffer_dirty(bh);
509-
nilfs_mdt_mark_dirty(sufile);
510-
kaddr = kmap_atomic(bh->b_page);
511-
su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
507+
if (ret)
508+
goto out_sem;
509+
510+
kaddr = kmap_atomic(bh->b_page);
511+
su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
512+
if (unlikely(nilfs_segment_usage_error(su))) {
513+
struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
514+
515+
kunmap_atomic(kaddr);
516+
brelse(bh);
517+
if (nilfs_segment_is_active(nilfs, segnum)) {
518+
nilfs_error(sufile->i_sb,
519+
"active segment %llu is erroneous",
520+
(unsigned long long)segnum);
521+
} else {
522+
/*
523+
* Segments marked erroneous are never allocated by
524+
* nilfs_sufile_alloc(); only active segments, ie,
525+
* the segments indexed by ns_segnum or ns_nextnum,
526+
* can be erroneous here.
527+
*/
528+
WARN_ON_ONCE(1);
529+
}
530+
ret = -EIO;
531+
} else {
512532
nilfs_segment_usage_set_dirty(su);
513533
kunmap_atomic(kaddr);
534+
mark_buffer_dirty(bh);
535+
nilfs_mdt_mark_dirty(sufile);
514536
brelse(bh);
515537
}
538+
out_sem:
516539
up_write(&NILFS_MDT(sufile)->mi_sem);
517540
return ret;
518541
}
@@ -539,9 +562,14 @@ int nilfs_sufile_set_segment_usage(struct inode *sufile, __u64 segnum,
539562

540563
kaddr = kmap_atomic(bh->b_page);
541564
su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
542-
WARN_ON(nilfs_segment_usage_error(su));
543-
if (modtime)
565+
if (modtime) {
566+
/*
567+
* Check segusage error and set su_lastmod only when updating
568+
* this entry with a valid timestamp, not for cancellation.
569+
*/
570+
WARN_ON_ONCE(nilfs_segment_usage_error(su));
544571
su->su_lastmod = cpu_to_le64(modtime);
572+
}
545573
su->su_nblocks = cpu_to_le32(nblocks);
546574
kunmap_atomic(kaddr);
547575

0 commit comments

Comments
 (0)