ext2: implement file writing
there is much room for improvement in the traversal of indirectly linked blocks
This commit is contained in:
parent
178d88b2aa
commit
f8d84c7780
6 changed files with 423 additions and 31 deletions
414
fs/ext2.c
414
fs/ext2.c
|
|
@ -43,7 +43,7 @@ static int ext2_read_super(struct block_device *bdev, struct ext2_superblock *sb
|
|||
}
|
||||
|
||||
/* PCD must be 1 for buf */
|
||||
int ext2_read_block(struct ext2fs *fs, void *buf, size_t block)
|
||||
static int ext2_read_block(struct ext2fs *fs, void *buf, size_t block)
|
||||
{
|
||||
uint32_t bdev_block_size = fs->bdev->block_size;
|
||||
uint32_t num_blocks_read = fs->block_size / bdev_block_size;
|
||||
|
|
@ -54,10 +54,10 @@ int ext2_read_block(struct ext2fs *fs, void *buf, size_t block)
|
|||
}
|
||||
|
||||
/* nasty function, but it works */
|
||||
static int ext2_inode_read_block(struct ext2fs *fs, struct ext2_inode *inode, void *buf, size_t ino_block)
|
||||
static long ext2_inode_get_block_indir(struct ext2fs *fs, struct ext2_inode *inode, size_t ino_block)
|
||||
{
|
||||
if (ino_block < 12)
|
||||
return ext2_read_block(fs, buf, inode->block[ino_block]);
|
||||
return inode->block[ino_block];
|
||||
|
||||
if (ino_block < 12 + fs->indir_block_size) {
|
||||
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
|
|
@ -66,7 +66,7 @@ static int ext2_inode_read_block(struct ext2fs *fs, struct ext2_inode *inode, vo
|
|||
|
||||
ext2_read_block(fs, indir_buf, inode->block[12]);
|
||||
|
||||
int ret = ext2_read_block(fs, buf, indir_buf[ino_block - 12]);
|
||||
long ret = indir_buf[ino_block - 12];
|
||||
kfree(indir_buf);
|
||||
|
||||
return ret;
|
||||
|
|
@ -87,7 +87,7 @@ static int ext2_inode_read_block(struct ext2fs *fs, struct ext2_inode *inode, vo
|
|||
|
||||
ext2_read_block(fs, indir_buf2, indir_buf[(ino_block - 12 - fs->indir_block_size) / fs->indir_block_size]);
|
||||
|
||||
int ret = ext2_read_block(fs, buf, indir_buf2[(ino_block - 12 - fs->indir_block_size) % fs->indir_block_size]);
|
||||
long ret = indir_buf2[(ino_block - 12 - fs->indir_block_size) % fs->indir_block_size];
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
|
||||
|
|
@ -124,11 +124,9 @@ static int ext2_inode_read_block(struct ext2fs *fs, struct ext2_inode *inode, vo
|
|||
(fs->indir_block_size * fs->indir_block_size)) /
|
||||
fs->indir_block_size]);
|
||||
|
||||
int ret = ext2_read_block(
|
||||
fs, buf,
|
||||
indir_buf3[((ino_block - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
||||
(fs->indir_block_size * fs->indir_block_size)) %
|
||||
fs->indir_block_size]);
|
||||
long ret = indir_buf3[((ino_block - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
||||
(fs->indir_block_size * fs->indir_block_size)) %
|
||||
fs->indir_block_size];
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
kfree(indir_buf3);
|
||||
|
|
@ -139,7 +137,16 @@ static int ext2_inode_read_block(struct ext2fs *fs, struct ext2_inode *inode, vo
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
int ext2_write_block(struct ext2fs *fs, void *buf, size_t block)
|
||||
static int ext2_inode_read_block(struct ext2fs *fs, struct ext2_inode *inode, void *buf, size_t ino_block)
|
||||
{
|
||||
long ret = ext2_inode_get_block_indir(fs, inode, ino_block);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
return ext2_read_block(fs, buf, ret);
|
||||
}
|
||||
|
||||
static int ext2_write_block(struct ext2fs *fs, void *buf, size_t block)
|
||||
{
|
||||
uint32_t bdev_block_size = fs->bdev->block_size;
|
||||
uint32_t num_blocks_read = fs->block_size / bdev_block_size;
|
||||
|
|
@ -149,7 +156,16 @@ int ext2_write_block(struct ext2fs *fs, void *buf, size_t block)
|
|||
return block_write(fs->bdev, buf, bdev_block, num_blocks_read);
|
||||
}
|
||||
|
||||
int ext2_read_inode(struct ext2fs *fs, struct ext2_inode *out, ino_t inode)
|
||||
static int ext2_inode_write_block(struct ext2fs *fs, struct ext2_inode *inode, void *buf, size_t ino_block)
|
||||
{
|
||||
long ret = ext2_inode_get_block_indir(fs, inode, ino_block);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
return ext2_write_block(fs, buf, ret);
|
||||
}
|
||||
|
||||
static int ext2_read_inode(struct ext2fs *fs, struct ext2_inode *out, ino_t inode)
|
||||
{
|
||||
uint32_t group = (inode - 1) / fs->sb.inodes_per_group;
|
||||
uint32_t index = (inode - 1) % fs->sb.inodes_per_group;
|
||||
|
|
@ -176,7 +192,7 @@ int ext2_read_inode(struct ext2fs *fs, struct ext2_inode *out, ino_t inode)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, ino_t inode)
|
||||
static int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, ino_t inode)
|
||||
{
|
||||
uint32_t group = (inode - 1) / fs->sb.inodes_per_group;
|
||||
uint32_t index = (inode - 1) % fs->sb.inodes_per_group;
|
||||
|
|
@ -209,7 +225,294 @@ int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, ino_t inode)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ext2_open_file(struct fs *vfs, struct file *file, const char *path)
|
||||
static long ext2_alloc_block(struct ext2fs *fs)
|
||||
{
|
||||
uint32_t num_groups = fs->sb.blocks_count / fs->sb.blocks_per_group;
|
||||
if (fs->sb.blocks_count % fs->sb.blocks_per_group)
|
||||
num_groups++;
|
||||
|
||||
for (int i = 0; i < num_groups; i++) {
|
||||
struct ext2_group_desc *bg = &fs->bgdt[i];
|
||||
|
||||
if (bg->free_blocks_count) {
|
||||
uint32_t block = bg->block_bitmap;
|
||||
uint32_t block_offset = 0;
|
||||
|
||||
char *buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
ext2_read_block(fs, buf, block);
|
||||
|
||||
while (block_offset < fs->block_size) {
|
||||
uint8_t *byte = (uint8_t *)buf + block_offset;
|
||||
if (*byte == 0xFF)
|
||||
continue;
|
||||
|
||||
for (int j = 0; j < 8; j++) {
|
||||
if (!(*byte & (1 << j))) {
|
||||
*byte |= (1 << j);
|
||||
bg->free_blocks_count--;
|
||||
fs->sb.free_blocks_count--;
|
||||
|
||||
ext2_write_block(fs, buf, block);
|
||||
|
||||
/* update superblock and bgdt */
|
||||
ext2_write_block(fs, &fs->sb, 1);
|
||||
for (size_t i = 0; i < fs->bgdt_num_blocks; i++)
|
||||
ext2_write_block(fs, fs->bgdt + fs->block_size * i, fs->bgdt_blockno + i);
|
||||
|
||||
kfree(buf);
|
||||
|
||||
uint32_t block_ret_no = (i * fs->sb.blocks_per_group) + (block_offset * 8) + j + 1;
|
||||
|
||||
/* zero out the block */
|
||||
char *block_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!block_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
memset(block_buf, 0, fs->block_size);
|
||||
ext2_write_block(fs, block_buf, block_ret_no);
|
||||
kfree(block_buf);
|
||||
|
||||
return block_ret_no;
|
||||
}
|
||||
}
|
||||
|
||||
block_offset++;
|
||||
}
|
||||
|
||||
kfree(buf);
|
||||
}
|
||||
}
|
||||
|
||||
return -ENOSPC;
|
||||
}
|
||||
|
||||
static long ext2_inode_alloc_block(struct ext2fs *fs, struct ext2_inode *inode, ino_t inode_no)
|
||||
{
|
||||
/* allocate inode block and handle indirect if necessary */
|
||||
long block = ext2_alloc_block(fs);
|
||||
|
||||
if (block < 0)
|
||||
return block;
|
||||
|
||||
size_t num_blocks = (inode->blocks * fs->bdev->block_size) / fs->block_size;
|
||||
size_t ino_blocks_per_block = fs->block_size / fs->bdev->block_size;
|
||||
|
||||
/* there has to be a better way to do this -- TOO BAD! */
|
||||
if (num_blocks < 12) {
|
||||
inode->block[num_blocks] = block;
|
||||
} else if (num_blocks < 12 + fs->indir_block_size) {
|
||||
if (num_blocks == 12) {
|
||||
/* allocate indirect block */
|
||||
long indir_block = ext2_alloc_block(fs);
|
||||
if (indir_block < 0)
|
||||
return indir_block;
|
||||
|
||||
inode->block_indirect = indir_block;
|
||||
inode->blocks += ino_blocks_per_block;
|
||||
}
|
||||
|
||||
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
ext2_inode_read_block(fs, inode, indir_buf, 12);
|
||||
|
||||
indir_buf[num_blocks - 12] = block;
|
||||
|
||||
ext2_inode_write_block(fs, inode, indir_buf, 12);
|
||||
|
||||
kfree(indir_buf);
|
||||
} else if (num_blocks < 12 + fs->indir_block_size + fs->indir_block_size * fs->indir_block_size) {
|
||||
if (num_blocks == 12 + fs->indir_block_size) {
|
||||
/* allocate double indirect block */
|
||||
long indir_block = ext2_alloc_block(fs);
|
||||
if (indir_block < 0)
|
||||
return indir_block;
|
||||
|
||||
inode->block_dindirect = indir_block;
|
||||
inode->blocks += ino_blocks_per_block;
|
||||
}
|
||||
|
||||
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
ext2_inode_read_block(fs, inode, indir_buf, 13);
|
||||
|
||||
uint32_t *indir_buf2 = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf2) {
|
||||
kfree(indir_buf);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
if ((num_blocks - 12 - fs->indir_block_size) % fs->indir_block_size == 0) {
|
||||
/* allocate indirect block */
|
||||
long indir_block = ext2_alloc_block(fs);
|
||||
if (indir_block < 0) {
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
return indir_block;
|
||||
}
|
||||
|
||||
indir_buf[(num_blocks - 12 - fs->indir_block_size) / fs->indir_block_size] = indir_block;
|
||||
inode->blocks += ino_blocks_per_block;
|
||||
}
|
||||
|
||||
ext2_inode_read_block(fs, inode, indir_buf2,
|
||||
indir_buf[(num_blocks - 12 - fs->indir_block_size) / fs->indir_block_size]);
|
||||
|
||||
indir_buf2[(num_blocks - 12 - fs->indir_block_size) % fs->indir_block_size] = block;
|
||||
|
||||
ext2_inode_write_block(fs, inode, indir_buf2,
|
||||
indir_buf[(num_blocks - 12 - fs->indir_block_size) / fs->indir_block_size]);
|
||||
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
} else if (num_blocks < 12 + fs->indir_block_size + fs->indir_block_size * fs->indir_block_size +
|
||||
fs->indir_block_size * fs->indir_block_size * fs->indir_block_size) {
|
||||
if (num_blocks == 12 + fs->indir_block_size + fs->indir_block_size * fs->indir_block_size) {
|
||||
/* allocate triple indirect block */
|
||||
long indir_block = ext2_alloc_block(fs);
|
||||
if (indir_block < 0)
|
||||
return indir_block;
|
||||
|
||||
inode->block_tindirect = indir_block;
|
||||
inode->blocks += ino_blocks_per_block;
|
||||
}
|
||||
|
||||
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
ext2_inode_read_block(fs, inode, indir_buf, 14);
|
||||
|
||||
uint32_t *indir_buf2 = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf2) {
|
||||
kfree(indir_buf);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
ext2_inode_read_block(
|
||||
fs, inode, indir_buf2,
|
||||
indir_buf[(num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) /
|
||||
(fs->indir_block_size * fs->indir_block_size)]);
|
||||
|
||||
uint32_t *indir_buf3 = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf3) {
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
if ((num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
||||
(fs->indir_block_size * fs->indir_block_size) ==
|
||||
0) {
|
||||
/* allocate indirect block */
|
||||
long indir_block = ext2_alloc_block(fs);
|
||||
if (indir_block < 0) {
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
kfree(indir_buf3);
|
||||
return indir_block;
|
||||
}
|
||||
inode->blocks += ino_blocks_per_block;
|
||||
|
||||
indir_buf2[(num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) /
|
||||
(fs->indir_block_size * fs->indir_block_size)] = indir_block;
|
||||
}
|
||||
|
||||
ext2_inode_read_block(
|
||||
fs, inode, indir_buf3,
|
||||
indir_buf2[((num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
||||
(fs->indir_block_size * fs->indir_block_size)) /
|
||||
fs->indir_block_size]);
|
||||
|
||||
uint32_t *indir_buf4 = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf4) {
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
kfree(indir_buf3);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
if ((num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) % fs->indir_block_size ==
|
||||
0) {
|
||||
/* allocate indirect block */
|
||||
long indir_block = ext2_alloc_block(fs);
|
||||
if (indir_block < 0) {
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
kfree(indir_buf3);
|
||||
kfree(indir_buf4);
|
||||
return indir_block;
|
||||
}
|
||||
inode->blocks += ino_blocks_per_block;
|
||||
indir_buf3[((num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
||||
(fs->indir_block_size * fs->indir_block_size)) /
|
||||
fs->indir_block_size] = indir_block;
|
||||
}
|
||||
|
||||
indir_buf4[(num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
||||
fs->indir_block_size] = block;
|
||||
|
||||
ext2_inode_write_block(
|
||||
fs, inode, indir_buf4,
|
||||
indir_buf2[((num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
||||
(fs->indir_block_size * fs->indir_block_size)) /
|
||||
fs->indir_block_size]);
|
||||
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
kfree(indir_buf3);
|
||||
kfree(indir_buf4);
|
||||
}
|
||||
|
||||
/* write inode to disk */
|
||||
ext2_write_inode(fs, inode, inode_no);
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
static int ext2_free_block(struct ext2fs *fs, uint32_t block_no)
|
||||
{
|
||||
/* find the group that this block belongs to */
|
||||
uint32_t group = (block_no - 1) / fs->sb.blocks_per_group;
|
||||
uint32_t index = (block_no - 1) % fs->sb.blocks_per_group;
|
||||
|
||||
struct ext2_group_desc *bg = &fs->bgdt[group];
|
||||
|
||||
/* mark the block as free */
|
||||
char *buf = kmalloc(fs->block_size * fs->bgdt_num_blocks, ALLOC_DMA);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
ext2_read_block(fs, buf, bg->block_bitmap);
|
||||
|
||||
uint32_t byte = index / 8;
|
||||
uint32_t bit = index % 8;
|
||||
|
||||
uint8_t *byte_ptr = (uint8_t *)buf + byte;
|
||||
*byte_ptr &= ~(1 << bit);
|
||||
|
||||
bg->free_blocks_count++;
|
||||
fs->sb.free_blocks_count++;
|
||||
|
||||
/* update superblock and bgdt */
|
||||
ext2_write_block(fs, &fs->sb, 1);
|
||||
for (size_t i = 0; i < fs->bgdt_num_blocks; i++)
|
||||
ext2_write_block(fs, fs->bgdt + fs->block_size * i, fs->bgdt_blockno + i);
|
||||
|
||||
ext2_write_block(fs, buf, bg->block_bitmap);
|
||||
|
||||
kfree(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ext2_open_file(struct fs *vfs, struct file *file, const char *path)
|
||||
{
|
||||
/* find the inode of the file */
|
||||
struct ext2fs *fs = (struct ext2fs *)vfs->fs;
|
||||
|
|
@ -243,7 +546,9 @@ int ext2_open_file(struct fs *vfs, struct file *file, const char *path)
|
|||
return -ENOMEM;
|
||||
}
|
||||
|
||||
for (int i = 0; i < inode->blocks; i++) {
|
||||
/* inode blocks value is number of BDEV blocks, not EXT2 blocks. so we convert it */
|
||||
size_t ino_num_blocks = (inode->blocks * fs->bdev->block_size) / fs->block_size;
|
||||
for (int i = 0; i < ino_num_blocks; i++) {
|
||||
/* since this inode is a directory, we just read the blocks
|
||||
* sequentially until we find the file we're looking for
|
||||
*/
|
||||
|
|
@ -289,7 +594,7 @@ found:
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ext2_readdir(struct fs *fs, uint32_t ino_num, struct dirent **out)
|
||||
static int ext2_readdir(struct fs *fs, uint32_t ino_num, struct dirent **out)
|
||||
{
|
||||
struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
|
||||
if (!inode)
|
||||
|
|
@ -316,7 +621,8 @@ int ext2_readdir(struct fs *fs, uint32_t ino_num, struct dirent **out)
|
|||
|
||||
/* populate the dirent array with all the entries in the inode */
|
||||
int num_entries = 0;
|
||||
size_t dir_arr_size = inode->blocks * extfs->block_size;
|
||||
size_t ino_num_blocks = (inode->blocks * extfs->bdev->block_size) / extfs->block_size;
|
||||
size_t dir_arr_size = ino_num_blocks * extfs->block_size;
|
||||
dir_arr_size /= sizeof(struct dirent);
|
||||
|
||||
struct dirent *dir_arr = kmalloc(dir_arr_size * sizeof(struct dirent), ALLOC_KERN);
|
||||
|
|
@ -326,7 +632,7 @@ int ext2_readdir(struct fs *fs, uint32_t ino_num, struct dirent **out)
|
|||
return -ENOMEM;
|
||||
}
|
||||
|
||||
for (int i = 0; i < inode->blocks; i++) {
|
||||
for (int i = 0; i < ino_num_blocks; i++) {
|
||||
ext2_inode_read_block(extfs, inode, entry, i);
|
||||
|
||||
uint32_t rec_offset = 0;
|
||||
|
|
@ -354,17 +660,79 @@ int ext2_readdir(struct fs *fs, uint32_t ino_num, struct dirent **out)
|
|||
return num_entries;
|
||||
}
|
||||
|
||||
int ext2_close_file(struct fs *vfs, struct file *file)
|
||||
static int ext2_close_file(struct fs *vfs, struct file *file)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t offset, size_t count)
|
||||
static int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t offset, size_t count)
|
||||
{
|
||||
return 0;
|
||||
struct ext2fs *fs = vfs->fs;
|
||||
|
||||
if ((file->inode.mode & 0xF000) != EXT2_INO_REG)
|
||||
return -EISDIR;
|
||||
|
||||
size_t cur_num_blocks = (file->inode.blocks * fs->block_size) / fs->bdev->block_size;
|
||||
size_t block = offset / fs->block_size;
|
||||
size_t ooffset = offset;
|
||||
offset %= fs->block_size;
|
||||
|
||||
if (offset > file->size)
|
||||
return -EINVAL;
|
||||
|
||||
if (offset + count > file->size) {
|
||||
/* allocate more blocks */
|
||||
size_t new_num_blocks = npow2(offset + count) / fs->block_size;
|
||||
new_num_blocks -= MIN(cur_num_blocks, new_num_blocks);
|
||||
|
||||
for (size_t i = 0; i < new_num_blocks; i++) {
|
||||
long block_no = ext2_inode_alloc_block(fs, (struct ext2_inode *)&file->inode, file->inode_num);
|
||||
if (block_no < 0)
|
||||
return block_no;
|
||||
}
|
||||
}
|
||||
|
||||
void *block_buf = kmalloc(fs->block_size, ALLOC_KERN);
|
||||
|
||||
if (!block_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
size_t bufoffset = 0;
|
||||
size_t num = 0;
|
||||
|
||||
while (count > 0) {
|
||||
int retval = ext2_inode_read_block(fs, (struct ext2_inode *)&file->inode, block_buf, block);
|
||||
if (retval < 0) {
|
||||
kfree(block_buf);
|
||||
return retval;
|
||||
}
|
||||
|
||||
memcpy(block_buf + offset, buf + bufoffset, MIN(count, fs->block_size - offset));
|
||||
bufoffset += fs->block_size;
|
||||
offset = 0;
|
||||
|
||||
retval = ext2_inode_write_block(fs, (struct ext2_inode *)&file->inode, block_buf, block);
|
||||
if (retval < 0) {
|
||||
kfree(block_buf);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* adjust count accounting for the fact that we may have written less than a block */
|
||||
num += MIN(fs->block_size - offset, count);
|
||||
count -= MIN(fs->block_size - offset, count);
|
||||
offset = 0;
|
||||
block++;
|
||||
}
|
||||
|
||||
kfree(block_buf);
|
||||
|
||||
file->inode.size = MAX(file->inode.size, num + ooffset);
|
||||
ext2_write_inode(fs, (struct ext2_inode *)&file->inode, file->inode_num);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t offset, size_t count)
|
||||
static int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t offset, size_t count)
|
||||
{
|
||||
struct ext2fs *fs = vfs->fs;
|
||||
|
||||
|
|
@ -437,6 +805,8 @@ struct fs *ext2_init_fs(struct block_device *bdev)
|
|||
ext2_read_block(bdev->fs, buf, bgdt_block);
|
||||
|
||||
extfs->bgdt = (struct ext2_group_desc *)kmalloc(bgdt_size, ALLOC_KERN);
|
||||
extfs->bgdt_blockno = bgdt_block;
|
||||
extfs->bgdt_num_blocks = npow2(bgdt_size) / extfs->block_size;
|
||||
memcpy(extfs->bgdt, buf, bgdt_size);
|
||||
|
||||
kfree(buf);
|
||||
|
|
|
|||
24
fs/vfs.c
24
fs/vfs.c
|
|
@ -14,9 +14,29 @@ static int fd_counter = 0;
|
|||
|
||||
typedef struct fs *(*vfs_init_t)(struct block_device *);
|
||||
|
||||
int write(int fd, const void *buf, size_t count)
|
||||
int write(int fd, void *buf, size_t count)
|
||||
{
|
||||
return 0;
|
||||
struct rbnode *fdnode = rbt_search(kfd, fd);
|
||||
|
||||
if (!fdnode)
|
||||
return -EBADF;
|
||||
|
||||
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
|
||||
|
||||
if (fdesc->file.type == VFS_FILE_DIR)
|
||||
return -EISDIR;
|
||||
|
||||
if (fdesc->file.type != VFS_FILE_FILE)
|
||||
return -EBADF;
|
||||
|
||||
int ret = fdesc->fs->ops.write(fdesc->fs, &fdesc->file, buf, fdesc->pos, count);
|
||||
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
fdesc->pos = MIN(fdesc->pos + ret, fdesc->file.size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int read(int fd, void *buf, size_t count)
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#define EBADF 4
|
||||
#define ENOTDIR 5
|
||||
#define EISDIR 6
|
||||
#define ENOSPC 7
|
||||
|
||||
const char *strerror(int errnum);
|
||||
|
||||
|
|
|
|||
|
|
@ -144,6 +144,8 @@ struct ext2fs {
|
|||
struct ext2_superblock sb;
|
||||
struct block_device *bdev;
|
||||
|
||||
uint32_t bgdt_blockno;
|
||||
uint32_t bgdt_num_blocks;
|
||||
struct ext2_group_desc *bgdt;
|
||||
|
||||
uint32_t indir_block_size;
|
||||
|
|
@ -153,10 +155,4 @@ struct ext2fs {
|
|||
|
||||
struct fs *ext2_init_fs(struct block_device *bdev);
|
||||
|
||||
/* Filesystem block sizes are not equal to block device block sizes */
|
||||
int ext2_read_block(struct ext2fs *fs, void *buf, size_t block);
|
||||
int ext2_write_block(struct ext2fs *fs, void *buf, size_t block);
|
||||
int ext2_read_inode(struct ext2fs *fs, struct ext2_inode *out, uint32_t inode);
|
||||
int ext2_read_inode(struct ext2fs *fs, struct ext2_inode *in, uint32_t inode);
|
||||
|
||||
#endif /* _EXT2_H_ */
|
||||
|
|
|
|||
|
|
@ -47,6 +47,10 @@ struct inode {
|
|||
uint32_t file_acl;
|
||||
uint32_t dir_acl;
|
||||
uint32_t faddr;
|
||||
|
||||
uint64_t af_size;
|
||||
uint16_t frag_number;
|
||||
uint16_t frag_size;
|
||||
} PACKED; /* PACKED to allow for memcpy */
|
||||
|
||||
struct dirent {
|
||||
|
|
@ -105,7 +109,7 @@ struct fs {
|
|||
};
|
||||
|
||||
void vfs_init(const char *rootfs);
|
||||
int write(int fd, const void *buf, size_t count);
|
||||
int write(int fd, void *buf, size_t count);
|
||||
int read(int fd, void *buf, size_t count);
|
||||
int open(const char *pathname, int flags);
|
||||
int close(int fd);
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ static const char *errno_str[] = {
|
|||
[EBADF] = "Bad file descriptor",
|
||||
[ENOTDIR] = "Not a directory",
|
||||
[EISDIR] = "Is a directory",
|
||||
[ENOSPC] = "No space left on device",
|
||||
};
|
||||
|
||||
inline const char *strerror(int errnum)
|
||||
|
|
|
|||
Loading…
Reference in a new issue