44 lines
792 B
C
44 lines
792 B
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include <kernel/slab.h>
|
|
|
|
#include <fs/ext2.h>
|
|
|
|
static int ext2_read_super(struct block_device *bdev, struct ext2_superblock *sb_struct)
|
|
{
|
|
char *buf = kmalloc(1024, ALLOC_DMA);
|
|
if (!buf)
|
|
return -1;
|
|
|
|
int ret = block_read(bdev, buf, 2, 1);
|
|
if (ret < 0) {
|
|
kfree(buf);
|
|
return ret;
|
|
}
|
|
|
|
struct ext2_superblock *sb = (struct ext2_superblock *)buf;
|
|
if (sb->magic != EXT2_SUPER_MAGIC) {
|
|
kfree(buf);
|
|
return -1;
|
|
}
|
|
|
|
memcpy(sb_struct, buf, sizeof(*sb_struct));
|
|
kfree(buf);
|
|
|
|
return ret;
|
|
}
|
|
|
|
struct ext2fs *ext2_init_fs(struct block_device *bdev)
|
|
{
|
|
struct ext2fs *ret = kmalloc(sizeof(*ret), ALLOC_KERN);
|
|
|
|
if (!ret)
|
|
return NULL;
|
|
|
|
ret->bdev = bdev;
|
|
if (ext2_read_super(bdev, ret->sb) < 0) {
|
|
kfree(ret);
|
|
return NULL;
|
|
}
|
|
|
|
return ret;
|
|
}
|