1 Star 1 Fork 17

xiaoshzx/littlefs

forked from yocop/littlefs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
littlefs_vfs.c 12.64 KB
一键复制 编辑 原始数据 按行查看 历史
shuke 提交于 2021-02-22 21:25 . init version
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
/*
* Copyright (C) 2019-2020 Alibaba Group Holding Limited
*/
#include <vfs.h>
#include <vfs_inode.h>
#include <vfs_file.h>
#include <vfs_register.h>
#include <yoc/partition.h>
#include "lfs.h"
#include "littlefs_vfs.h"
#define WAIT_FOREVER 0xFFFFFFFF
typedef aos_mutex_t lfs_lock_t;
typedef struct {
struct lfs_config *config;
lfs_t *lfs;
lfs_lock_t *lock;
} lfs_manager_t;
typedef struct _lfsvfs_dir_t
{
aos_dir_t dir;
lfs_dir_t lfsdir;
aos_dirent_t cur_dirent;
} lfsvfs_dir_t;
partition_t lfs_hdl;
static char *lfs_mount_path;
static lfs_manager_t g_lfs_manager = {0};
static int32_t littlefs_block_read(const struct lfs_config *cfg, lfs_block_t block,
lfs_off_t off, void *dst, lfs_size_t size)
{
uint32_t off_set = off + cfg->block_size * block;
return partition_read(lfs_hdl, off_set, dst, size);
}
static int32_t littlefs_block_write(const struct lfs_config *cfg, lfs_block_t block,
lfs_off_t off, const void *dst, lfs_size_t size)
{
uint32_t off_set = off + cfg->block_size * block;
return partition_write(lfs_hdl, off_set, (void *)dst, size);
}
static int32_t littlefs_block_erase(const struct lfs_config *cfg, lfs_block_t block)
{
partition_info_t *part_info = hal_flash_get_info(lfs_hdl);
uint32_t off_set = cfg->block_size * block;
return partition_erase(lfs_hdl, off_set, cfg->block_size / part_info->sector_size);
}
static int32_t littlefs_block_sync(const struct lfs_config *cfg)
{
return 0;
}
struct lfs_config default_cfg = {
// block device operations
.read = littlefs_block_read,
.prog = littlefs_block_write,
.erase = littlefs_block_erase,
.sync = littlefs_block_sync,
};
/* Global FS lock init */
static void lfs_lock_create(lfs_lock_t *lock)
{
aos_mutex_new(lock);
}
/* Global FS lock destroy */
static void lfs_lock_destory(lfs_lock_t *lock)
{
aos_mutex_free(lock);
}
static void lfs_lock(lfs_lock_t *lock)
{
aos_mutex_lock(lock, WAIT_FOREVER);
}
static void lfs_unlock(lfs_lock_t *lock)
{
aos_mutex_unlock(lock);
}
/* Relative path convert */
static char *path_convert(const char *path)
{
int32_t len, prefix;
char *target_path, *p;
if (path == NULL) {
return NULL;
}
len = strlen(path);
prefix = strlen(lfs_mount_path);
if (strncmp(lfs_mount_path, path, prefix) != 0) {
return NULL;
}
len = len - prefix;
target_path =(char *)aos_malloc(len + 1);
if (target_path == NULL) {
return NULL;
}
memset(target_path, 0, len + 1);
if (len > 0) {
p = (char *)(path + prefix + 1);
memcpy(target_path, p, len - 1);
}
target_path[len] = '\0';
return target_path;
}
static int32_t mode_convert(int32_t flags)
{
int32_t mode, res = 0;
mode = flags & O_ACCMODE;
if (mode == O_RDONLY) {
res |= LFS_O_RDONLY;
} else if (mode == O_WRONLY) {
res |= LFS_O_WRONLY;
} else if (mode == O_RDWR) {
res |= LFS_O_RDWR;
}
if (flags & O_CREAT) {
res |= LFS_O_CREAT;
}
if (flags & O_EXCL) {
res |= LFS_O_EXCL;
}
if (flags & O_TRUNC) {
res |= LFS_O_TRUNC;
}
if (flags & O_APPEND) {
res |= LFS_O_CREAT | LFS_O_APPEND;
}
return res;
}
static int32_t _lfs_deinit(void)
{
if (g_lfs_manager.lfs != NULL) {
aos_free(g_lfs_manager.lfs);
}
if (g_lfs_manager.lock != NULL) {
lfs_lock_destory(g_lfs_manager.lock);
aos_free(g_lfs_manager.lock);
}
return 0;
}
static int32_t _lfs_init(void)
{
/* Set LFS default config */
g_lfs_manager.config = &default_cfg;
/* Create LFS Global Lock */
g_lfs_manager.lock = (lfs_lock_t *)aos_malloc(sizeof(lfs_lock_t));
if (g_lfs_manager.lock == NULL) {
goto ERROR;
}
lfs_lock_create(g_lfs_manager.lock);
/* Create LFS struct */
g_lfs_manager.lfs = (lfs_t *)aos_malloc(sizeof(lfs_t));
if (g_lfs_manager.lfs == NULL) {
goto ERROR;
}
return 0;
ERROR:
_lfs_deinit();
return -1;
}
static int32_t lfs_vfs_open(file_t *fp, const char *path, int32_t flags)
{
int res;
char *target_path = NULL;
lfs_file_t *file = NULL;
target_path = path_convert(path);
if (target_path == NULL) {
return -EINVAL;
}
if (!strlen(target_path)) {
return -EINVAL;
}
file = (lfs_file_t *)aos_malloc(sizeof(lfs_file_t));
if (file == NULL) {
aos_free(target_path);
return -EINVAL;
}
lfs_lock(g_lfs_manager.lock);
res = lfs_file_open(g_lfs_manager.lfs, file, target_path, mode_convert(flags));
lfs_unlock(g_lfs_manager.lock);
if (res != LFS_ERR_OK) {
aos_free(target_path);
aos_free(file);
return res;
} else {
fp->f_arg = (void *)file;
}
aos_free(target_path);
return res;
}
static int32_t lfs_vfs_close(file_t *fp)
{
int res = LFS_ERR_INVAL;
lfs_file_t *file = (lfs_file_t *)(fp->f_arg);
lfs_lock(g_lfs_manager.lock);
res = lfs_file_close(g_lfs_manager.lfs, file);
lfs_unlock(g_lfs_manager.lock);
if (res == LFS_ERR_OK) {
aos_free(file);
}
return res;
}
static ssize_t lfs_vfs_read(file_t *fp, char *buf, size_t len)
{
int nbytes;
lfs_file_t *file = (lfs_file_t *)(fp->f_arg);
lfs_lock(g_lfs_manager.lock);
nbytes = lfs_file_read(g_lfs_manager.lfs, file, buf, len);
lfs_unlock(g_lfs_manager.lock);
return nbytes;
}
static ssize_t lfs_vfs_write(file_t *fp, const char *buf, size_t len)
{
int nbytes;
lfs_file_t *file = (lfs_file_t *)(fp->f_arg);
lfs_lock(g_lfs_manager.lock);
nbytes = lfs_file_write(g_lfs_manager.lfs, file, buf, len);
lfs_unlock(g_lfs_manager.lock);
return nbytes;
}
static off_t lfs_vfs_lseek(file_t *fp, off_t off, int32_t whence)
{
off_t res;
lfs_file_t *file = (lfs_file_t *)(fp->f_arg);
lfs_lock(g_lfs_manager.lock);
res = lfs_file_seek(g_lfs_manager.lfs, file, off, whence);
lfs_unlock(g_lfs_manager.lock);
return res;
}
static int32_t lfs_vfs_sync(file_t *fp)
{
int res;
lfs_file_t *file = (lfs_file_t *)(fp->f_arg);
lfs_lock(g_lfs_manager.lock);
res = lfs_file_sync(g_lfs_manager.lfs, file);
lfs_unlock(g_lfs_manager.lock);
return res;
}
static int32_t lfs_vfs_stat(file_t *fp, const char *path, struct stat *st)
{
struct lfs_info s;
int res;
char *target_path = NULL;
target_path = path_convert(path);
if (target_path == NULL) {
return -EINVAL;
}
lfs_lock(g_lfs_manager.lock);
res = lfs_stat(g_lfs_manager.lfs, target_path, &s);
lfs_unlock(g_lfs_manager.lock);
if (res == LFS_ERR_OK) {
st->st_size = s.size;
st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO |
((s.type == LFS_TYPE_DIR ? S_IFDIR : S_IFREG));
}
aos_free(target_path);
return res;
}
static int32_t lfs_vfs_remove(file_t *fp, const char *path)
{
int res;
char *target_path = NULL;
target_path = path_convert(path);
if (target_path == NULL) {
return -EINVAL;
}
lfs_lock(g_lfs_manager.lock);
res = lfs_remove(g_lfs_manager.lfs, target_path);
lfs_unlock(g_lfs_manager.lock);
aos_free(target_path);
return res;
}
static int32_t lfs_vfs_rename(file_t *fp, const char *oldpath, const char *newpath)
{
int32_t ret;
char *oldname = NULL;
char *newname = NULL;
oldname = path_convert(oldpath);
if (!oldname) {
return -EINVAL;
}
newname = path_convert(newpath);
if (!newname) {
aos_free(oldname);
return -EINVAL;
}
lfs_lock(g_lfs_manager.lock);
ret = lfs_rename(g_lfs_manager.lfs, oldname, newname);
lfs_unlock(g_lfs_manager.lock);
aos_free(oldname);
aos_free(newname);
return ret;
}
static aos_dir_t *lfs_vfs_opendir(file_t *fp, const char *path)
{
lfsvfs_dir_t *lfsvfs_dir = NULL;
char *relpath = NULL;
int32_t ret;
relpath = path_convert(path);
if (!relpath) {
return NULL;
}
lfsvfs_dir = (lfsvfs_dir_t *)aos_malloc(sizeof(lfsvfs_dir_t) + LFS_NAME_MAX + 1);
if (!lfsvfs_dir) {
aos_free(relpath);
return NULL;
}
memset(lfsvfs_dir, 0, sizeof(lfsvfs_dir_t) + LFS_NAME_MAX + 1);
lfs_lock(g_lfs_manager.lock);
ret = lfs_dir_open(g_lfs_manager.lfs, &lfsvfs_dir->lfsdir, relpath);
lfs_unlock(g_lfs_manager.lock);
if (ret != LFS_ERR_OK) {
aos_free(relpath);
aos_free(lfsvfs_dir);
return NULL;
}
aos_free(relpath);
return (aos_dir_t *)lfsvfs_dir;
}
static aos_dirent_t *lfs_vfs_readdir(file_t *fp, aos_dir_t *dir)
{
lfsvfs_dir_t *lfsvfs_dir = (lfsvfs_dir_t*)dir;
struct lfs_info info;
int32_t ret;
if (!lfsvfs_dir) {
return NULL;
}
lfs_lock(g_lfs_manager.lock);
ret = lfs_dir_read(g_lfs_manager.lfs, &lfsvfs_dir->lfsdir, &info);
lfs_unlock(g_lfs_manager.lock);
if (ret != true) {
return NULL;
}
if (info.name[0] == 0) {
return NULL;
}
lfsvfs_dir->cur_dirent.d_ino = 0;
lfsvfs_dir->cur_dirent.d_type = info.type;
strncpy(lfsvfs_dir->cur_dirent.d_name, info.name, LFS_NAME_MAX);
lfsvfs_dir->cur_dirent.d_name[LFS_NAME_MAX] = '\0';
return &lfsvfs_dir->cur_dirent;
}
static int32_t lfs_vfs_closedir(file_t *fp, aos_dir_t *dir)
{
lfsvfs_dir_t *lfsvfs_dir = (lfsvfs_dir_t *)dir;
int32_t ret;
if (!lfsvfs_dir) {
return -EINVAL;
}
lfs_lock(g_lfs_manager.lock);
ret = lfs_dir_close(g_lfs_manager.lfs, &lfsvfs_dir->lfsdir);
lfs_unlock(g_lfs_manager.lock);
if (ret != 0) {
return ret;
}
aos_free(lfsvfs_dir);
return ret;
}
static int32_t lfs_vfs_mkdir(file_t *fp, const char *path)
{
int32_t ret;
char *pathname = NULL;
pathname = path_convert(path);
if (!pathname) {
return -EINVAL;
}
lfs_lock(g_lfs_manager.lock);
ret = lfs_mkdir(g_lfs_manager.lfs, pathname);
lfs_unlock(g_lfs_manager.lock);
aos_free(pathname);
return ret;
}
static int32_t lfs_vfs_rmdir(file_t *fp, const char *path)
{
int32_t ret;
char *pathname = NULL;
pathname = path_convert(path);
if (!pathname) {
return -EINVAL;
}
lfs_lock(g_lfs_manager.lock);
ret = lfs_remove(g_lfs_manager.lfs, pathname);
lfs_unlock(g_lfs_manager.lock);
aos_free(pathname);
return ret;
}
static const fs_ops_t littlefs_ops = {
.open = &lfs_vfs_open,
.close = &lfs_vfs_close,
.read = &lfs_vfs_read,
.write = &lfs_vfs_write,
.lseek = &lfs_vfs_lseek,
.sync = &lfs_vfs_sync,
.stat = &lfs_vfs_stat,
.unlink = &lfs_vfs_remove,
.rename = &lfs_vfs_rename,
.opendir = &lfs_vfs_opendir,
.readdir = &lfs_vfs_readdir,
.closedir = &lfs_vfs_closedir,
.mkdir = &lfs_vfs_mkdir,
.rmdir = &lfs_vfs_rmdir,
.ioctl = NULL,
};
int lfs_vfs_mount(void)
{
int res;
res = _lfs_init();
if (res != LFS_ERR_OK) {
return res;
}
lfs_lock(g_lfs_manager.lock);
res = lfs_mount(g_lfs_manager.lfs, g_lfs_manager.config);
#ifdef LITTLEFS_FORMAT
if (res != LFS_ERR_OK) {
res = lfs_format(g_lfs_manager.lfs, g_lfs_manager.config);
if (res != LFS_ERR_OK) {
goto ERROR;
}
res = lfs_mount(g_lfs_manager.lfs, g_lfs_manager.config);
if (res != LFS_ERR_OK) {
goto ERROR;
}
}
#endif
lfs_unlock(g_lfs_manager.lock);
return res;
#ifdef LITTLEFS_FORMAT
ERROR:
lfs_unlock(g_lfs_manager.lock);
_lfs_deinit();
return res;
#endif
}
int lfs_vfs_unmount(void)
{
lfs_unmount(g_lfs_manager.lfs);
_lfs_deinit();
return LFS_ERR_OK;
}
int32_t vfs_lfs_register(char *partition_desc)
{
lfs_hdl = partition_open(partition_desc);
aos_check(lfs_hdl > 0, EIO);
lfs_mount_path = (char *)aos_malloc(sizeof(partition_desc) + 1);
sprintf(lfs_mount_path, "/%s", partition_desc);
// block device configuration
partition_info_t *part_info = hal_flash_get_info(lfs_hdl);
default_cfg.block_size = part_info->sector_size;
default_cfg.block_count = part_info->length / part_info->sector_size;
default_cfg.read_size = 256;
default_cfg.prog_size = 256;
default_cfg.cache_size = 256;
default_cfg.lookahead_size = 16;
default_cfg.block_cycles = 1000;
int res = lfs_vfs_mount();
if (res == LFS_ERR_OK) {
return aos_register_fs(lfs_mount_path, &littlefs_ops, NULL);
}
return res;
}
int32_t vfs_lfs_unregister(void)
{
lfs_vfs_unmount();
partition_close(lfs_hdl);
aos_unregister_fs(lfs_mount_path);
free(lfs_mount_path);
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hongzhenxiang/littlefs.git
[email protected]:hongzhenxiang/littlefs.git
hongzhenxiang
littlefs
littlefs
master

搜索帮助