Skip to content

Commit 675abf8

Browse files
konisakpm00
authored andcommitted
nilfs2: prevent WARNING in nilfs_sufile_set_segment_usage()
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]>
1 parent 4a3ef6b commit 675abf8

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
@@ -501,15 +501,38 @@ int nilfs_sufile_mark_dirty(struct inode *sufile, __u64 segnum)
501501

502502
down_write(&NILFS_MDT(sufile)->mi_sem);
503503
ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
504-
if (!ret) {
505-
mark_buffer_dirty(bh);
506-
nilfs_mdt_mark_dirty(sufile);
507-
kaddr = kmap_atomic(bh->b_page);
508-
su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
504+
if (ret)
505+
goto out_sem;
506+
507+
kaddr = kmap_atomic(bh->b_page);
508+
su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
509+
if (unlikely(nilfs_segment_usage_error(su))) {
510+
struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
511+
512+
kunmap_atomic(kaddr);
513+
brelse(bh);
514+
if (nilfs_segment_is_active(nilfs, segnum)) {
515+
nilfs_error(sufile->i_sb,
516+
"active segment %llu is erroneous",
517+
(unsigned long long)segnum);
518+
} else {
519+
/*
520+
* Segments marked erroneous are never allocated by
521+
* nilfs_sufile_alloc(); only active segments, ie,
522+
* the segments indexed by ns_segnum or ns_nextnum,
523+
* can be erroneous here.
524+
*/
525+
WARN_ON_ONCE(1);
526+
}
527+
ret = -EIO;
528+
} else {
509529
nilfs_segment_usage_set_dirty(su);
510530
kunmap_atomic(kaddr);
531+
mark_buffer_dirty(bh);
532+
nilfs_mdt_mark_dirty(sufile);
511533
brelse(bh);
512534
}
535+
out_sem:
513536
up_write(&NILFS_MDT(sufile)->mi_sem);
514537
return ret;
515538
}
@@ -536,9 +559,14 @@ int nilfs_sufile_set_segment_usage(struct inode *sufile, __u64 segnum,
536559

537560
kaddr = kmap_atomic(bh->b_page);
538561
su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
539-
WARN_ON(nilfs_segment_usage_error(su));
540-
if (modtime)
562+
if (modtime) {
563+
/*
564+
* Check segusage error and set su_lastmod only when updating
565+
* this entry with a valid timestamp, not for cancellation.
566+
*/
567+
WARN_ON_ONCE(nilfs_segment_usage_error(su));
541568
su->su_lastmod = cpu_to_le64(modtime);
569+
}
542570
su->su_nblocks = cpu_to_le32(nblocks);
543571
kunmap_atomic(kaddr);
544572

0 commit comments

Comments
 (0)