2019-01-09 21:26:51 +01:00
|
|
|
/****************************************************************************
|
2019-01-27 19:31:28 +01:00
|
|
|
* fs/littlefs/lfs_vfs.c
|
2019-01-09 21:26:51 +01:00
|
|
|
*
|
2020-04-21 09:22:31 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-01-09 21:26:51 +01:00
|
|
|
*
|
2020-04-21 09:22:31 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-01-09 21:26:51 +01:00
|
|
|
*
|
2020-04-21 09:22:31 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-01-09 21:26:51 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs/fs.h>
|
2021-08-25 03:50:15 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2020-04-21 09:22:31 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2019-01-09 21:26:51 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2019-01-27 19:31:28 +01:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statfs.h>
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2021-08-25 03:50:15 +02:00
|
|
|
#include "inode/inode.h"
|
2020-04-21 09:22:31 +02:00
|
|
|
#include "littlefs/lfs.h"
|
|
|
|
#include "littlefs/lfs_util.h"
|
2024-09-09 14:28:16 +02:00
|
|
|
#include "fs_heap.h"
|
2019-01-10 13:37:53 +01:00
|
|
|
|
2023-04-18 08:59:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_C99_BOOL
|
|
|
|
# error littlefs requires CONFIG_C99_BOOL to be selected
|
|
|
|
#endif
|
|
|
|
|
2019-01-09 21:26:51 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
struct littlefs_dir_s
|
|
|
|
{
|
|
|
|
struct fs_dirent_s base;
|
|
|
|
struct lfs_dir dir;
|
|
|
|
};
|
|
|
|
|
2021-03-22 10:39:44 +01:00
|
|
|
struct littlefs_file_s
|
|
|
|
{
|
|
|
|
struct lfs_file file;
|
|
|
|
int refs;
|
|
|
|
};
|
|
|
|
|
2020-03-30 12:22:26 +02:00
|
|
|
/* This structure represents the overall mountpoint state. An instance of
|
|
|
|
* this structure is retained as inode private data on each mountpoint that
|
|
|
|
* is mounted with a littlefs filesystem.
|
2019-01-27 19:31:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct littlefs_mountpt_s
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lock;
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct inode *drv;
|
2019-01-09 21:26:51 +01:00
|
|
|
struct mtd_geometry_s geo;
|
2020-04-21 09:22:31 +02:00
|
|
|
struct lfs_config cfg;
|
2021-02-24 06:26:58 +01:00
|
|
|
struct lfs lfs;
|
2019-01-09 21:26:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode);
|
2019-01-09 21:26:51 +01:00
|
|
|
static int littlefs_close(FAR struct file *filep);
|
|
|
|
static ssize_t littlefs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
2019-01-27 19:31:28 +01:00
|
|
|
static ssize_t littlefs_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
2019-01-09 21:26:51 +01:00
|
|
|
static off_t littlefs_seek(FAR struct file *filep, off_t offset,
|
|
|
|
int whence);
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_sync(FAR struct file *filep);
|
|
|
|
static int littlefs_dup(FAR const struct file *oldp,
|
|
|
|
FAR struct file *newp);
|
|
|
|
static int littlefs_fstat(FAR const struct file *filep,
|
|
|
|
FAR struct stat *buf);
|
|
|
|
static int littlefs_truncate(FAR struct file *filep,
|
|
|
|
off_t length);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
|
|
|
static int littlefs_opendir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir);
|
2019-01-09 21:26:51 +01:00
|
|
|
static int littlefs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int littlefs_readdir(FAR struct inode *mountpt,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry);
|
2019-01-09 21:26:51 +01:00
|
|
|
static int littlefs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_bind(FAR struct inode *driver,
|
|
|
|
FAR const void *data, FAR void **handle);
|
|
|
|
static int littlefs_unbind(FAR void *handle, FAR struct inode **driver,
|
|
|
|
unsigned int flags);
|
|
|
|
static int littlefs_statfs(FAR struct inode *mountpt,
|
|
|
|
FAR struct statfs *buf);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_unlink(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath);
|
2019-01-09 21:26:51 +01:00
|
|
|
static int littlefs_mkdir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath, mode_t mode);
|
|
|
|
static int littlefs_rmdir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath);
|
|
|
|
static int littlefs_rename(FAR struct inode *mountpt,
|
|
|
|
FAR const char *oldrelpath,
|
|
|
|
FAR const char *newrelpath);
|
|
|
|
static int littlefs_stat(FAR struct inode *mountpt,
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR const char *relpath, FAR struct stat *buf);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* See fs_mount.c -- this structure is explicitly extern'ed there.
|
|
|
|
* We use the old-fashioned kind of initializers so that this will compile
|
|
|
|
* with any compiler.
|
|
|
|
*/
|
|
|
|
|
2023-04-22 16:53:42 +02:00
|
|
|
const struct mountpt_operations g_littlefs_operations =
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
littlefs_open, /* open */
|
|
|
|
littlefs_close, /* close */
|
|
|
|
littlefs_read, /* read */
|
|
|
|
littlefs_write, /* write */
|
|
|
|
littlefs_seek, /* seek */
|
|
|
|
littlefs_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
littlefs_truncate, /* truncate */
|
2023-11-25 13:11:03 +01:00
|
|
|
NULL, /* poll */
|
2019-01-27 19:31:28 +01:00
|
|
|
|
|
|
|
littlefs_sync, /* sync */
|
|
|
|
littlefs_dup, /* dup */
|
|
|
|
littlefs_fstat, /* fstat */
|
2021-07-09 07:43:26 +02:00
|
|
|
NULL, /* fchstat */
|
2019-01-27 19:31:28 +01:00
|
|
|
|
|
|
|
littlefs_opendir, /* opendir */
|
|
|
|
littlefs_closedir, /* closedir */
|
|
|
|
littlefs_readdir, /* readdir */
|
|
|
|
littlefs_rewinddir, /* rewinddir */
|
|
|
|
|
|
|
|
littlefs_bind, /* bind */
|
|
|
|
littlefs_unbind, /* unbind */
|
|
|
|
littlefs_statfs, /* statfs */
|
|
|
|
|
|
|
|
littlefs_unlink, /* unlink */
|
|
|
|
littlefs_mkdir, /* mkdir */
|
|
|
|
littlefs_rmdir, /* rmdir */
|
|
|
|
littlefs_rename, /* rename */
|
2021-07-09 07:43:26 +02:00
|
|
|
littlefs_stat, /* stat */
|
|
|
|
NULL /* chstat */
|
2019-01-09 21:26:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_convert_result
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int littlefs_convert_result(int ret)
|
|
|
|
{
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case LFS_ERR_IO:
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
case LFS_ERR_CORRUPT:
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
case LFS_ERR_NOENT:
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
case LFS_ERR_EXIST:
|
|
|
|
return -EEXIST;
|
|
|
|
|
|
|
|
case LFS_ERR_NOTDIR:
|
|
|
|
return -ENOTDIR;
|
|
|
|
|
|
|
|
case LFS_ERR_ISDIR:
|
|
|
|
return -EISDIR;
|
|
|
|
|
|
|
|
case LFS_ERR_NOTEMPTY:
|
|
|
|
return -ENOTEMPTY;
|
|
|
|
|
|
|
|
case LFS_ERR_BADF:
|
|
|
|
return -EBADF;
|
|
|
|
|
|
|
|
case LFS_ERR_FBIG:
|
|
|
|
return -EFBIG;
|
|
|
|
|
|
|
|
case LFS_ERR_INVAL:
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
case LFS_ERR_NOSPC:
|
|
|
|
return -ENOSPC;
|
|
|
|
|
|
|
|
case LFS_ERR_NOMEM:
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
case LFS_ERR_NOATTR:
|
|
|
|
return -ENODATA;
|
|
|
|
|
|
|
|
case LFS_ERR_NAMETOOLONG:
|
|
|
|
return -ENAMETOOLONG;
|
|
|
|
|
|
|
|
case LFS_ERR_OK:
|
|
|
|
default:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_convert_oflags
|
|
|
|
****************************************************************************/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_convert_oflags(int oflags)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if ((oflags & O_RDONLY) != 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
ret |= LFS_O_RDONLY;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if ((oflags & O_WRONLY) != 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
ret |= LFS_O_WRONLY;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if ((oflags & O_CREAT) != 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
ret |= LFS_O_CREAT;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if ((oflags & O_EXCL) != 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
ret |= LFS_O_EXCL;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if ((oflags & O_APPEND) != 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
ret |= LFS_O_APPEND;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if ((oflags & O_TRUNC) != 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
ret |= LFS_O_TRUNC;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_open
|
|
|
|
****************************************************************************/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2021-03-22 10:39:44 +01:00
|
|
|
FAR struct littlefs_file_s *priv;
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Get the mountpoint inode reference from the file structure and the
|
|
|
|
* mountpoint private data from the inode structure
|
|
|
|
*/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Allocate memory for the open file */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
priv = fs_heap_malloc(sizeof(*priv));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (priv == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2021-03-22 10:39:44 +01:00
|
|
|
priv->refs = 1;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Lock */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errlock;
|
2020-03-30 12:22:26 +02:00
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Try to open the file */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
oflags = littlefs_convert_oflags(oflags);
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_file_open(&fs->lfs, &priv->file,
|
|
|
|
relpath, oflags));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Error opening file */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* In append mode, we need to set the file pointer to the end of the
|
|
|
|
* file.
|
|
|
|
*/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if (oflags & LFS_O_APPEND)
|
|
|
|
{
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_file_seek(&fs->lfs, &priv->file,
|
|
|
|
0, LFS_SEEK_END));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
filep->f_pos = ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout_with_file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 06:26:58 +01:00
|
|
|
/* Sync here in case of O_TRUNC haven't actually done immediately,
|
|
|
|
* e.g. total 8M, fileA 6M, O_TRUNC re-wrting fileA 6M, meet error.
|
|
|
|
*/
|
|
|
|
|
2021-03-22 10:39:44 +01:00
|
|
|
lfs_file_sync(&fs->lfs, &priv->file);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Attach the private date to the struct file instance */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
filep->f_priv = priv;
|
|
|
|
return OK;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
errout_with_file:
|
2021-03-22 10:39:44 +01:00
|
|
|
lfs_file_close(&fs->lfs, &priv->file);
|
2019-01-27 19:31:28 +01:00
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
|
|
|
errlock:
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(priv);
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int littlefs_close(FAR struct file *filep)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2021-03-22 10:39:44 +01:00
|
|
|
FAR struct littlefs_file_s *priv;
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct inode *inode;
|
2020-03-30 12:22:26 +02:00
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Recover our private data from the struct file instance */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
priv = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
/* Close the file */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-22 10:39:44 +01:00
|
|
|
return ret;
|
2020-03-30 12:22:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 10:39:44 +01:00
|
|
|
if (--priv->refs <= 0)
|
|
|
|
{
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_file_close(&fs->lfs, &priv->file));
|
2021-03-22 10:39:44 +01:00
|
|
|
}
|
2019-01-27 19:31:28 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2021-03-22 10:39:44 +01:00
|
|
|
if (priv->refs <= 0)
|
|
|
|
{
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(priv);
|
2021-03-22 10:39:44 +01:00
|
|
|
}
|
2019-01-27 19:31:28 +01:00
|
|
|
|
2021-06-16 10:05:39 +02:00
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t littlefs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2021-03-22 10:39:44 +01:00
|
|
|
FAR struct littlefs_file_s *priv;
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
priv = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
/* Call LFS to perform the read */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2021-02-24 05:35:20 +01:00
|
|
|
if (ret < 0)
|
2020-03-30 12:22:26 +02:00
|
|
|
{
|
2021-02-24 05:35:20 +01:00
|
|
|
return ret;
|
2020-03-30 12:22:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 10:39:44 +01:00
|
|
|
if (filep->f_pos != priv->file.pos)
|
|
|
|
{
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_file_seek(&fs->lfs, &priv->file,
|
|
|
|
filep->f_pos,
|
|
|
|
LFS_SEEK_SET));
|
2021-03-22 10:39:44 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_file_read(&fs->lfs, &priv->file,
|
|
|
|
buffer, buflen));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret > 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
filep->f_pos += ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2021-03-22 10:39:44 +01:00
|
|
|
out:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t littlefs_write(FAR struct file *filep, const char *buffer,
|
|
|
|
size_t buflen)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2021-03-22 10:39:44 +01:00
|
|
|
FAR struct littlefs_file_s *priv;
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
priv = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Call LFS to perform the write */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2021-02-24 05:35:20 +01:00
|
|
|
if (ret < 0)
|
2020-03-30 12:22:26 +02:00
|
|
|
{
|
2021-02-24 05:35:20 +01:00
|
|
|
return ret;
|
2020-03-30 12:22:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 10:39:44 +01:00
|
|
|
if (filep->f_pos != priv->file.pos)
|
|
|
|
{
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_file_seek(&fs->lfs, &priv->file,
|
|
|
|
filep->f_pos,
|
|
|
|
LFS_SEEK_SET));
|
2021-03-22 10:39:44 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_file_write(&fs->lfs, &priv->file,
|
|
|
|
buffer, buflen));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret > 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
filep->f_pos += ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2021-03-22 10:39:44 +01:00
|
|
|
out:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_seek
|
|
|
|
****************************************************************************/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static off_t littlefs_seek(FAR struct file *filep, off_t offset, int whence)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2021-03-22 10:39:44 +01:00
|
|
|
FAR struct littlefs_file_s *priv;
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
off_t ret;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
priv = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Call LFS to perform the seek */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2021-02-24 05:35:20 +01:00
|
|
|
if (ret < 0)
|
2020-03-30 12:22:26 +02:00
|
|
|
{
|
2021-02-24 05:35:20 +01:00
|
|
|
return ret;
|
2020-03-30 12:22:26 +02:00
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_file_seek(&fs->lfs, &priv->file,
|
|
|
|
offset, whence));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret >= 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
filep->f_pos = ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_ioctl
|
|
|
|
****************************************************************************/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2021-08-25 03:50:15 +02:00
|
|
|
FAR struct littlefs_file_s *priv;
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct inode *drv;
|
2021-08-25 03:50:15 +02:00
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2021-08-25 03:50:15 +02:00
|
|
|
priv = filep->f_priv;
|
2019-01-27 19:31:28 +01:00
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
drv = fs->drv;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2021-08-25 03:50:15 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
|
|
|
if (ret < 0)
|
2019-01-27 19:31:28 +01:00
|
|
|
{
|
2021-08-25 03:50:15 +02:00
|
|
|
return ret;
|
2019-01-27 19:31:28 +01:00
|
|
|
}
|
2021-08-25 03:50:15 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2021-08-25 03:50:15 +02:00
|
|
|
case FIOC_FILEPATH:
|
2022-04-11 07:04:33 +02:00
|
|
|
{
|
2021-08-25 03:50:15 +02:00
|
|
|
FAR char *path = (FAR char *)(uintptr_t)arg;
|
|
|
|
ret = inode_getpath(inode, path, PATH_MAX);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
size_t len = strlen(path);
|
|
|
|
if (path[len - 1] != '/')
|
|
|
|
{
|
|
|
|
path[len++] = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = littlefs_convert_result(lfs_file_path(&fs->lfs,
|
|
|
|
&priv->file,
|
|
|
|
path + len,
|
|
|
|
PATH_MAX - len));
|
|
|
|
}
|
2022-04-11 07:04:33 +02:00
|
|
|
}
|
2021-08-25 03:50:15 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-04-11 07:04:33 +02:00
|
|
|
{
|
2021-08-25 03:50:15 +02:00
|
|
|
if (INODE_IS_MTD(drv))
|
|
|
|
{
|
|
|
|
ret = MTD_IOCTL(drv->u.i_mtd, cmd, arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (drv->u.i_bops->ioctl != NULL)
|
|
|
|
{
|
|
|
|
ret = drv->u.i_bops->ioctl(drv, cmd, arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOTTY;
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 07:04:33 +02:00
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
2021-08-25 03:50:15 +02:00
|
|
|
|
|
|
|
nxmutex_unlock(&fs->lock);
|
|
|
|
return ret;
|
2019-01-27 19:31:28 +01:00
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_sync
|
|
|
|
*
|
|
|
|
* Description: Synchronize the file state on disk to match internal, in-
|
|
|
|
* memory state.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_sync(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2021-03-22 10:39:44 +01:00
|
|
|
FAR struct littlefs_file_s *priv;
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
priv = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_file_sync(&fs->lfs, &priv->file));
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_dup
|
|
|
|
*
|
|
|
|
* Description: Duplicate open file data in the new file structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int littlefs_dup(FAR const struct file *oldp, FAR struct file *newp)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2021-03-22 10:39:44 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
|
|
|
FAR struct littlefs_file_s *priv;
|
|
|
|
FAR struct inode *inode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
priv = oldp->f_priv;
|
|
|
|
inode = oldp->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2021-03-22 10:39:44 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->refs++;
|
|
|
|
newp->f_priv = priv;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2021-03-22 10:39:44 +01:00
|
|
|
|
|
|
|
return ret;
|
2019-01-27 19:31:28 +01:00
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_fstat
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Obtain information about an open file associated with the file
|
|
|
|
* descriptor 'fd', and will write it to the area pointed to by 'buf'.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2021-03-22 10:39:44 +01:00
|
|
|
FAR struct littlefs_file_s *priv;
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct inode *inode;
|
2020-03-30 12:22:26 +02:00
|
|
|
int ret;
|
2019-01-27 19:31:28 +01:00
|
|
|
|
|
|
|
memset(buf, 0, sizeof(*buf));
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
priv = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
/* Call LFS to get file size */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-22 10:39:44 +01:00
|
|
|
buf->st_size = lfs_file_size(&fs->lfs, &priv->file);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-27 19:31:28 +01:00
|
|
|
|
|
|
|
if (buf->st_size < 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2022-03-27 07:24:20 +02:00
|
|
|
return littlefs_convert_result(buf->st_size);
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU | S_IFREG;
|
|
|
|
buf->st_blksize = fs->cfg.block_size;
|
|
|
|
buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / buf->st_blksize;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_truncate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the length of the open, regular file associated with the file
|
|
|
|
* structure 'filep' to 'length'.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int littlefs_truncate(FAR struct file *filep, off_t length)
|
|
|
|
{
|
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2021-03-22 10:39:44 +01:00
|
|
|
FAR struct littlefs_file_s *priv;
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
priv = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
/* Call LFS to perform the truncate */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_file_truncate(&fs->lfs, &priv->file,
|
|
|
|
length));
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-27 19:31:28 +01:00
|
|
|
|
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_opendir
|
|
|
|
*
|
|
|
|
* Description: Open a directory for read access
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-10 00:34:48 +01:00
|
|
|
static int littlefs_opendir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct littlefs_dir_s *ldir;
|
2019-01-27 19:31:28 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
|
|
|
/* Allocate memory for the open directory */
|
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
ldir = fs_heap_malloc(sizeof(*ldir));
|
2022-08-05 06:31:25 +02:00
|
|
|
if (ldir == NULL)
|
2019-01-27 19:31:28 +01:00
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errlock;
|
2020-03-30 12:22:26 +02:00
|
|
|
}
|
2019-01-27 19:31:28 +01:00
|
|
|
|
|
|
|
/* Call the LFS's opendir function */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ret = littlefs_convert_result(lfs_dir_open(&fs->lfs, &ldir->dir, relpath));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2022-08-05 06:31:25 +02:00
|
|
|
*dir = &ldir->base;
|
2019-01-27 19:31:28 +01:00
|
|
|
return OK;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
|
|
|
errlock:
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(ldir);
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_closedir
|
|
|
|
*
|
|
|
|
* Description: Close a directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-09 21:26:51 +01:00
|
|
|
static int littlefs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
|
|
|
FAR struct littlefs_dir_s *ldir;
|
2020-03-30 12:22:26 +02:00
|
|
|
int ret;
|
2019-01-27 19:31:28 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ldir = (FAR struct littlefs_dir_s *)dir;
|
2019-01-27 19:31:28 +01:00
|
|
|
fs = mountpt->i_private;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Call the LFS's closedir function */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-22 10:39:44 +01:00
|
|
|
return ret;
|
2020-03-30 12:22:26 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
lfs_dir_close(&fs->lfs, &ldir->dir);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(ldir);
|
2019-01-27 19:31:28 +01:00
|
|
|
return OK;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_readdir
|
|
|
|
*
|
|
|
|
* Description: Read the next directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-09 21:26:51 +01:00
|
|
|
static int littlefs_readdir(FAR struct inode *mountpt,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct littlefs_dir_s *ldir;
|
2020-04-21 09:22:31 +02:00
|
|
|
struct lfs_info info;
|
2019-01-27 19:31:28 +01:00
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ldir = (FAR struct littlefs_dir_s *)dir;
|
2019-01-27 19:31:28 +01:00
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
|
|
|
/* Call the LFS's readdir function */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ret = littlefs_convert_result(lfs_dir_read(&fs->lfs, &ldir->dir, &info));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret > 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
if (info.type == LFS_TYPE_REG)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_FILE;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
2019-01-27 19:31:28 +01:00
|
|
|
else
|
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_DIRECTORY;
|
2019-01-27 19:31:28 +01:00
|
|
|
}
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
strlcpy(entry->d_name, info.name, sizeof(entry->d_name));
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
2019-01-27 19:31:28 +01:00
|
|
|
else if (ret == 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
ret = -ENOENT;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-09 21:26:51 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_rewindir
|
|
|
|
*
|
|
|
|
* Description: Reset directory read to the first entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-09 21:26:51 +01:00
|
|
|
static int littlefs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
|
|
|
FAR struct littlefs_dir_s *ldir;
|
2019-01-27 19:31:28 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ldir = (FAR struct littlefs_dir_s *)dir;
|
2019-01-27 19:31:28 +01:00
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
|
|
|
/* Call the LFS's rewinddir function */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ret = littlefs_convert_result(lfs_dir_rewind(&fs->lfs, &ldir->dir));
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_bind
|
|
|
|
*
|
|
|
|
* Description: This implements a portion of the mount operation. This
|
|
|
|
* function allocates and initializes the mountpoint private data and
|
|
|
|
* binds the driver inode to the filesystem private data. The final
|
|
|
|
* binding of the private data (containing the driver) to the
|
|
|
|
* mountpoint is performed by mount().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-21 09:22:31 +02:00
|
|
|
static int littlefs_read_block(FAR const struct lfs_config *c,
|
2019-01-29 14:39:25 +01:00
|
|
|
lfs_block_t block, lfs_off_t off,
|
|
|
|
FAR void *buffer, lfs_size_t size)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs = c->context;
|
|
|
|
FAR struct mtd_geometry_s *geo = &fs->geo;
|
|
|
|
FAR struct inode *drv = fs->drv;
|
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
block = (block * c->block_size + off) / geo->blocksize;
|
|
|
|
size = size / geo->blocksize;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if (INODE_IS_MTD(drv))
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
ret = MTD_BREAD(drv->u.i_mtd, block, size, buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = drv->u.i_bops->read(drv, buffer, block, size);
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret >= 0 ? OK : ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_write_block
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-21 09:22:31 +02:00
|
|
|
static int littlefs_write_block(FAR const struct lfs_config *c,
|
2019-01-29 14:39:25 +01:00
|
|
|
lfs_block_t block, lfs_off_t off,
|
|
|
|
FAR const void *buffer, lfs_size_t size)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs = c->context;
|
|
|
|
FAR struct mtd_geometry_s *geo = &fs->geo;
|
|
|
|
FAR struct inode *drv = fs->drv;
|
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
block = (block * c->block_size + off) / geo->blocksize;
|
|
|
|
size = size / geo->blocksize;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if (INODE_IS_MTD(drv))
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
ret = MTD_BWRITE(drv->u.i_mtd, block, size, buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = drv->u.i_bops->write(drv, buffer, block, size);
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret >= 0 ? OK : ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_erase_block
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-21 09:22:31 +02:00
|
|
|
static int littlefs_erase_block(FAR const struct lfs_config *c,
|
2019-01-29 14:39:25 +01:00
|
|
|
lfs_block_t block)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs = c->context;
|
|
|
|
FAR struct inode *drv = fs->drv;
|
2019-01-09 21:26:51 +01:00
|
|
|
int ret = OK;
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if (INODE_IS_MTD(drv))
|
|
|
|
{
|
|
|
|
FAR struct mtd_geometry_s *geo = &fs->geo;
|
|
|
|
size_t size = c->block_size / geo->erasesize;
|
|
|
|
|
|
|
|
block = block * c->block_size / geo->erasesize;
|
|
|
|
ret = MTD_ERASE(drv->u.i_mtd, block, size);
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret >= 0 ? OK : ret;
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_sync_block
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-21 09:22:31 +02:00
|
|
|
static int littlefs_sync_block(FAR const struct lfs_config *c)
|
2019-01-27 19:31:28 +01:00
|
|
|
{
|
|
|
|
FAR struct littlefs_mountpt_s *fs = c->context;
|
|
|
|
FAR struct inode *drv = fs->drv;
|
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if (INODE_IS_MTD(drv))
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2021-08-11 08:07:58 +02:00
|
|
|
ret = MTD_IOCTL(drv->u.i_mtd, BIOC_FLUSH, 0);
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
2019-01-27 19:31:28 +01:00
|
|
|
else
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2022-04-11 07:04:33 +02:00
|
|
|
if (drv->u.i_bops->ioctl != NULL)
|
|
|
|
{
|
|
|
|
ret = drv->u.i_bops->ioctl(drv, BIOC_FLUSH, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOTTY;
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret == -ENOTTY ? OK : ret;
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_bind
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int littlefs_bind(FAR struct inode *driver, FAR const void *data,
|
|
|
|
FAR void **handle)
|
|
|
|
{
|
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Open the block driver */
|
|
|
|
|
|
|
|
if (INODE_IS_BLOCK(driver) && driver->u.i_bops->open)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
ret = driver->u.i_bops->open(driver);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Create an instance of the mountpt state structure */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
fs = fs_heap_zalloc(sizeof(*fs));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (!fs)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_block;
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Initialize the allocated mountpt state structure. The filesystem is
|
|
|
|
* responsible for one reference on the driver inode and does not
|
|
|
|
* have to addref() here (but does have to release in unbind().
|
|
|
|
*/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
fs->drv = driver; /* Save the driver reference */
|
|
|
|
nxmutex_init(&fs->lock); /* Initialize the access control mutex */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if (INODE_IS_MTD(driver))
|
|
|
|
{
|
|
|
|
/* Get MTD geometry directly */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
ret = MTD_IOCTL(driver->u.i_mtd, MTDIOC_GEOMETRY,
|
|
|
|
(unsigned long)&fs->geo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Try to get FLT MTD geometry first */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-04-11 07:04:33 +02:00
|
|
|
if (driver->u.i_bops->ioctl != NULL)
|
|
|
|
{
|
|
|
|
ret = driver->u.i_bops->ioctl(driver, MTDIOC_GEOMETRY,
|
|
|
|
(unsigned long)&fs->geo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOTTY;
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
struct geometry geometry;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Not FLT MTD device, get normal block geometry */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
ret = driver->u.i_bops->geometry(driver, &geometry);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* And convert to MTD geometry */
|
|
|
|
|
|
|
|
fs->geo.blocksize = geometry.geo_sectorsize;
|
|
|
|
fs->geo.erasesize = geometry.geo_sectorsize;
|
|
|
|
fs->geo.neraseblocks = geometry.geo_nsectors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
goto errout_with_fs;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Initialize lfs_config structure */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2020-04-21 09:22:31 +02:00
|
|
|
fs->cfg.context = fs;
|
|
|
|
fs->cfg.read = littlefs_read_block;
|
|
|
|
fs->cfg.prog = littlefs_write_block;
|
|
|
|
fs->cfg.erase = littlefs_erase_block;
|
|
|
|
fs->cfg.sync = littlefs_sync_block;
|
2021-08-12 09:17:01 +02:00
|
|
|
fs->cfg.read_size = fs->geo.blocksize *
|
2023-02-13 09:55:46 +01:00
|
|
|
CONFIG_FS_LITTLEFS_READ_SIZE_FACTOR;
|
|
|
|
fs->cfg.prog_size = fs->geo.blocksize *
|
|
|
|
CONFIG_FS_LITTLEFS_PROGRAM_SIZE_FACTOR;
|
|
|
|
fs->cfg.block_size = fs->geo.erasesize *
|
|
|
|
CONFIG_FS_LITTLEFS_BLOCK_SIZE_FACTOR;
|
|
|
|
fs->cfg.block_count = fs->geo.neraseblocks /
|
|
|
|
CONFIG_FS_LITTLEFS_BLOCK_SIZE_FACTOR;
|
2021-08-12 09:17:01 +02:00
|
|
|
fs->cfg.block_cycles = CONFIG_FS_LITTLEFS_BLOCK_CYCLE;
|
|
|
|
fs->cfg.cache_size = fs->geo.blocksize *
|
2023-02-13 09:55:46 +01:00
|
|
|
CONFIG_FS_LITTLEFS_CACHE_SIZE_FACTOR;
|
|
|
|
|
|
|
|
#if CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE == 0
|
2021-03-29 07:58:03 +02:00
|
|
|
fs->cfg.lookahead_size = lfs_min(lfs_alignup(fs->cfg.block_count, 64) / 8,
|
|
|
|
fs->cfg.read_size);
|
2023-02-13 09:55:46 +01:00
|
|
|
#else
|
|
|
|
fs->cfg.lookahead_size = CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE;
|
|
|
|
#endif
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Then get information about the littlefs filesystem on the devices
|
|
|
|
* managed by this driver.
|
|
|
|
*/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Force format the device if -o forceformat */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if (data && strcmp(data, "forceformat") == 0)
|
|
|
|
{
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_format(&fs->lfs, &fs->cfg));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_fs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_mount(&fs->lfs, &fs->cfg));
|
2019-01-09 21:26:51 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Auto format the device if -o autoformat */
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
if (ret != -EFAULT || !data || strcmp(data, "autoformat"))
|
2019-01-27 19:31:28 +01:00
|
|
|
{
|
|
|
|
goto errout_with_fs;
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_format(&fs->lfs, &fs->cfg));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to mount the device again */
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_mount(&fs->lfs, &fs->cfg));
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_fs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*handle = fs;
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_fs:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&fs->lock);
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(fs);
|
2019-01-27 19:31:28 +01:00
|
|
|
errout_with_block:
|
|
|
|
if (INODE_IS_BLOCK(driver) && driver->u.i_bops->close)
|
|
|
|
{
|
|
|
|
driver->u.i_bops->close(driver);
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_unbind
|
|
|
|
*
|
|
|
|
* Description: This implements the filesystem portion of the umount
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_unbind(FAR void *handle, FAR struct inode **driver,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
FAR struct littlefs_mountpt_s *fs = handle;
|
|
|
|
FAR struct inode *drv = fs->drv;
|
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Unmount */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_unmount(&fs->lfs));
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-27 19:31:28 +01:00
|
|
|
|
|
|
|
if (ret >= 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Close the block driver */
|
|
|
|
|
|
|
|
if (INODE_IS_BLOCK(drv) && drv->u.i_bops->close)
|
|
|
|
{
|
|
|
|
drv->u.i_bops->close(drv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We hold a reference to the driver but should not but
|
|
|
|
* mucking with inodes in this context. So, we will just return
|
|
|
|
* our contained reference to the driver inode and let the
|
|
|
|
* umount logic dispose of it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (driver)
|
|
|
|
{
|
|
|
|
*driver = drv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the mountpoint private data */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&fs->lock);
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(fs);
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_statfs
|
|
|
|
*
|
|
|
|
* Description: Return filesystem statistics
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int littlefs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Get the mountpoint private data from the inode structure */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
|
|
|
/* Return something for the file system description */
|
|
|
|
|
2019-01-29 14:39:25 +01:00
|
|
|
buf->f_type = LITTLEFS_SUPER_MAGIC;
|
2019-01-27 19:31:28 +01:00
|
|
|
buf->f_namelen = LFS_NAME_MAX;
|
|
|
|
buf->f_bsize = fs->cfg.block_size;
|
|
|
|
buf->f_blocks = fs->cfg.block_count;
|
|
|
|
buf->f_bfree = fs->cfg.block_count;
|
|
|
|
buf->f_bavail = fs->cfg.block_count;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_fs_size(&fs->lfs));
|
2020-04-21 09:22:31 +02:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
buf->f_bfree -= ret;
|
|
|
|
buf->f_bavail -= ret;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-09 21:26:51 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_unlink
|
|
|
|
*
|
|
|
|
* Description: Remove a file
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
static int littlefs_unlink(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Get the mountpoint private data from the inode structure */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
fs = mountpt->i_private;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Call the LFS to perform the unlink */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_remove(&fs->lfs, relpath));
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_mkdir
|
|
|
|
*
|
|
|
|
* Description: Create a directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int littlefs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
mode_t mode)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
|
|
|
/* Call LFS to do the mkdir */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
ret = lfs_mkdir(&fs->lfs, relpath);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_rmdir
|
|
|
|
*
|
|
|
|
* Description: Remove a directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int littlefs_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
|
|
|
|
{
|
2021-09-23 08:08:49 +02:00
|
|
|
struct stat buf;
|
2022-12-02 17:30:25 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = littlefs_stat(mountpt, relpath, &buf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2021-09-23 08:08:49 +02:00
|
|
|
|
|
|
|
if (S_ISDIR(buf.st_mode))
|
|
|
|
{
|
|
|
|
return littlefs_unlink(mountpt, relpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -ENOTDIR;
|
|
|
|
}
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_rename
|
|
|
|
*
|
|
|
|
* Description: Rename a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-09 21:26:51 +01:00
|
|
|
static int littlefs_rename(FAR struct inode *mountpt,
|
|
|
|
FAR const char *oldrelpath,
|
|
|
|
FAR const char *newrelpath)
|
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
|
|
|
/* Call LFS to do the rename */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:28:51 +01:00
|
|
|
ret = littlefs_convert_result(lfs_rename(&fs->lfs, oldrelpath,
|
|
|
|
newrelpath));
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: littlefs_stat
|
|
|
|
*
|
|
|
|
* Description: Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-09 21:26:51 +01:00
|
|
|
static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct stat *buf)
|
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
FAR struct littlefs_mountpt_s *fs;
|
2020-04-21 09:22:31 +02:00
|
|
|
struct lfs_info info;
|
2019-01-27 19:31:28 +01:00
|
|
|
int ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
memset(buf, 0, sizeof(*buf));
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Get the mountpoint private data from the inode structure */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
fs = mountpt->i_private;
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Call the LFS to do the stat operation */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&fs->lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
ret = lfs_stat(&fs->lfs, relpath, &info);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fs->lock);
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
if (ret >= 0)
|
2019-01-09 21:26:51 +01:00
|
|
|
{
|
2019-01-27 19:31:28 +01:00
|
|
|
/* Convert info to stat */
|
2019-01-09 21:26:51 +01:00
|
|
|
|
2019-01-27 19:31:28 +01:00
|
|
|
buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU;
|
|
|
|
if (info.type == LFS_TYPE_REG)
|
|
|
|
{
|
|
|
|
buf->st_mode |= S_IFREG;
|
2022-05-20 12:07:54 +02:00
|
|
|
buf->st_size = info.size;
|
2019-01-27 19:31:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf->st_mode |= S_IFDIR;
|
2022-05-20 12:07:54 +02:00
|
|
|
buf->st_size = 0;
|
2019-01-27 19:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
buf->st_blksize = fs->cfg.block_size;
|
|
|
|
buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
|
|
|
|
buf->st_blksize;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|
2019-01-27 19:31:28 +01:00
|
|
|
|
|
|
|
return ret;
|
2019-01-09 21:26:51 +01:00
|
|
|
}
|