ext2: slightly clean up indirect read/alloc
This commit is contained in:
parent
f8d84c7780
commit
0a1b73edc2
4 changed files with 275 additions and 169 deletions
180
fs/ext2.c
180
fs/ext2.c
|
|
@ -5,7 +5,15 @@
|
||||||
#include <fs/ext2.h>
|
#include <fs/ext2.h>
|
||||||
#include <fs/vfs.h>
|
#include <fs/vfs.h>
|
||||||
|
|
||||||
|
#define N_DIRECT 12
|
||||||
|
#define N_INDIR (fs->block_size / sizeof(uint32_t))
|
||||||
|
#define N_DINDIR (N_INDIR * N_INDIR)
|
||||||
|
#define N_TINDIR (N_DINDIR * N_INDIR + N_DINDIR + N_INDIR)
|
||||||
|
|
||||||
|
#define N_PER_INDIRECT (fs->block_size / sizeof(uint32_t))
|
||||||
|
|
||||||
#define ITYPE_DECL(_ino, _de) [(_ino >> 12)] = (_de)
|
#define ITYPE_DECL(_ino, _de) [(_ino >> 12)] = (_de)
|
||||||
|
|
||||||
static uint8_t inode_to_ftype[] = {
|
static uint8_t inode_to_ftype[] = {
|
||||||
ITYPE_DECL(EXT2_INO_FIFO, VFS_FILE_FIFO), ITYPE_DECL(EXT2_INO_CHARDEV, VFS_FILE_CHARDEV),
|
ITYPE_DECL(EXT2_INO_FIFO, VFS_FILE_FIFO), ITYPE_DECL(EXT2_INO_CHARDEV, VFS_FILE_CHARDEV),
|
||||||
ITYPE_DECL(EXT2_INO_DIR, VFS_FILE_DIR), ITYPE_DECL(EXT2_INO_BLKDEV, VFS_FILE_BLKDEV),
|
ITYPE_DECL(EXT2_INO_DIR, VFS_FILE_DIR), ITYPE_DECL(EXT2_INO_BLKDEV, VFS_FILE_BLKDEV),
|
||||||
|
|
@ -53,31 +61,39 @@ static int ext2_read_block(struct ext2fs *fs, void *buf, size_t block)
|
||||||
return block_read(fs->bdev, buf, bdev_block, num_blocks_read);
|
return block_read(fs->bdev, buf, bdev_block, num_blocks_read);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* nasty function, but it works */
|
static long ext2_inode_get_block_indir(struct ext2fs *fs, struct ext2_inode *ino, size_t index)
|
||||||
static long ext2_inode_get_block_indir(struct ext2fs *fs, struct ext2_inode *inode, size_t ino_block)
|
|
||||||
{
|
{
|
||||||
if (ino_block < 12)
|
if (index < N_DIRECT)
|
||||||
return inode->block[ino_block];
|
return ino->block[index];
|
||||||
|
|
||||||
if (ino_block < 12 + fs->indir_block_size) {
|
/* only search for single indirect blocks */
|
||||||
|
if (index < N_DIRECT + N_INDIR) {
|
||||||
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||||
if (!indir_buf)
|
if (!indir_buf)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
ext2_read_block(fs, indir_buf, inode->block[12]);
|
ext2_read_block(fs, indir_buf, ino->block_indirect);
|
||||||
|
|
||||||
long ret = indir_buf[ino_block - 12];
|
long ret = indir_buf[index - N_DIRECT];
|
||||||
kfree(indir_buf);
|
kfree(indir_buf);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ino_block < 12 + fs->indir_block_size + fs->indir_block_size * fs->indir_block_size) {
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static long ext2_inode_get_block_dindir(struct ext2fs *fs, struct ext2_inode *ino, size_t index)
|
||||||
|
{
|
||||||
|
if (index < N_DIRECT + N_INDIR)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (index < N_DIRECT + N_INDIR + N_DINDIR) {
|
||||||
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||||
if (!indir_buf)
|
if (!indir_buf)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
ext2_read_block(fs, indir_buf, inode->block[13]);
|
ext2_read_block(fs, indir_buf, ino->block_dindirect);
|
||||||
|
|
||||||
uint32_t *indir_buf2 = kmalloc(fs->block_size, ALLOC_DMA);
|
uint32_t *indir_buf2 = kmalloc(fs->block_size, ALLOC_DMA);
|
||||||
if (!indir_buf2) {
|
if (!indir_buf2) {
|
||||||
|
|
@ -85,22 +101,29 @@ static long ext2_inode_get_block_indir(struct ext2fs *fs, struct ext2_inode *ino
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
ext2_read_block(fs, indir_buf2, indir_buf[(ino_block - 12 - fs->indir_block_size) / fs->indir_block_size]);
|
ext2_read_block(fs, indir_buf2, indir_buf[(index - N_DIRECT - N_INDIR) / N_INDIR]);
|
||||||
|
|
||||||
long ret = indir_buf2[(ino_block - 12 - fs->indir_block_size) % fs->indir_block_size];
|
long ret = indir_buf2[(index - N_DIRECT - N_INDIR) % N_INDIR];
|
||||||
kfree(indir_buf);
|
kfree(indir_buf);
|
||||||
kfree(indir_buf2);
|
kfree(indir_buf2);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ino_block < 12 + fs->indir_block_size + fs->indir_block_size * fs->indir_block_size +
|
return -EINVAL;
|
||||||
fs->indir_block_size * fs->indir_block_size * fs->indir_block_size) {
|
}
|
||||||
|
|
||||||
|
static long ext2_inode_get_block_tindir(struct ext2fs *fs, struct ext2_inode *ino, size_t index)
|
||||||
|
{
|
||||||
|
if (index < N_DIRECT + N_INDIR + N_DINDIR)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (index < N_DIRECT + N_INDIR + N_DINDIR + N_TINDIR) {
|
||||||
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||||
if (!indir_buf)
|
if (!indir_buf)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
ext2_read_block(fs, indir_buf, inode->block[14]);
|
ext2_read_block(fs, indir_buf, ino->block_tindirect);
|
||||||
|
|
||||||
uint32_t *indir_buf2 = kmalloc(fs->block_size, ALLOC_DMA);
|
uint32_t *indir_buf2 = kmalloc(fs->block_size, ALLOC_DMA);
|
||||||
if (!indir_buf2) {
|
if (!indir_buf2) {
|
||||||
|
|
@ -108,9 +131,7 @@ static long ext2_inode_get_block_indir(struct ext2fs *fs, struct ext2_inode *ino
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
ext2_read_block(fs, indir_buf2,
|
ext2_read_block(fs, indir_buf2, indir_buf[(index - N_DIRECT - N_INDIR - N_DINDIR) / N_DINDIR]);
|
||||||
indir_buf[(ino_block - 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);
|
uint32_t *indir_buf3 = kmalloc(fs->block_size, ALLOC_DMA);
|
||||||
if (!indir_buf3) {
|
if (!indir_buf3) {
|
||||||
|
|
@ -119,14 +140,9 @@ static long ext2_inode_get_block_indir(struct ext2fs *fs, struct ext2_inode *ino
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
ext2_read_block(fs, indir_buf3,
|
ext2_read_block(fs, indir_buf3, indir_buf2[((index - N_DIRECT - N_INDIR - N_DINDIR) % N_DINDIR) / N_INDIR]);
|
||||||
indir_buf2[((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) %
|
long ret = indir_buf3[((index - N_DIRECT - N_INDIR - N_DINDIR) % N_DINDIR) % N_INDIR];
|
||||||
(fs->indir_block_size * fs->indir_block_size)) %
|
|
||||||
fs->indir_block_size];
|
|
||||||
kfree(indir_buf);
|
kfree(indir_buf);
|
||||||
kfree(indir_buf2);
|
kfree(indir_buf2);
|
||||||
kfree(indir_buf3);
|
kfree(indir_buf3);
|
||||||
|
|
@ -137,9 +153,26 @@ static long ext2_inode_get_block_indir(struct ext2fs *fs, struct ext2_inode *ino
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* nasty function, but it works */
|
||||||
|
static long ext2_inode_get_block(struct ext2fs *fs, struct ext2_inode *inode, size_t ino_block)
|
||||||
|
{
|
||||||
|
if (ino_block < N_DIRECT)
|
||||||
|
return inode->block[ino_block];
|
||||||
|
|
||||||
|
if (ino_block < (N_DIRECT + N_INDIR))
|
||||||
|
return ext2_inode_get_block_indir(fs, inode, ino_block);
|
||||||
|
|
||||||
|
if (ino_block < N_DIRECT + (N_INDIR * N_INDIR))
|
||||||
|
return ext2_inode_get_block_dindir(fs, inode, ino_block);
|
||||||
|
|
||||||
|
return ext2_inode_get_block_tindir(fs, inode, ino_block);
|
||||||
|
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
static int ext2_inode_read_block(struct ext2fs *fs, struct ext2_inode *inode, void *buf, size_t ino_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);
|
long ret = ext2_inode_get_block(fs, inode, ino_block);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
|
@ -158,7 +191,7 @@ static int ext2_write_block(struct ext2fs *fs, void *buf, size_t block)
|
||||||
|
|
||||||
static int ext2_inode_write_block(struct ext2fs *fs, struct ext2_inode *inode, void *buf, size_t ino_block)
|
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);
|
long ret = ext2_inode_get_block(fs, inode, ino_block);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
|
@ -289,22 +322,14 @@ static long ext2_alloc_block(struct ext2fs *fs)
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
}
|
}
|
||||||
|
|
||||||
static long ext2_inode_alloc_block(struct ext2fs *fs, struct ext2_inode *inode, ino_t inode_no)
|
static long ext2_inode_alloc_block_indir(struct ext2fs *fs, struct ext2_inode *inode)
|
||||||
{
|
{
|
||||||
/* allocate inode block and handle indirect if necessary */
|
|
||||||
long block = ext2_alloc_block(fs);
|
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 num_blocks = (inode->blocks * fs->bdev->block_size) / fs->block_size;
|
||||||
size_t ino_blocks_per_block = fs->block_size / fs->bdev->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 == N_DIRECT) {
|
||||||
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 */
|
/* allocate indirect block */
|
||||||
long indir_block = ext2_alloc_block(fs);
|
long indir_block = ext2_alloc_block(fs);
|
||||||
if (indir_block < 0)
|
if (indir_block < 0)
|
||||||
|
|
@ -325,8 +350,18 @@ static long ext2_inode_alloc_block(struct ext2fs *fs, struct ext2_inode *inode,
|
||||||
ext2_inode_write_block(fs, inode, indir_buf, 12);
|
ext2_inode_write_block(fs, inode, indir_buf, 12);
|
||||||
|
|
||||||
kfree(indir_buf);
|
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) {
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
static long ext2_inode_alloc_block_dindir(struct ext2fs *fs, struct ext2_inode *inode)
|
||||||
|
{
|
||||||
|
long block = ext2_alloc_block(fs);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (num_blocks == N_DIRECT + fs->indir_block_size) {
|
||||||
/* allocate double indirect block */
|
/* allocate double indirect block */
|
||||||
long indir_block = ext2_alloc_block(fs);
|
long indir_block = ext2_alloc_block(fs);
|
||||||
if (indir_block < 0)
|
if (indir_block < 0)
|
||||||
|
|
@ -361,18 +396,25 @@ static long ext2_inode_alloc_block(struct ext2fs *fs, struct ext2_inode *inode,
|
||||||
inode->blocks += ino_blocks_per_block;
|
inode->blocks += ino_blocks_per_block;
|
||||||
}
|
}
|
||||||
|
|
||||||
ext2_inode_read_block(fs, inode, indir_buf2,
|
ext2_inode_read_block(fs, inode, indir_buf2, indir_buf[(num_blocks - 12 - fs->indir_block_size) / fs->indir_block_size]);
|
||||||
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;
|
indir_buf2[(num_blocks - 12 - fs->indir_block_size) % fs->indir_block_size] = block;
|
||||||
|
|
||||||
ext2_inode_write_block(fs, inode, indir_buf2,
|
ext2_inode_write_block(fs, inode, indir_buf2, indir_buf[(num_blocks - 12 - fs->indir_block_size) / fs->indir_block_size]);
|
||||||
indir_buf[(num_blocks - 12 - fs->indir_block_size) / fs->indir_block_size]);
|
|
||||||
|
|
||||||
kfree(indir_buf);
|
kfree(indir_buf);
|
||||||
kfree(indir_buf2);
|
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) {
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
static long ext2_inode_alloc_block_tindir(struct ext2fs *fs, struct ext2_inode *inode)
|
||||||
|
{
|
||||||
|
long block = ext2_alloc_block(fs);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
if (num_blocks == 12 + 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 */
|
/* allocate triple indirect block */
|
||||||
long indir_block = ext2_alloc_block(fs);
|
long indir_block = ext2_alloc_block(fs);
|
||||||
|
|
@ -395,8 +437,7 @@ static long ext2_inode_alloc_block(struct ext2fs *fs, struct ext2_inode *inode,
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
ext2_inode_read_block(
|
ext2_inode_read_block(fs, inode, indir_buf2,
|
||||||
fs, inode, indir_buf2,
|
|
||||||
indir_buf[(num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) /
|
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)]);
|
(fs->indir_block_size * fs->indir_block_size)]);
|
||||||
|
|
||||||
|
|
@ -424,8 +465,7 @@ static long ext2_inode_alloc_block(struct ext2fs *fs, struct ext2_inode *inode,
|
||||||
(fs->indir_block_size * fs->indir_block_size)] = indir_block;
|
(fs->indir_block_size * fs->indir_block_size)] = indir_block;
|
||||||
}
|
}
|
||||||
|
|
||||||
ext2_inode_read_block(
|
ext2_inode_read_block(fs, inode, indir_buf3,
|
||||||
fs, inode, indir_buf3,
|
|
||||||
indir_buf2[((num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
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 * fs->indir_block_size)) /
|
||||||
fs->indir_block_size]);
|
fs->indir_block_size]);
|
||||||
|
|
@ -438,8 +478,7 @@ static long ext2_inode_alloc_block(struct ext2fs *fs, struct ext2_inode *inode,
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((num_blocks - 12 - 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) % fs->indir_block_size == 0) {
|
||||||
0) {
|
|
||||||
/* allocate indirect block */
|
/* allocate indirect block */
|
||||||
long indir_block = ext2_alloc_block(fs);
|
long indir_block = ext2_alloc_block(fs);
|
||||||
if (indir_block < 0) {
|
if (indir_block < 0) {
|
||||||
|
|
@ -455,11 +494,10 @@ static long ext2_inode_alloc_block(struct ext2fs *fs, struct ext2_inode *inode,
|
||||||
fs->indir_block_size] = indir_block;
|
fs->indir_block_size] = indir_block;
|
||||||
}
|
}
|
||||||
|
|
||||||
indir_buf4[(num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
indir_buf4[(num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) % fs->indir_block_size] =
|
||||||
fs->indir_block_size] = block;
|
block;
|
||||||
|
|
||||||
ext2_inode_write_block(
|
ext2_inode_write_block(fs, inode, indir_buf4,
|
||||||
fs, inode, indir_buf4,
|
|
||||||
indir_buf2[((num_blocks - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
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 * fs->indir_block_size)) /
|
||||||
fs->indir_block_size]);
|
fs->indir_block_size]);
|
||||||
|
|
@ -468,8 +506,30 @@ static long ext2_inode_alloc_block(struct ext2fs *fs, struct ext2_inode *inode,
|
||||||
kfree(indir_buf2);
|
kfree(indir_buf2);
|
||||||
kfree(indir_buf3);
|
kfree(indir_buf3);
|
||||||
kfree(indir_buf4);
|
kfree(indir_buf4);
|
||||||
}
|
|
||||||
|
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (block < 0)
|
||||||
|
return block;
|
||||||
|
|
||||||
|
size_t num_blocks = (inode->blocks * fs->bdev->block_size) / fs->block_size;
|
||||||
|
|
||||||
|
if (num_blocks < 12) {
|
||||||
|
block = ext2_alloc_block(fs);
|
||||||
|
inode->block[num_blocks] = block;
|
||||||
|
} else if (num_blocks < N_DIRECT + N_INDIR) {
|
||||||
|
block = ext2_inode_alloc_block_indir(fs, inode);
|
||||||
|
} else if (num_blocks < N_DIRECT + N_INDIR + N_DINDIR) {
|
||||||
|
block = ext2_inode_alloc_block_dindir(fs, inode);
|
||||||
|
} else if (num_blocks < N_DIRECT + N_INDIR + N_DINDIR + N_TINDIR) {
|
||||||
|
block = ext2_inode_alloc_block_tindir(fs, inode);
|
||||||
|
}
|
||||||
/* write inode to disk */
|
/* write inode to disk */
|
||||||
ext2_write_inode(fs, inode, inode_no);
|
ext2_write_inode(fs, inode, inode_no);
|
||||||
|
|
||||||
|
|
@ -558,8 +618,11 @@ static int ext2_open_file(struct fs *vfs, struct file *file, const char *path)
|
||||||
uint32_t rec_offset = 0;
|
uint32_t rec_offset = 0;
|
||||||
while (entry->inode && rec_offset < fs->block_size) {
|
while (entry->inode && rec_offset < fs->block_size) {
|
||||||
inode_no = entry->inode;
|
inode_no = entry->inode;
|
||||||
if (!strncmp(entry->name, token, MIN(entry->name_len, strlen(token))))
|
size_t token_len = strlen(token);
|
||||||
|
if (!strncmp(entry->name, token, MIN(entry->name_len, token_len))) {
|
||||||
|
if (token_len == entry->name_len || token_len == 0)
|
||||||
goto found;
|
goto found;
|
||||||
|
}
|
||||||
|
|
||||||
entry = (void *)entry + entry->rec_len;
|
entry = (void *)entry + entry->rec_len;
|
||||||
rec_offset += entry->rec_len;
|
rec_offset += entry->rec_len;
|
||||||
|
|
@ -675,7 +738,6 @@ static int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t
|
||||||
size_t cur_num_blocks = (file->inode.blocks * fs->block_size) / fs->bdev->block_size;
|
size_t cur_num_blocks = (file->inode.blocks * fs->block_size) / fs->bdev->block_size;
|
||||||
size_t block = offset / fs->block_size;
|
size_t block = offset / fs->block_size;
|
||||||
size_t ooffset = offset;
|
size_t ooffset = offset;
|
||||||
offset %= fs->block_size;
|
|
||||||
|
|
||||||
if (offset > file->size)
|
if (offset > file->size)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
@ -692,6 +754,7 @@ static int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
offset %= fs->block_size;
|
||||||
void *block_buf = kmalloc(fs->block_size, ALLOC_KERN);
|
void *block_buf = kmalloc(fs->block_size, ALLOC_KERN);
|
||||||
|
|
||||||
if (!block_buf)
|
if (!block_buf)
|
||||||
|
|
@ -740,7 +803,6 @@ static int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t o
|
||||||
return -EISDIR;
|
return -EISDIR;
|
||||||
|
|
||||||
size_t block = offset / fs->block_size;
|
size_t block = offset / fs->block_size;
|
||||||
offset %= fs->block_size;
|
|
||||||
|
|
||||||
if (offset > file->size)
|
if (offset > file->size)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
@ -748,6 +810,8 @@ static int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t o
|
||||||
if (offset + count > file->size)
|
if (offset + count > file->size)
|
||||||
count = (file->size - offset);
|
count = (file->size - offset);
|
||||||
|
|
||||||
|
offset %= fs->block_size;
|
||||||
|
|
||||||
size_t ret = count;
|
size_t ret = count;
|
||||||
|
|
||||||
void *block_buf = kmalloc(fs->block_size, ALLOC_KERN);
|
void *block_buf = kmalloc(fs->block_size, ALLOC_KERN);
|
||||||
|
|
|
||||||
14
fs/vfs.c
14
fs/vfs.c
|
|
@ -64,6 +64,20 @@ int read(int fd, void *buf, size_t count)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int statfd(int fd, struct statbuf *statbuf)
|
||||||
|
{
|
||||||
|
struct rbnode *fdnode = rbt_search(kfd, fd);
|
||||||
|
|
||||||
|
if (!fdnode)
|
||||||
|
return -EBADF;
|
||||||
|
|
||||||
|
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
|
||||||
|
|
||||||
|
statbuf->size = fdesc->file.size;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int open(const char *pathname, int flags)
|
int open(const char *pathname, int flags)
|
||||||
{
|
{
|
||||||
/* allocate a file descriptor */
|
/* allocate a file descriptor */
|
||||||
|
|
|
||||||
|
|
@ -108,12 +108,17 @@ struct fs {
|
||||||
char *mount_point;
|
char *mount_point;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct statbuf {
|
||||||
|
size_t size;
|
||||||
|
};
|
||||||
|
|
||||||
void vfs_init(const char *rootfs);
|
void vfs_init(const char *rootfs);
|
||||||
int write(int fd, void *buf, size_t count);
|
int write(int fd, void *buf, size_t count);
|
||||||
int read(int fd, void *buf, size_t count);
|
int read(int fd, void *buf, size_t count);
|
||||||
int open(const char *pathname, int flags);
|
int open(const char *pathname, int flags);
|
||||||
int close(int fd);
|
int close(int fd);
|
||||||
int seek(int fd, size_t offset, int whence);
|
int seek(int fd, size_t offset, int whence);
|
||||||
|
int statfd(int fd, struct statbuf *statbuf);
|
||||||
|
|
||||||
DIR *opendir(const char *name);
|
DIR *opendir(const char *name);
|
||||||
struct dirent *readdir(DIR *dir);
|
struct dirent *readdir(DIR *dir);
|
||||||
|
|
|
||||||
25
init/cmd.c
25
init/cmd.c
|
|
@ -12,6 +12,7 @@ int kcmd_help(int argc, char **argv);
|
||||||
int kcmd_ls(int argc, char **argv);
|
int kcmd_ls(int argc, char **argv);
|
||||||
int kcmd_clear(int argc, char **argv);
|
int kcmd_clear(int argc, char **argv);
|
||||||
int kcmd_cat(int argc, char **argv);
|
int kcmd_cat(int argc, char **argv);
|
||||||
|
int kcmd_stat(int argc, char **argv);
|
||||||
|
|
||||||
#define KCMD_DECL(name) \
|
#define KCMD_DECL(name) \
|
||||||
{ \
|
{ \
|
||||||
|
|
@ -23,7 +24,7 @@ struct kcmd {
|
||||||
int (*func)(int argc, char **argv);
|
int (*func)(int argc, char **argv);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct kcmd cmd_list[] = { KCMD_DECL(cat), KCMD_DECL(clear), KCMD_DECL(help), KCMD_DECL(ls) };
|
struct kcmd cmd_list[] = { KCMD_DECL(cat), KCMD_DECL(clear), KCMD_DECL(help), KCMD_DECL(ls), KCMD_DECL(stat) };
|
||||||
|
|
||||||
int kcmd_cat(int argc, char **argv)
|
int kcmd_cat(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
|
@ -99,6 +100,28 @@ int kcmd_ls(int argc, char **argv)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int kcmd_stat(int argc, char **argv)
|
||||||
|
{
|
||||||
|
if (!argv[1] || strempty(argv[1])) {
|
||||||
|
kprintf("Usage: stat <file>\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd = open(argv[1], 0);
|
||||||
|
|
||||||
|
struct statbuf st;
|
||||||
|
int ret = statfd(fd, &st);
|
||||||
|
if (ret < 0) {
|
||||||
|
kprintf("Failed to stat %s: %s\n", argv[1], strerror(-ret));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
kprintf("File: %s\n", argv[1]);
|
||||||
|
kprintf("Size: %d\n", st.size);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void kexec(const char *cmd)
|
void kexec(const char *cmd)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue