shithub: lwext4

Download patch

ref: 79fb06860bb52d73e6f69fbb2c79d05eaba37a5c
parent: 142e8c1b517189dbc9466e6c6c445aa40f208a1f
author: gkostka <kostka.grzegorz@gmail.com>
date: Mon Nov 16 16:28:08 EST 2015

Create default directory structure in mkfs

--- a/lwext4/ext4_mkfs.c
+++ b/lwext4/ext4_mkfs.c
@@ -37,6 +37,7 @@
 #include "ext4_config.h"
 #include "ext4_super.h"
 #include "ext4_block_group.h"
+#include "ext4_dir.h"
 #include "ext4_fs.h"
 #include "ext4_inode.h"
 #include "ext4_debug.h"
@@ -281,6 +282,7 @@
 		bg_free_blk = info->blocks_per_group -
 			(aux_info->inode_table_blocks + aux_info->bg_desc_blocks);
 
+		bg_free_blk -= 2;
 		blk_off += aux_info->bg_desc_blocks;
 
 		if (has_superblock(info, i)) {
@@ -465,27 +467,76 @@
 {
 	int r = EOK;
 	int i;
-
+	struct ext4_inode_ref inode_ref;
 	for (i = 1; i < 12; ++i) {
+		int filetype = EXT4_DIRENTRY_REG_FILE;
 
-		bool dir = false;
-
 		switch (i) {
 		case EXT4_ROOT_INO:
 		case EXT4_GOOD_OLD_FIRST_INO:
-			dir = true;
+			filetype = EXT4_DIRENTRY_DIR;
 			break;
 		}
 
-		uint32_t index = 0;
-		r = ext4_ialloc_alloc_inode(fs, &index, dir);
+		r = ext4_fs_alloc_inode(fs, &inode_ref, filetype);
 		if (r != EOK)
 			return r;
+
+		ext4_inode_set_mode(&fs->sb, inode_ref.inode, 0);
+		ext4_fs_put_inode_ref(&inode_ref);
 	}
 
 	return r;
 }
 
+static int create_dirs(struct ext4_fs *fs)
+{
+	int r = EOK;
+	struct ext4_inode_ref root;
+	struct ext4_inode_ref child;
+
+	r = ext4_fs_get_inode_ref(fs, EXT4_ROOT_INO, &root);
+	if (r != EOK)
+		return r;
+
+	r = ext4_fs_get_inode_ref(fs, EXT4_GOOD_OLD_FIRST_INO, &child);
+	if (r != EOK)
+		return r;
+
+	ext4_inode_set_mode(&fs->sb, child.inode,
+			EXT4_INODE_MODE_DIRECTORY | 0777);
+
+	ext4_inode_set_mode(&fs->sb, root.inode,
+			EXT4_INODE_MODE_DIRECTORY | 0777);
+
+	r = ext4_dir_add_entry(&root, ".", strlen("."), &root);
+	if (r != EOK)
+		return r;
+
+	r = ext4_dir_add_entry(&root, "..", strlen(".."), &root);
+	if (r != EOK)
+		return r;
+
+	r = ext4_dir_add_entry(&child, ".", strlen("."), &child);
+	if (r != EOK)
+		return r;
+
+	r = ext4_dir_add_entry(&child, "..", strlen(".."), &root);
+	if (r != EOK)
+		return r;
+
+	r = ext4_dir_add_entry(&root, "lost+found", strlen("lost+found"), &child);
+	if (r != EOK)
+		return r;
+
+	ext4_inode_set_links_count(root.inode, 3);
+	ext4_inode_set_links_count(child.inode, 2);
+
+	ext4_fs_put_inode_ref(&child);
+	ext4_fs_put_inode_ref(&root);
+	return r;
+}
+
 int ext4_mkfs(struct ext4_fs *fs, struct ext4_blockdev *bd,
 	      struct ext4_mkfs_info *info)
 {
@@ -581,6 +632,10 @@
 		goto cache_fini;
 
 	r = alloc_inodes(fs);
+	if (r != EOK)
+		goto fs_fini;
+
+	r = create_dirs(fs);
 	if (r != EOK)
 		goto fs_fini;