Whamcloud - gitweb
libext2fs: don't truncate the orphan file inode if it is newly allocated
authorTheodore Ts'o <tytso@mit.edu>
Fri, 25 Aug 2023 21:28:01 +0000 (17:28 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 25 Aug 2023 21:28:01 +0000 (17:28 -0400)
In ext2fs_create_orphan_file(), don't try truncating inode for the
orphan file if ext2fs_create_orphan_file() allocated the inode.  This
avoids problems where the newly allocated inode in the inode table
might contain garbage; if the metadata checksum feature is enabled,
this will generally result in the function failing with a checksum
invalid error, but this can cause mke2fs (which calls
ext2fs_create_orphan_file) to fail.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/orphan.c

index e25f20c..c2f8356 100644 (file)
@@ -127,22 +127,21 @@ errcode_t ext2fs_create_orphan_file(ext2_filsys fs, blk_t num_blocks)
        struct mkorphan_info oi;
        struct ext4_orphan_block_tail *ob_tail;
 
-       if (!ino) {
+       if (ino) {
+               err = ext2fs_read_inode(fs, ino, &inode);
+               if (err)
+                       return err;
+               if (EXT2_I_SIZE(&inode)) {
+                       err = ext2fs_truncate_orphan_file(fs);
+                       if (err)
+                               return err;
+               }
+       } else {
                err = ext2fs_new_inode(fs, EXT2_ROOT_INO, LINUX_S_IFREG | 0600,
                                       0, &ino);
                if (err)
                        return err;
                ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
-               ext2fs_mark_ib_dirty(fs);
-       }
-
-       err = ext2fs_read_inode(fs, ino, &inode);
-       if (err)
-               return err;
-       if (EXT2_I_SIZE(&inode)) {
-               err = ext2fs_truncate_orphan_file(fs);
-               if (err)
-                       return err;
        }
 
        memset(&inode, 0, sizeof(struct ext2_inode));