@@ -41,6 +41,7 @@ errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
ext2_ino_t scratch_ino;
blk64_t blk;
char *block = 0;
+ int unmark_blk = 0;
EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
@@ -115,6 +116,14 @@ errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
}
/*
+ * XXX: We need to update accouting before we mkdir successful because
+ * in ext2fs_link it could allocate a new block when there is no space
+ * to store a new dir entry in inline data. If ext2fs_link failed, we
+ * need to unmark this block.
+ */
+ ext2fs_block_alloc_stats2(fs, blk, +1);
+
+ /*
* Link the directory into the filesystem hierarchy
*/
if (name) {
@@ -123,32 +132,48 @@ errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
if (!retval) {
retval = EXT2_ET_DIR_EXISTS;
name = 0;
+ unmark_blk = 1;
goto cleanup;
}
- if (retval != EXT2_ET_FILE_NOT_FOUND)
+ if (retval != EXT2_ET_FILE_NOT_FOUND) {
+ unmark_blk = 1;
goto cleanup;
+ }
retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
- if (retval)
+ if (retval) {
+ unmark_blk = 1;
goto cleanup;
+ }
}
/*
* Update parent inode's counts
*/
if (parent != ino) {
+ /* Reload parent inode */
+ retval = ext2fs_read_inode(fs, parent, &parent_inode);
+ if (retval) {
+ unmark_blk = 1;
+ goto cleanup;
+ }
parent_inode.i_links_count++;
retval = ext2fs_write_inode(fs, parent, &parent_inode);
- if (retval)
+ if (retval) {
+ unmark_blk = 1;
goto cleanup;
+ }
}
/*
* Update accounting....
*/
- ext2fs_block_alloc_stats2(fs, blk, +1);
ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
cleanup:
+ /* Unmark block because mkdir failed */
+ if (unmark_blk)
+ ext2fs_block_alloc_stats2(fs, blk, -1);
+
if (block)
ext2fs_free_mem(&block);
return retval;