Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial code to support ext4 #303

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions arch/arm/include/arch/arm.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ GEN_CP15_REG_FUNCS(midr, 0, c0, c0, 0);
GEN_CP15_REG_FUNCS(mpidr, 0, c0, c0, 5);
GEN_CP15_REG_FUNCS(vbar, 0, c12, c0, 0);
GEN_CP15_REG_FUNCS(cbar, 4, c15, c0, 0);
GEN_CP15_REG_FUNCS(nsacr, 0, c1, c1, 2); // non-secure access control register
GEN_CP15_REG_FUNCS(cntfrq, 0, c14, c0, 0); // counter frequency
GEN_CP14_REG_FUNCS(scr, 0, c1, c1, 0); // secure configuration register

GEN_CP15_REG_FUNCS(ats1cpr, 0, c7, c8, 0);
GEN_CP15_REG_FUNCS(ats1cpw, 0, c7, c8, 1);
Expand Down
125 changes: 122 additions & 3 deletions lib/fs/ext2/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,34 @@

#define LOCAL_TRACE 0

struct dircookie {
ext2_t *fs;
struct ext2_inode dir_inode;
uint fileblock;
uint cursor;
};

// TODO, move to ext2.c
static void ext2_dump_inode(struct ext2_inode *inode) {
#if LOCAL_TRACE
printf("mode: %d\n", inode->i_mode);
printf("uid: %d\n", inode->i_uid);
printf("size: %d\n", inode->i_size);
printf("blocks: %d\n", inode->i_blocks);
printf("flags: 0x%x\n", inode->i_flags);
#endif
}

/* read in the dir, look for the entry */
static int ext2_dir_lookup(ext2_t *ext2, struct ext2_inode *dir_inode, const char *name, inodenum_t *inum) {
uint file_blocknum;
int err;
uint8_t *buf;
size_t namelen = strlen(name);
#if LOCAL_TRACE
printf("looking for %s in inode %p\n", name, dir_inode);
ext2_dump_inode(dir_inode);
cleverca22 marked this conversation as resolved.
Show resolved Hide resolved
#endif

if (!S_ISDIR(dir_inode->i_mode))
return ERR_NOT_DIR;
Expand All @@ -41,8 +63,8 @@ static int ext2_dir_lookup(ext2_t *ext2, struct ext2_inode *dir_inode, const cha
while (pos < EXT2_BLOCK_SIZE(ext2->sb)) {
ent = (struct ext2_dir_entry_2 *)&buf[pos];

LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d\n",
file_blocknum, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len/* , ent->name*/);
LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d, type: %d, name '%s'\n",
file_blocknum, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len, ent->file_type , ent->name);

/* sanity check the record length */
if (LE16(ent->rec_len) == 0)
Expand Down Expand Up @@ -78,6 +100,7 @@ static int ext2_walk(ext2_t *ext2, char *path, struct ext2_inode *start_inode, i
bool done;

LTRACEF("path '%s', start_inode %p, inum %p, recurse %d\n", path, start_inode, inum, recurse);
ext2_dump_inode(start_inode);

if (recurse > 4)
return ERR_RECURSE_TOO_DEEP;
Expand Down Expand Up @@ -150,7 +173,7 @@ static int ext2_walk(ext2_t *ext2, char *path, struct ext2_inode *start_inode, i
} else {
if (!done) {
/* we aren't done and this walked over a nondir, abort */
LTRACEF("not finished and component is nondir\n");
LTRACEF("not finished and component is nondir: i_mode: 0x%x\n", inode.i_mode);
return ERR_NOT_FOUND;
}
}
Expand Down Expand Up @@ -178,3 +201,99 @@ int ext2_lookup(ext2_t *ext2, const char *_path, inodenum_t *inum) {
return ext2_walk(ext2, path, &ext2->root_inode, inum, 1);
}

status_t ext2_opendir(fscookie *cookie, const char *name, dircookie **dcookie) {
LTRACEF("cookie %p name '%s' dircookie %p\n", cookie, name, dcookie);
ext2_t *ext2 = (ext2_t *)cookie;
int err;
inodenum_t inum;
//uint file_blocknum;
if (name[0] == 0) {
inum = EXT2_ROOT_INO;
} else {
err = ext2_lookup(ext2, name, &inum);
if (err < 0) {
LTRACEF("err = %d\n", err);
return err;
}
}
LTRACEF("inum = %d\n", inum);

// allocate a dir cookie, point it at the first file, and stuff it in the dircookie jar
dircookie *dir = calloc(1, sizeof(*dir));
if (!dir) return ERR_NO_MEMORY;
dir->fs = ext2;
/* load the next inode */
err = ext2_load_inode(ext2, inum, &dir->dir_inode);
if (err < 0) {
free(dir);
return err;
}

if (!S_ISDIR(dir->dir_inode.i_mode)) {
free(dir);
return ERR_NOT_DIR;
cleverca22 marked this conversation as resolved.
Show resolved Hide resolved
}

*dcookie = dir;

return 0;
}
status_t ext2_readdir(dircookie *cookie, struct dirent *ent_out) {
int err;
uint8_t *buf;
struct ext2_dir_entry_2 *ent;
uint pos;

if (!ent_out)
return ERR_INVALID_ARGS;

LTRACEF("dircookie: %p, direnv: %p, %d:%d\n", cookie, ent_out, cookie->fileblock, cookie->cursor);

if (cookie->fileblock >= cookie->dir_inode.i_blocks) {
return ERR_NOT_FOUND;
}

// TODO, write a function that will get a reference to the block cache
// then copy and byte-swap the ext2_dir_entry_2 in one action
buf = malloc(EXT2_BLOCK_SIZE(cookie->fs->sb));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preferably find algorithm that does't need to malloc a buffer, but in the short term check for failure to malloc and abort.


err = ext2_read_inode(cookie->fs, &cookie->dir_inode, buf, cookie->fileblock * EXT2_BLOCK_SIZE(cookie->fs->sb), EXT2_BLOCK_SIZE(cookie->fs->sb));
if (err <= 0) {
free(buf);
return -1;
}

pos = cookie->cursor;
ent = (struct ext2_dir_entry_2 *)&buf[pos];

LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d\n", cookie->fileblock, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len/* , ent->name*/);

/* sanity check the record length */
if (LE16(ent->rec_len) == 0) {
free(buf);
return -1;
}

pos += ROUNDUP(LE16(ent->rec_len), 4);
if (ent->inode == 0) { // deleted file
// TODO, assumes end of dir listing
// it should continue to the next record, potentially in the next block
free(buf);
return ERR_NOT_FOUND;
}
if (pos >= EXT2_BLOCK_SIZE(cookie->fs->sb)) {
cookie->fileblock++;
cookie->cursor = 0;
} else {
cookie->cursor = pos;
}
int size = MIN(ent->name_len, FS_MAX_FILE_LEN-1);
memcpy(ent_out->name, ent->name, size);
ent_out->name[size] = 0;
free(buf);
return NO_ERROR;
}
status_t ext2_closedir(dircookie *cookie) {
free(cookie);
return NO_ERROR;
}
79 changes: 63 additions & 16 deletions lib/fs/ext2/ext2.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ static void endian_swap_superblock(struct ext2_super_block *sb) {
LE32SWAP(sb->s_last_orphan);
LE32SWAP(sb->s_default_mount_opts);
LE32SWAP(sb->s_first_meta_bg);
LE32SWAP(sb->s_desc_size);
// TODO not all fields past s_first_meta_bg are byte-swapped
LE32SWAP(sb->s_log_groups_per_flex);
}

static void endian_swap_inode(struct ext2_inode *inode) {
Expand Down Expand Up @@ -94,6 +97,7 @@ static void endian_swap_group_desc(struct ext2_group_desc *gd) {

status_t ext2_mount(bdev_t *dev, fscookie **cookie) {
int err;
int i;

LTRACEF("dev %p\n", dev);

Expand Down Expand Up @@ -121,14 +125,31 @@ status_t ext2_mount(bdev_t *dev, fscookie **cookie) {
/* print some info */
LTRACEF("rev level %d\n", ext2->sb.s_rev_level);
LTRACEF("compat features 0x%x\n", ext2->sb.s_feature_compat);
if (LOCAL_TRACE) {
if (ext2->sb.s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) puts(" 0x001 sparse super");
if (ext2->sb.s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_LARGE_FILE) puts(" 0x002 large file");
if (ext2->sb.s_feature_ro_compat & RO_COMPAT_HUGE_FILE) puts(" 0x008 huge file");
if (ext2->sb.s_feature_ro_compat & RO_COMPAT_DIR_NLINK) puts(" 0x020 directory nlink");
if (ext2->sb.s_feature_ro_compat & RO_COMPAT_EXTRA_ISIZE) puts(" 0x040 extra isize");
if (ext2->sb.s_feature_ro_compat & RO_COMPAT_METADATA_CSUM) puts(" 0x400 metadata checksum");
}
LTRACEF("incompat features 0x%x\n", ext2->sb.s_feature_incompat);
if (LOCAL_TRACE) {
if (ext2->sb.s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE) puts(" 0x002 ext4_dir_entry_2 in use");
if (ext2->sb.s_feature_incompat & INCOMPAT_EXTENTS) puts(" 0x040 inodes may be using extents ");
if (ext2->sb.s_feature_incompat & INCOMPAT_64BIT) puts(" 0x080 allow an FS of up to 2^64 blocks ");
if (ext2->sb.s_feature_incompat & INCOMPAT_FLEX_BG) puts(" 0x200 flexible block groups ");
}
LTRACEF("ro compat features 0x%x\n", ext2->sb.s_feature_ro_compat);
LTRACEF("block size %d\n", EXT2_BLOCK_SIZE(ext2->sb));
LTRACEF("inode size %d\n", EXT2_INODE_SIZE(ext2->sb));
LTRACEF("block count %d\n", ext2->sb.s_blocks_count);
LTRACEF("blocks per group %d\n", ext2->sb.s_blocks_per_group);
LTRACEF("group count %d\n", ext2->s_group_count);
LTRACEF("inodes per group %d\n", ext2->sb.s_inodes_per_group);
if (ext2->sb.s_feature_incompat & INCOMPAT_64BIT) {
LTRACEF("block group size: %d\n", ext2->sb.s_desc_size);
}

/* we only support dynamic revs */
if (ext2->sb.s_rev_level > EXT2_DYNAMIC_REV) {
Expand All @@ -137,31 +158,49 @@ status_t ext2_mount(bdev_t *dev, fscookie **cookie) {
}

/* make sure it doesn't have any ro features we don't support */
if (ext2->sb.s_feature_ro_compat & ~(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
if (ext2->sb.s_feature_ro_compat & ~(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
|EXT2_FEATURE_RO_COMPAT_LARGE_FILE|RO_COMPAT_HUGE_FILE
|RO_COMPAT_DIR_NLINK|RO_COMPAT_EXTRA_ISIZE|RO_COMPAT_METADATA_CSUM)) {
LTRACEF("s_feature_ro_compat not compatible, 0x%x\n", ext2->sb.s_feature_ro_compat);
err = -3;
return err;
}

ext2->has_new_directory_entries = (ext2->sb.s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE) != 0;

if (ext2->sb.s_feature_incompat & INCOMPAT_FLEX_BG) {
printf("s_log_groups_per_flex: %d\n", ext2->sb.s_log_groups_per_flex);
}

// ext2/3 always have 32 byte block group descriptors
// ext4 WITHOUT INCOMPAT_64BIT also has 32 byte descriptors
// ext4 with INCOMPAT_64BIT has ${s_desc_size} byte descriptors
uint32_t block_group_size;
if (ext2->sb.s_feature_incompat & INCOMPAT_64BIT) {
block_group_size = ext2->sb.s_desc_size;
} else {
block_group_size = 32;
}

/* read in all the group descriptors */
ext2->gd = malloc(sizeof(struct ext2_group_desc) * ext2->s_group_count);
err = bio_read(ext2->dev, (void *)ext2->gd,
(EXT2_BLOCK_SIZE(ext2->sb) == 4096) ? 4096 : 2048,
sizeof(struct ext2_group_desc) * ext2->s_group_count);
if (err < 0) {
err = -4;
return err;
for (i=0; i < ext2->s_group_count; i++) {
uint32_t block_groups_start = (EXT2_BLOCK_SIZE(ext2->sb) == 4096) ? 4096 : 2048;
uint32_t offset = block_groups_start + (i * block_group_size);
err = bio_read(ext2->dev, (void *)&ext2->gd[i], offset,
sizeof(struct ext2_group_desc));
if (err < 0) {
err = -4;
return err;
}
endian_swap_group_desc(&ext2->gd[i]);
}

int i;
for (i=0; i < ext2->s_group_count; i++) {
endian_swap_group_desc(&ext2->gd[i]);
LTRACEF("group %d:\n", i);
LTRACEF("\tblock bitmap %d\n", ext2->gd[i].bg_block_bitmap);
LTRACEF("\tinode bitmap %d\n", ext2->gd[i].bg_inode_bitmap);
LTRACEF("\tinode table %d\n", ext2->gd[i].bg_inode_table);
LTRACEF("\tfree blocks %d\n", ext2->gd[i].bg_free_blocks_count);
LTRACEF("\tfree inodes %d\n", ext2->gd[i].bg_free_inodes_count);
LTRACEF("\tused dirs %d\n", ext2->gd[i].bg_used_dirs_count);
LTRACEF(" Group %2d: block bitmap at %d, inode bitmap at %d, inode table at %d\n", i, ext2->gd[i].bg_block_bitmap,
ext2->gd[i].bg_inode_bitmap, ext2->gd[i].bg_inode_table);
LTRACEF(" %d free blocks, %d free inodes, %d used directories\n",
ext2->gd[i].bg_free_blocks_count, ext2->gd[i].bg_free_inodes_count, ext2->gd[i].bg_used_dirs_count);
}

/* initialize the block cache */
Expand Down Expand Up @@ -203,6 +242,7 @@ static void get_inode_addr(ext2_t *ext2, inodenum_t num, blocknum_t *block, size

// calculate the start of the inode table for the group it's in
*block = ext2->gd[group].bg_inode_table;
LTRACEF("inode %d is in block group %d, the inode table for that group begins at %d\n", num, group, *block);

// add the offset of the inode within the group
size_t offset = (num % EXT2_INODES_PER_GROUP(ext2->sb)) * EXT2_INODE_SIZE(ext2->sb);
Expand Down Expand Up @@ -238,6 +278,9 @@ int ext2_load_inode(ext2_t *ext2, inodenum_t num, struct ext2_inode *inode) {

LTRACEF("read inode: mode 0x%x, size %d\n", inode->i_mode, inode->i_size);

LTRACEF("loaded inode %d from disk block %d offset %d, i_uid:%d i_gid:%d i_flags:0x%x i_blocks:%d i_size:%d\n",
num, bnum, block_offset, inode->i_uid, inode->i_gid, inode->i_flags, inode->i_blocks, inode->i_size);

return 0;
}

Expand All @@ -248,6 +291,10 @@ static const struct fs_api ext2_api = {
.stat = ext2_stat_file,
.read = ext2_read_file,
.close = ext2_close_file,

.opendir = ext2_opendir,
.readdir = ext2_readdir,
.closedir = ext2_closedir,
};

STATIC_FS_IMPL(ext2, &ext2_api);
Loading