ext4: do not convert the unwritten extents if data writeback fails
authorBaokun Li <libaokun1@huawei.com>
Wed, 22 Jan 2025 11:05:26 +0000 (19:05 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 13 Mar 2025 14:08:08 +0000 (10:08 -0400)
When dioread_nolock is turned on (the default), it will convert unwritten
extents to written at ext4_end_io_end(), even if the data writeback fails.

It leads to the possibility that stale data may be exposed when the
physical block corresponding to the file data is read-only (i.e., writes
return -EIO, but reads are normal).

Therefore a new ext4_io_end->flags EXT4_IO_END_FAILED is added, which
indicates that some bio write-back failed in the current ext4_io_end.
When this flag is set, the unwritten to written conversion is no longer
performed. Users can read the data normally until the caches are dropped,
after that, the failed extents can only be read to all 0.

Signed-off-by: Baokun Li <libaokun1@huawei.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Zhang Yi <yi.zhang@huawei.com>
Link: https://patch.msgid.link/20250122110533.4116662-3-libaokun@huaweicloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/ext4.h
fs/ext4/page-io.c

index 39032255a70b8386e50a3d208366522697b6c92a..226865b2fabca9a311c791746d737526ac61208a 100644 (file)
@@ -278,7 +278,8 @@ struct ext4_system_blocks {
 /*
  * Flags for ext4_io_end->flags
  */
-#define        EXT4_IO_END_UNWRITTEN   0x0001
+#define EXT4_IO_END_UNWRITTEN  0x0001
+#define EXT4_IO_END_FAILED     0x0002
 
 struct ext4_io_end_vec {
        struct list_head list;          /* list of io_end_vec */
index f53b018ea259b2534d9963ee3c9b602df442c96e..6054ec27fb483be73fb30a77950fb6580ee215f6 100644 (file)
@@ -181,14 +181,25 @@ static int ext4_end_io_end(ext4_io_end_t *io_end)
                   "list->prev 0x%p\n",
                   io_end, inode->i_ino, io_end->list.next, io_end->list.prev);
 
-       io_end->handle = NULL;  /* Following call will use up the handle */
-       ret = ext4_convert_unwritten_io_end_vec(handle, io_end);
+       /*
+        * Do not convert the unwritten extents if data writeback fails,
+        * or stale data may be exposed.
+        */
+       io_end->handle = NULL;  /* Following call will use up the handle */
+       if (unlikely(io_end->flag & EXT4_IO_END_FAILED)) {
+               ret = -EIO;
+               if (handle)
+                       jbd2_journal_free_reserved(handle);
+       } else {
+               ret = ext4_convert_unwritten_io_end_vec(handle, io_end);
+       }
        if (ret < 0 && !ext4_forced_shutdown(inode->i_sb)) {
                ext4_msg(inode->i_sb, KERN_EMERG,
                         "failed to convert unwritten extents to written "
                         "extents -- potential data loss!  "
                         "(inode %lu, error %d)", inode->i_ino, ret);
        }
+
        ext4_clear_io_unwritten_flag(io_end);
        ext4_release_io_end(io_end);
        return ret;
@@ -339,6 +350,7 @@ static void ext4_end_bio(struct bio *bio)
                             bio->bi_status, inode->i_ino,
                             (unsigned long long)
                             bi_sector >> (inode->i_blkbits - 9));
+               io_end->flag |= EXT4_IO_END_FAILED;
                mapping_set_error(inode->i_mapping,
                                blk_status_to_errno(bio->bi_status));
        }