From: Stefan Brüns Date: Tue, 6 Sep 2016 02:36:42 +0000 (+0200) Subject: ext4: propagate error if creation of directory entry fails X-Git-Url: http://git.dujemihanovic.xyz/?a=commitdiff_plain;h=a0d767e2c1e40e781fdaa592f06f92614b4e5cdf;p=u-boot.git ext4: propagate error if creation of directory entry fails In case the dir entry creation failed, ext4fs_write would later overwrite a random inode, as inodeno was never initialized. Signed-off-by: Stefan Brüns Reviewed-by: Lukasz Majewski --- diff --git a/fs/ext4/ext4_common.c b/fs/ext4/ext4_common.c index d647ae0fd2..680e0011cc 100644 --- a/fs/ext4/ext4_common.c +++ b/fs/ext4/ext4_common.c @@ -366,7 +366,7 @@ static int check_void_in_dentry(struct ext2_dirent *dir, char *filename) return 0; } -void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type) +int ext4fs_update_parent_dentry(char *filename, int file_type) { unsigned int *zero_buffer = NULL; char *root_first_block_buffer = NULL; @@ -380,7 +380,7 @@ void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type) unsigned int last_entry_dirlen; int sizeof_void_space = 0; int templength = 0; - int inodeno; + int inodeno = -1; int status; struct ext_filesystem *fs = get_fs(); /* directory entry */ @@ -393,13 +393,13 @@ void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type) zero_buffer = zalloc(fs->blksz); if (!zero_buffer) { printf("No Memory\n"); - return; + return -1; } root_first_block_buffer = zalloc(fs->blksz); if (!root_first_block_buffer) { free(zero_buffer); printf("No Memory\n"); - return; + return -1; } restart: @@ -518,8 +518,6 @@ restart: temp_dir = temp_dir + sizeof(struct ext2_dirent); memcpy(temp_dir, filename, strlen(filename)); - *p_ino = inodeno; - /* update or write the 1st block of root inode */ if (ext4fs_put_metadata(root_first_block_buffer, first_block_no_of_root)) @@ -528,6 +526,8 @@ restart: fail: free(zero_buffer); free(root_first_block_buffer); + + return inodeno; } static int search_dir(struct ext2_inode *parent_inode, char *dirname) diff --git a/fs/ext4/ext4_common.h b/fs/ext4/ext4_common.h index 370a7175ca..cc9d0c5a5f 100644 --- a/fs/ext4/ext4_common.h +++ b/fs/ext4/ext4_common.h @@ -61,7 +61,7 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name, uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n); uint16_t ext4fs_checksum_update(unsigned int i); int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags); -void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type); +int ext4fs_update_parent_dentry(char *filename, int file_type); uint32_t ext4fs_get_new_blk_no(void); int ext4fs_get_new_inode_no(void); void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, diff --git a/fs/ext4/ext4_write.c b/fs/ext4/ext4_write.c index 9200c4727e..a52804e8e1 100644 --- a/fs/ext4/ext4_write.c +++ b/fs/ext4/ext4_write.c @@ -907,7 +907,9 @@ int ext4fs_write(const char *fname, unsigned char *buffer, goto fail; } - ext4fs_update_parent_dentry(filename, &inodeno, FILETYPE_REG); + inodeno = ext4fs_update_parent_dentry(filename, FILETYPE_REG); + if (inodeno == -1) + goto fail; /* prepare file inode */ inode_buffer = zalloc(fs->inodesz); if (!inode_buffer)