2013-05-01 04:13:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/smartfs/smartfs_smart.c
|
|
|
|
*
|
2021-04-10 08:50:57 +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
|
2013-05-01 04:13:30 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-05-01 04:13:30 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +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.
|
2013-05-01 04:13:30 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2013-05-01 04:13:30 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/fat.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2013-12-10 16:38:48 +01:00
|
|
|
#include <nuttx/fs/smart.h>
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
#include "smartfs.h"
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Type
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct smartfs_dir_s
|
|
|
|
{
|
|
|
|
struct fs_dirent_s fs_base; /* VFS directory structure */
|
|
|
|
uint16_t fs_firstsector; /* First sector of directory list */
|
|
|
|
uint16_t fs_currsector; /* Current sector of directory list */
|
|
|
|
uint16_t fs_curroffset; /* Current offset within current sector */
|
|
|
|
};
|
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static int smartfs_open(FAR struct file *filep, FAR const char *relpath,
|
2013-05-01 04:13:30 +02:00
|
|
|
int oflags, mode_t mode);
|
|
|
|
static int smartfs_close(FAR struct file *filep);
|
2022-09-06 08:18:45 +02:00
|
|
|
static ssize_t smartfs_read(FAR struct file *filep, FAR char *buffer,
|
2017-02-12 20:42:27 +01:00
|
|
|
size_t buflen);
|
2022-09-06 08:18:45 +02:00
|
|
|
static ssize_t smartfs_write(FAR struct file *filep, FAR const char *buffer,
|
2013-05-01 04:13:30 +02:00
|
|
|
size_t buflen);
|
2017-02-12 20:42:27 +01:00
|
|
|
static off_t smartfs_seek(FAR struct file *filep, off_t offset,
|
|
|
|
int whence);
|
|
|
|
static int smartfs_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
static int smartfs_sync(FAR struct file *filep);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int smartfs_dup(FAR const struct file *oldp,
|
|
|
|
FAR struct file *newp);
|
|
|
|
static int smartfs_fstat(FAR const struct file *filep,
|
|
|
|
FAR struct stat *buf);
|
2018-01-04 17:54:54 +01:00
|
|
|
static int smartfs_truncate(FAR struct file *filep, off_t length);
|
2017-02-12 20:42:27 +01:00
|
|
|
|
|
|
|
static int smartfs_opendir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir);
|
|
|
|
static int smartfs_closedir(FAR struct inode *mountpt,
|
2017-02-12 20:42:27 +01:00
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int smartfs_readdir(FAR struct inode *mountpt,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *dentry);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int smartfs_rewinddir(FAR struct inode *mountpt,
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct fs_dirent_s *dir);
|
2017-02-12 20:42:27 +01:00
|
|
|
|
|
|
|
static int smartfs_bind(FAR struct inode *blkdriver,
|
|
|
|
FAR const void *data,
|
|
|
|
FAR void **handle);
|
2022-09-06 08:18:45 +02:00
|
|
|
static int smartfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
2015-03-15 00:22:02 +01:00
|
|
|
unsigned int flags);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int smartfs_statfs(FAR struct inode *mountpt,
|
|
|
|
FAR struct statfs *buf);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2017-02-12 20:42:27 +01:00
|
|
|
static int smartfs_unlink(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath);
|
|
|
|
static int smartfs_mkdir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath,
|
2013-05-01 04:13:30 +02:00
|
|
|
mode_t mode);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int smartfs_rmdir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath);
|
|
|
|
static int smartfs_rename(FAR struct inode *mountpt,
|
|
|
|
FAR const char *oldrelpath,
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR const char *newrelpath);
|
2017-02-14 17:30:11 +01:00
|
|
|
static void smartfs_stat_common(FAR struct smartfs_mountpt_s *fs,
|
|
|
|
FAR struct smartfs_entry_s *entry,
|
|
|
|
FAR struct stat *buf);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int smartfs_stat(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR struct stat *buf);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2016-02-22 01:08:58 +01:00
|
|
|
* Private Data
|
2013-05-01 04:13:30 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static mutex_t g_lock = NXMUTEX_INITIALIZER;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2013-05-01 04:13:30 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* See fs_mount.c -- this structure is explicitly externed 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_smartfs_operations =
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
|
|
|
smartfs_open, /* open */
|
|
|
|
smartfs_close, /* close */
|
|
|
|
smartfs_read, /* read */
|
|
|
|
smartfs_write, /* write */
|
|
|
|
smartfs_seek, /* seek */
|
|
|
|
smartfs_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
smartfs_truncate, /* truncate */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
smartfs_sync, /* sync */
|
|
|
|
smartfs_dup, /* dup */
|
2017-02-12 20:42:27 +01:00
|
|
|
smartfs_fstat, /* fstat */
|
2021-07-09 07:43:26 +02:00
|
|
|
NULL, /* fchstat */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
smartfs_opendir, /* opendir */
|
2022-08-05 06:31:25 +02:00
|
|
|
smartfs_closedir, /* closedir */
|
2013-05-01 04:13:30 +02:00
|
|
|
smartfs_readdir, /* readdir */
|
|
|
|
smartfs_rewinddir, /* rewinddir */
|
|
|
|
|
|
|
|
smartfs_bind, /* bind */
|
|
|
|
smartfs_unbind, /* unbind */
|
|
|
|
smartfs_statfs, /* statfs */
|
|
|
|
|
2020-11-08 16:57:30 +01:00
|
|
|
smartfs_unlink, /* unlink */
|
2013-05-01 04:13:30 +02:00
|
|
|
smartfs_mkdir, /* mkdir */
|
|
|
|
smartfs_rmdir, /* rmdir */
|
|
|
|
smartfs_rename, /* rename */
|
2021-07-09 07:43:26 +02:00
|
|
|
smartfs_stat, /* stat */
|
|
|
|
NULL /* chstat */
|
2013-05-01 04:13:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static int smartfs_open(FAR struct file *filep, FAR const char *relpath,
|
2013-05-01 04:13:30 +02:00
|
|
|
int oflags, mode_t mode)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
int ret;
|
|
|
|
uint16_t parentdirsector;
|
|
|
|
FAR const char *filename;
|
|
|
|
FAR struct smartfs_ofile_s *sf;
|
2018-06-21 00:36:38 +02:00
|
|
|
#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER
|
2022-09-06 08:18:45 +02:00
|
|
|
struct smart_read_write_s readwrite;
|
2018-06-21 00:36:38 +02:00
|
|
|
#endif
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT((filep->f_priv == NULL) && (filep->f_inode != NULL));
|
|
|
|
|
|
|
|
/* Get the mountpoint inode reference from the file structure and the
|
|
|
|
* mountpoint private data from the inode structure
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(fs != NULL);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Locate the directory entry for this path */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
sf = kmm_malloc(sizeof *sf);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (sf == NULL)
|
|
|
|
{
|
2014-02-11 01:08:49 +01:00
|
|
|
ret = -ENOMEM;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 21:11:15 +01:00
|
|
|
/* Allocate a sector buffer if CRC enabled in the MTD layer */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER
|
2023-08-28 09:39:47 +02:00
|
|
|
sf->buffer = kmm_malloc(fs->fs_llformat.availbytes);
|
2014-12-09 21:11:15 +01:00
|
|
|
if (sf->buffer == NULL)
|
|
|
|
{
|
|
|
|
/* Error ... no memory */
|
|
|
|
|
|
|
|
kmm_free(sf);
|
|
|
|
ret = -ENOMEM;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2014-12-09 21:11:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sf->bflags = 0;
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_SMARTFS_USE_SECTOR_BUFFER */
|
2014-12-09 21:11:15 +01:00
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
sf->entry.name = NULL;
|
|
|
|
ret = smartfs_finddirentry(fs, &sf->entry, relpath, &parentdirsector,
|
|
|
|
&filename);
|
|
|
|
|
2014-12-09 21:11:15 +01:00
|
|
|
/* Three possibilities: (1) a node exists for the relpath and
|
2013-05-01 04:13:30 +02:00
|
|
|
* dirinfo describes the directory entry of the entity, (2) the
|
|
|
|
* node does not exist, or (3) some error occurred.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2019-03-01 22:00:00 +01:00
|
|
|
/* The name exists -- but is is a file or a directory? */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
if (sf->entry.flags & SMARTFS_DIRENT_TYPE_DIR)
|
|
|
|
{
|
|
|
|
/* Can't open a dir as a file! */
|
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
ret = -EISDIR;
|
2013-05-01 04:13:30 +02:00
|
|
|
goto errout_with_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It would be an error if we are asked to create it exclusively */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
|
|
|
/* Already exists -- can't create it exclusively */
|
|
|
|
|
|
|
|
ret = -EEXIST;
|
|
|
|
goto errout_with_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Test open mode based on the file mode */
|
|
|
|
|
2019-11-05 14:28:24 +01:00
|
|
|
/* If O_TRUNC is specified and the file is opened for writing,
|
|
|
|
* then truncate the file. This operation requires that the file
|
|
|
|
* is writeable. O_TRUNC without write access is ignored.
|
2019-03-01 22:00:00 +01:00
|
|
|
*/
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2019-11-05 14:28:24 +01:00
|
|
|
if ((oflags & (O_TRUNC | O_WROK)) == (O_TRUNC | O_WROK))
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2019-11-05 14:28:24 +01:00
|
|
|
/* Truncate the file as part of the open */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2019-11-05 14:28:24 +01:00
|
|
|
ret = smartfs_shrinkfile(fs, sf, 0);
|
|
|
|
if (ret < 0)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2019-11-05 14:28:24 +01:00
|
|
|
goto errout_with_buffer;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ret == -ENOENT)
|
|
|
|
{
|
|
|
|
/* The file does not exist. Were we asked to create it? */
|
|
|
|
|
|
|
|
if ((oflags & O_CREAT) == 0)
|
|
|
|
{
|
|
|
|
/* No.. then we fail with -ENOENT */
|
|
|
|
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto errout_with_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes... test if the parent directory is valid */
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
if (parentdirsector != 0xffff)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
|
|
|
/* We can create in the given parent directory */
|
|
|
|
|
2014-04-14 00:22:22 +02:00
|
|
|
ret = smartfs_createentry(fs, parentdirsector, filename,
|
2013-05-01 04:13:30 +02:00
|
|
|
SMARTFS_DIRENT_TYPE_FILE, mode,
|
2019-10-27 18:48:14 +01:00
|
|
|
&sf->entry, 0xffff, sf);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
goto errout_with_buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Trying to create in a directory that doesn't exist */
|
|
|
|
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto errout_with_buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout_with_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now perform the "open" on the file in direntry */
|
|
|
|
|
2018-06-21 00:36:38 +02:00
|
|
|
sf->oflags = oflags;
|
|
|
|
sf->crefs = 1;
|
|
|
|
sf->filepos = 0;
|
|
|
|
sf->curroffset = sizeof(struct smartfs_chain_header_s);
|
|
|
|
sf->currsector = sf->entry.firstsector;
|
2013-05-01 04:13:30 +02:00
|
|
|
sf->byteswritten = 0;
|
|
|
|
|
2018-06-21 00:36:38 +02:00
|
|
|
#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER
|
|
|
|
|
2020-03-30 12:22:26 +02:00
|
|
|
/* When using sector buffering, current sector with its header should
|
|
|
|
* always be present in sf->buffer. Otherwise data corruption may arise
|
2022-01-18 04:56:36 +01:00
|
|
|
* when writing.
|
2018-06-21 00:36:38 +02:00
|
|
|
*/
|
|
|
|
|
2022-01-18 04:56:36 +01:00
|
|
|
if (sf->currsector != SMARTFS_ERASEDSTATE_16BIT)
|
2018-06-21 00:36:38 +02:00
|
|
|
{
|
|
|
|
readwrite.logsector = sf->currsector;
|
|
|
|
readwrite.offset = 0;
|
|
|
|
readwrite.count = fs->fs_llformat.availbytes;
|
2022-09-06 08:18:45 +02:00
|
|
|
readwrite.buffer = (FAR uint8_t *)sf->buffer;
|
2018-06-21 00:36:38 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = FS_IOCTL(fs, BIOC_READSECT, (unsigned long)&readwrite);
|
2018-06-21 00:36:38 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Error %d reading sector %d header\n",
|
|
|
|
ret, sf->currsector);
|
|
|
|
goto errout_with_buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
/* Test if we opened for APPEND mode. If we did, then seek to the
|
|
|
|
* end of the file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (oflags & O_APPEND)
|
|
|
|
{
|
|
|
|
/* Perform the seek */
|
|
|
|
|
|
|
|
smartfs_seek_internal(fs, sf, 0, SEEK_END);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach the private date to the struct file instance */
|
|
|
|
|
|
|
|
filep->f_priv = sf;
|
|
|
|
|
|
|
|
/* Then insert the new instance into the mountpoint structure.
|
|
|
|
* It needs to be there (1) to handle error conditions that effect
|
|
|
|
* all files, and (2) to inform the umount logic that we are busy
|
|
|
|
* (but a simple reference count could have done that).
|
|
|
|
*/
|
|
|
|
|
|
|
|
sf->fnext = fs->fs_head;
|
|
|
|
fs->fs_head = sf;
|
|
|
|
|
|
|
|
ret = OK;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
errout_with_buffer:
|
|
|
|
if (sf->entry.name != NULL)
|
|
|
|
{
|
|
|
|
/* Free the space for the name too */
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(sf->entry.name);
|
2013-05-01 04:13:30 +02:00
|
|
|
sf->entry.name = NULL;
|
|
|
|
}
|
|
|
|
|
2020-01-08 11:51:11 +01:00
|
|
|
#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER
|
|
|
|
kmm_free(sf->buffer);
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_SMARTFS_USE_SECTOR_BUFFER */
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(sf);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret == -EINVAL)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int smartfs_close(FAR struct file *filep)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
FAR struct smartfs_ofile_s *sf;
|
|
|
|
FAR struct smartfs_ofile_s *nextfile;
|
|
|
|
FAR struct smartfs_ofile_s *prevfile;
|
2020-03-30 12:22:26 +02:00
|
|
|
int ret;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
sf = filep->f_priv;
|
|
|
|
|
|
|
|
/* Sync the file */
|
|
|
|
|
|
|
|
smartfs_sync(filep);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Check if we are the last one with a reference to the file and
|
2019-03-01 22:00:00 +01:00
|
|
|
* only close if we are.
|
|
|
|
*/
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
if (sf->crefs > 1)
|
|
|
|
{
|
|
|
|
/* The file is opened more than once. Just decrement the
|
2019-03-01 22:00:00 +01:00
|
|
|
* reference count and return.
|
|
|
|
*/
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
sf->crefs--;
|
|
|
|
goto okout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove ourselves from the linked list */
|
|
|
|
|
|
|
|
nextfile = fs->fs_head;
|
2013-05-03 20:52:33 +02:00
|
|
|
prevfile = nextfile;
|
2013-05-01 04:13:30 +02:00
|
|
|
while ((nextfile != sf) && (nextfile != NULL))
|
|
|
|
{
|
|
|
|
/* Save the previous file pointer too */
|
|
|
|
|
|
|
|
prevfile = nextfile;
|
|
|
|
nextfile = nextfile->fnext;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextfile != NULL)
|
|
|
|
{
|
|
|
|
/* Test if we were the first entry */
|
|
|
|
|
|
|
|
if (nextfile == fs->fs_head)
|
|
|
|
{
|
|
|
|
/* Assign a new head */
|
|
|
|
|
|
|
|
fs->fs_head = nextfile->fnext;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Take ourselves out of the list */
|
|
|
|
|
|
|
|
prevfile->fnext = nextfile->fnext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now free the pointer */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
filep->f_priv = NULL;
|
2013-05-01 04:13:30 +02:00
|
|
|
if (sf->entry.name != NULL)
|
|
|
|
{
|
|
|
|
/* Free the space for the name too */
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(sf->entry.name);
|
2013-05-01 04:13:30 +02:00
|
|
|
sf->entry.name = NULL;
|
|
|
|
}
|
2014-12-09 21:11:15 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER
|
|
|
|
if (sf->buffer)
|
|
|
|
{
|
|
|
|
kmm_free(sf->buffer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(sf);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
okout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static ssize_t smartfs_read(FAR struct file *filep, FAR char *buffer,
|
2020-03-30 12:22:26 +02:00
|
|
|
size_t buflen)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
FAR struct smartfs_ofile_s *sf;
|
|
|
|
struct smart_read_write_s readwrite;
|
|
|
|
FAR struct smartfs_chain_header_s *header;
|
|
|
|
int ret = OK;
|
|
|
|
uint32_t bytesread;
|
|
|
|
uint16_t bytestoread;
|
|
|
|
uint16_t bytesinsector;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
sf = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(fs != NULL);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Loop until all byte read or error */
|
|
|
|
|
|
|
|
bytesread = 0;
|
|
|
|
while (bytesread != buflen)
|
|
|
|
{
|
|
|
|
/* Test if we are at the end of data */
|
|
|
|
|
|
|
|
if (sf->currsector == SMARTFS_ERASEDSTATE_16BIT)
|
|
|
|
{
|
|
|
|
/* Break and return the number of bytes we read (may be zero) */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Read the current sector into our buffer */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
readwrite.logsector = sf->currsector;
|
|
|
|
readwrite.offset = 0;
|
2022-09-06 08:18:45 +02:00
|
|
|
readwrite.buffer = (FAR uint8_t *)fs->fs_rwbuffer;
|
2013-05-01 04:13:30 +02:00
|
|
|
readwrite.count = fs->fs_llformat.availbytes;
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = FS_IOCTL(fs, BIOC_READSECT, (unsigned long)&readwrite);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Error %d reading sector %d data\n",
|
|
|
|
ret, sf->currsector);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Point header to the read data to get used byte count */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
header = (FAR struct smartfs_chain_header_s *)fs->fs_rwbuffer;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Get number of used bytes in this sector */
|
|
|
|
|
2022-09-05 01:02:24 +02:00
|
|
|
bytesinsector = SMARTFS_USED(header);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (bytesinsector == SMARTFS_ERASEDSTATE_16BIT)
|
|
|
|
{
|
|
|
|
/* No bytes to read from this sector */
|
|
|
|
|
|
|
|
bytesinsector = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the number of bytes to read into the buffer */
|
|
|
|
|
2014-04-14 00:22:22 +02:00
|
|
|
bytestoread = bytesinsector - (sf->curroffset -
|
2013-05-01 04:13:30 +02:00
|
|
|
sizeof(struct smartfs_chain_header_s));
|
|
|
|
if (bytestoread + bytesread > buflen)
|
|
|
|
{
|
2020-11-08 16:57:30 +01:00
|
|
|
/* Truncate bytes to read based on buffer len */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
bytestoread = buflen - bytesread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy data to the read buffer */
|
|
|
|
|
|
|
|
if (bytestoread > 0)
|
|
|
|
{
|
|
|
|
/* Do incremental copy from this sector */
|
|
|
|
|
2020-03-30 12:22:26 +02:00
|
|
|
memcpy(&buffer[bytesread], &fs->fs_rwbuffer[sf->curroffset],
|
|
|
|
bytestoread);
|
2013-05-01 04:13:30 +02:00
|
|
|
bytesread += bytestoread;
|
|
|
|
sf->filepos += bytestoread;
|
|
|
|
sf->curroffset += bytestoread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if we are at the end of the data in this sector */
|
|
|
|
|
2020-03-30 12:22:26 +02:00
|
|
|
if ((bytestoread == 0) ||
|
|
|
|
(sf->curroffset == fs->fs_llformat.availbytes))
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
|
|
|
/* Set the next sector as the current sector */
|
|
|
|
|
|
|
|
sf->currsector = SMARTFS_NEXTSECTOR(header);
|
|
|
|
sf->curroffset = sizeof(struct smartfs_chain_header_s);
|
|
|
|
|
|
|
|
/* Test if at end of data */
|
|
|
|
|
|
|
|
if (sf->currsector == SMARTFS_ERASEDSTATE_16BIT)
|
|
|
|
{
|
|
|
|
/* No more data! Return what we have */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of bytes we read */
|
|
|
|
|
|
|
|
ret = bytesread;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static ssize_t smartfs_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
FAR struct smartfs_ofile_s *sf;
|
|
|
|
struct smart_read_write_s readwrite;
|
|
|
|
FAR struct smartfs_chain_header_s *header;
|
|
|
|
size_t byteswritten;
|
|
|
|
int ret;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
sf = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(fs != NULL);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Test the permissions. Only allow write if the file was opened with
|
|
|
|
* write flags.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((sf->oflags & O_WROK) == 0)
|
|
|
|
{
|
|
|
|
ret = -EACCES;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 14:32:23 +01:00
|
|
|
/* Test if we opened for APPEND mode. If we did, then seek to the
|
|
|
|
* end of the file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sf->oflags & O_APPEND)
|
|
|
|
{
|
|
|
|
ret = smartfs_seek_internal(fs, sf, 0, SEEK_END);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2019-11-05 14:32:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:22:22 +02:00
|
|
|
/* First test if we are overwriting an existing location or writing to
|
2019-03-01 22:00:00 +01:00
|
|
|
* a new one.
|
|
|
|
*/
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
header = (FAR struct smartfs_chain_header_s *)fs->fs_rwbuffer;
|
2013-05-01 04:13:30 +02:00
|
|
|
byteswritten = 0;
|
|
|
|
while ((sf->filepos < sf->entry.datlen) && (buflen > 0))
|
|
|
|
{
|
|
|
|
/* Overwriting data caused by a seek, etc. In this case, we need
|
|
|
|
* to check if the write causes the file length to be extended
|
|
|
|
* or not and update it accordingly. We will write data up to
|
|
|
|
* the current end-of-file and then break, allowing the next while
|
|
|
|
* loop below to write the additional data to the end of the file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
readwrite.offset = sf->curroffset;
|
|
|
|
readwrite.logsector = sf->currsector;
|
2022-09-06 08:18:45 +02:00
|
|
|
readwrite.buffer = (FAR uint8_t *)&buffer[byteswritten];
|
2013-05-01 04:13:30 +02:00
|
|
|
readwrite.count = fs->fs_llformat.availbytes - sf->curroffset;
|
|
|
|
|
|
|
|
/* Limit the write based on available data to write */
|
|
|
|
|
|
|
|
if (readwrite.count > buflen)
|
2018-01-04 17:54:54 +01:00
|
|
|
{
|
|
|
|
readwrite.count = buflen;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Limit the write based on current file length */
|
|
|
|
|
|
|
|
if (readwrite.count > sf->entry.datlen - sf->filepos)
|
|
|
|
{
|
|
|
|
/* Limit the write length so we write to the current EOF. */
|
|
|
|
|
|
|
|
readwrite.count = sf->entry.datlen - sf->filepos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now perform the write. */
|
|
|
|
|
2014-09-22 17:33:58 +02:00
|
|
|
if (readwrite.count > 0)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = FS_IOCTL(fs, BIOC_WRITESECT, (unsigned long)&readwrite);
|
2014-09-22 17:33:58 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-06-28 15:20:07 +02:00
|
|
|
ferr("ERROR: Error %d writing sector %d data\n",
|
2016-07-09 14:41:39 +02:00
|
|
|
ret, sf->currsector);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2014-09-22 17:33:58 +02:00
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2014-09-22 17:33:58 +02:00
|
|
|
/* Update our control variables */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2014-09-22 17:33:58 +02:00
|
|
|
sf->filepos += readwrite.count;
|
|
|
|
sf->curroffset += readwrite.count;
|
|
|
|
buflen -= readwrite.count;
|
|
|
|
byteswritten += readwrite.count;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Test if we wrote to the end of the current sector */
|
|
|
|
|
|
|
|
if (sf->curroffset == fs->fs_llformat.availbytes)
|
|
|
|
{
|
|
|
|
/* Wrote to the end of the sector. Update to point to the
|
|
|
|
* next sector for additional writes. First read the sector
|
|
|
|
* header to get the sector chain info.
|
|
|
|
*/
|
|
|
|
|
|
|
|
readwrite.offset = 0;
|
2022-09-06 08:18:45 +02:00
|
|
|
readwrite.buffer = (FAR uint8_t *)fs->fs_rwbuffer;
|
2013-05-01 04:13:30 +02:00
|
|
|
readwrite.count = sizeof(struct smartfs_chain_header_s);
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = FS_IOCTL(fs, BIOC_READSECT, (unsigned long)&readwrite);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Error %d reading sector %d header\n",
|
|
|
|
ret, sf->currsector);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now get the chained sector info and reset the offset */
|
|
|
|
|
|
|
|
sf->curroffset = sizeof(struct smartfs_chain_header_s);
|
|
|
|
sf->currsector = SMARTFS_NEXTSECTOR(header);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now append data to end of the file. */
|
|
|
|
|
|
|
|
while (buflen > 0)
|
|
|
|
{
|
2014-04-14 00:22:22 +02:00
|
|
|
/* We will fill up the current sector. Write data to
|
2013-05-01 04:13:30 +02:00
|
|
|
* the current sector first.
|
|
|
|
*/
|
|
|
|
|
2014-12-09 21:11:15 +01:00
|
|
|
#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER
|
|
|
|
readwrite.count = fs->fs_llformat.availbytes - sf->curroffset;
|
|
|
|
if (readwrite.count > buflen)
|
|
|
|
{
|
|
|
|
readwrite.count = buflen;
|
|
|
|
}
|
|
|
|
|
2020-03-30 12:22:26 +02:00
|
|
|
memcpy(&sf->buffer[sf->curroffset], &buffer[byteswritten],
|
|
|
|
readwrite.count);
|
2014-12-09 21:11:15 +01:00
|
|
|
sf->bflags |= SMARTFS_BFLAG_DIRTY;
|
|
|
|
|
|
|
|
#else /* CONFIG_SMARTFS_USE_SECTOR_BUFFER */
|
2013-05-01 04:13:30 +02:00
|
|
|
readwrite.offset = sf->curroffset;
|
|
|
|
readwrite.logsector = sf->currsector;
|
2022-09-06 08:18:45 +02:00
|
|
|
readwrite.buffer = (FAR uint8_t *)&buffer[byteswritten];
|
2013-05-01 04:13:30 +02:00
|
|
|
readwrite.count = fs->fs_llformat.availbytes - sf->curroffset;
|
|
|
|
if (readwrite.count > buflen)
|
|
|
|
{
|
|
|
|
/* Limit the write base on remaining bytes to write */
|
|
|
|
|
|
|
|
readwrite.count = buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the write */
|
|
|
|
|
2014-09-22 17:33:58 +02:00
|
|
|
if (readwrite.count > 0)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = FS_IOCTL(fs, BIOC_WRITESECT, (unsigned long)&readwrite);
|
2014-09-22 17:33:58 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Error %d writing sector %d data\n",
|
|
|
|
ret, sf->currsector);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2014-09-22 17:33:58 +02:00
|
|
|
}
|
2014-12-09 21:11:15 +01:00
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_SMARTFS_USE_SECTOR_BUFFER */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2014-12-09 21:11:15 +01:00
|
|
|
/* Update our control variables */
|
|
|
|
|
|
|
|
sf->entry.datlen += readwrite.count;
|
|
|
|
sf->byteswritten += readwrite.count;
|
|
|
|
sf->filepos += readwrite.count;
|
|
|
|
sf->curroffset += readwrite.count;
|
|
|
|
buflen -= readwrite.count;
|
|
|
|
byteswritten += readwrite.count;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Test if we wrote a full sector of data */
|
|
|
|
|
2014-12-09 21:11:15 +01:00
|
|
|
#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER
|
|
|
|
if (sf->curroffset == fs->fs_llformat.availbytes && buflen)
|
|
|
|
{
|
|
|
|
/* First get a new chained sector */
|
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
ret = FS_IOCTL(fs, BIOC_ALLOCSECT, 0xffff);
|
2014-12-09 21:11:15 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Error %d allocating new sector\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2014-12-09 21:11:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the new sector to the old one and chain it */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
header = (FAR struct smartfs_chain_header_s *)sf->buffer;
|
2022-09-05 20:34:58 +02:00
|
|
|
SMARTFS_SET_NEXTSECTOR(header, ret);
|
2014-12-09 21:11:15 +01:00
|
|
|
|
|
|
|
/* Now sync the file to write this sector out */
|
|
|
|
|
|
|
|
ret = smartfs_sync_internal(fs, sf);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2014-12-09 21:11:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Record the new sector in our tracking variables and
|
|
|
|
* reset the offset to "zero".
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sf->currsector == SMARTFS_NEXTSECTOR(header))
|
|
|
|
{
|
|
|
|
/* Error allocating logical sector! */
|
|
|
|
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Duplicate logical sector %d\n", sf->currsector);
|
2014-12-09 21:11:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sf->bflags = SMARTFS_BFLAG_DIRTY;
|
|
|
|
sf->currsector = SMARTFS_NEXTSECTOR(header);
|
|
|
|
sf->curroffset = sizeof(struct smartfs_chain_header_s);
|
2020-03-30 12:22:26 +02:00
|
|
|
memset(sf->buffer, CONFIG_SMARTFS_ERASEDSTATE,
|
|
|
|
fs->fs_llformat.availbytes);
|
2014-12-09 21:11:15 +01:00
|
|
|
header->type = SMARTFS_DIRENT_TYPE_FILE;
|
|
|
|
}
|
|
|
|
#else /* CONFIG_SMARTFS_USE_SECTOR_BUFFER */
|
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
if (sf->curroffset == fs->fs_llformat.availbytes)
|
|
|
|
{
|
|
|
|
/* Sync the file to write this sector out */
|
|
|
|
|
2014-12-09 21:11:15 +01:00
|
|
|
ret = smartfs_sync_internal(fs, sf);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2014-12-09 21:11:15 +01:00
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Allocate a new sector if needed */
|
|
|
|
|
|
|
|
if (buflen > 0)
|
|
|
|
{
|
|
|
|
/* Allocate a new sector */
|
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
ret = FS_IOCTL(fs, BIOC_ALLOCSECT, 0xffff);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Error %d allocating new sector\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the new sector to the old one and chain it */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
header = (FAR struct smartfs_chain_header_s *)fs->fs_rwbuffer;
|
2022-09-05 20:34:58 +02:00
|
|
|
SMARTFS_SET_NEXTSECTOR(header, ret);
|
2013-05-01 04:13:30 +02:00
|
|
|
readwrite.offset = offsetof(struct smartfs_chain_header_s,
|
|
|
|
nextsector);
|
2022-09-06 08:18:45 +02:00
|
|
|
readwrite.buffer = (FAR uint8_t *)header->nextsector;
|
2013-05-01 04:13:30 +02:00
|
|
|
readwrite.count = sizeof(uint16_t);
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = FS_IOCTL(fs, BIOC_WRITESECT, (unsigned long)&readwrite);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Error %d writing next sector\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Record the new sector in our tracking variables and
|
|
|
|
* reset the offset to "zero".
|
|
|
|
*/
|
|
|
|
|
2014-12-09 21:11:15 +01:00
|
|
|
if (sf->currsector == SMARTFS_NEXTSECTOR(header))
|
|
|
|
{
|
|
|
|
/* Error allocating logical sector! */
|
|
|
|
|
2020-03-30 12:22:26 +02:00
|
|
|
ferr("ERROR: Duplicate logical sector %d\n",
|
|
|
|
sf->currsector);
|
2014-12-09 21:11:15 +01:00
|
|
|
}
|
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
sf->currsector = SMARTFS_NEXTSECTOR(header);
|
|
|
|
sf->curroffset = sizeof(struct smartfs_chain_header_s);
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_SMARTFS_USE_SECTOR_BUFFER */
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = byteswritten;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_seek
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static off_t smartfs_seek(FAR struct file *filep, off_t offset, int whence)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
FAR struct smartfs_ofile_s *sf;
|
|
|
|
int ret;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
sf = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(fs != NULL);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return (off_t)ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Call our internal routine to perform the seek */
|
|
|
|
|
|
|
|
ret = smartfs_seek_internal(fs, sf, offset, whence);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
filep->f_pos = ret;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int smartfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
/* We don't use any ioctls */
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_sync
|
|
|
|
*
|
|
|
|
* Description: Synchronize the file state on disk to match internal, in-
|
|
|
|
* memory state.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int smartfs_sync(FAR struct file *filep)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
FAR struct smartfs_ofile_s *sf;
|
|
|
|
int ret;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
sf = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(fs != NULL);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
ret = smartfs_sync_internal(fs, sf);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smart_dup
|
|
|
|
*
|
|
|
|
* Description: Duplicate open file data in the new file structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int smartfs_dup(FAR const struct file *oldp, FAR struct file *newp)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct smartfs_ofile_s *sf;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(oldp->f_priv != NULL &&
|
|
|
|
newp->f_priv == NULL &&
|
|
|
|
newp->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
sf = oldp->f_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(sf != NULL);
|
|
|
|
|
|
|
|
/* Just increment the reference count on the ofile */
|
|
|
|
|
|
|
|
sf->crefs++;
|
2022-09-06 08:18:45 +02:00
|
|
|
newp->f_priv = sf;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-02-12 20:42:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_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 smartfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
|
|
|
|
{
|
2017-02-14 17:30:11 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
FAR struct smartfs_ofile_s *sf;
|
2020-03-30 12:22:26 +02:00
|
|
|
int ret;
|
2017-02-14 17:30:11 +01:00
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(buf != NULL);
|
2017-02-14 17:30:11 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2017-02-14 17:30:11 +01:00
|
|
|
sf = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
fs = inode->i_private;
|
|
|
|
DEBUGASSERT(fs != NULL);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2017-02-14 17:30:11 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-02-14 17:30:11 +01:00
|
|
|
|
|
|
|
/* Return information about the directory entry in the stat structure */
|
|
|
|
|
|
|
|
smartfs_stat_common(fs, &sf->entry, buf);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2017-02-14 17:30:11 +01:00
|
|
|
return OK;
|
2017-02-12 20:42:27 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 17:54:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_truncate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the length of the open, regular file associated with the file
|
|
|
|
* structure 'filep' to 'length'.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int smartfs_truncate(FAR struct file *filep, off_t length)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
FAR struct smartfs_ofile_s *sf;
|
|
|
|
off_t oldsize;
|
|
|
|
int ret;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2018-01-04 17:54:54 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
sf = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(fs != NULL);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2018-01-04 17:54:54 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2018-01-04 17:54:54 +01:00
|
|
|
|
|
|
|
/* Test the permissions. Only allow truncation if the file was opened with
|
|
|
|
* write flags.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((sf->oflags & O_WROK) == 0)
|
|
|
|
{
|
|
|
|
ret = -EACCES;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2018-01-04 17:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we shrinking the file? Or extending it? */
|
|
|
|
|
|
|
|
oldsize = sf->entry.datlen;
|
|
|
|
if (oldsize == length)
|
|
|
|
{
|
2018-01-06 17:42:36 +01:00
|
|
|
/* Let's not and say we did */
|
|
|
|
|
2018-01-04 17:54:54 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else if (oldsize > length)
|
|
|
|
{
|
|
|
|
/* We are shrinking the file */
|
|
|
|
|
2018-01-06 17:42:36 +01:00
|
|
|
ret = smartfs_shrinkfile(fs, sf, length);
|
2018-01-04 17:54:54 +01:00
|
|
|
}
|
2018-01-06 17:42:36 +01:00
|
|
|
else
|
2018-01-04 17:54:54 +01:00
|
|
|
{
|
2018-01-06 17:42:36 +01:00
|
|
|
/* Otherwise we are extending the file. This is essentially the same
|
|
|
|
* as a write except that (1) we write zeros and (2) we don't update
|
|
|
|
* the file position.
|
2018-01-04 17:54:54 +01:00
|
|
|
*/
|
|
|
|
|
2018-01-06 17:42:36 +01:00
|
|
|
ret = smartfs_extendfile(fs, sf, length);
|
2018-01-04 17:54:54 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2018-01-04 17:54:54 +01:00
|
|
|
/* Relinquish exclusive access */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2018-01-04 17:54:54 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_opendir
|
|
|
|
*
|
|
|
|
* Description: Open a directory for read access
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 12:22:26 +02:00
|
|
|
static int smartfs_opendir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
FAR struct smartfs_dir_s *sdir;
|
|
|
|
int ret;
|
|
|
|
struct smartfs_entry_s entry;
|
|
|
|
uint16_t parentdirsector;
|
|
|
|
FAR const char *filename;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
2022-08-05 06:31:25 +02:00
|
|
|
sdir = kmm_zalloc(sizeof(*sdir));
|
|
|
|
if (sdir == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
goto errout_with_sdir;
|
2020-03-30 12:22:26 +02:00
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Search for the path on the volume */
|
|
|
|
|
|
|
|
entry.name = NULL;
|
|
|
|
ret = smartfs_finddirentry(fs, &entry, relpath, &parentdirsector,
|
|
|
|
&filename);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Populate our private data in the fs_dirent_s struct */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
sdir->fs_firstsector = entry.firstsector;
|
|
|
|
sdir->fs_currsector = entry.firstsector;
|
|
|
|
sdir->fs_curroffset = sizeof(struct smartfs_chain_header_s);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
*dir = &sdir->fs_base;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2022-08-05 06:31:25 +02:00
|
|
|
return OK;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
/* If space for the entry name was allocated, then free it */
|
|
|
|
|
|
|
|
if (entry.name != NULL)
|
|
|
|
{
|
2020-03-30 12:22:26 +02:00
|
|
|
kmm_free(entry.name);
|
|
|
|
entry.name = NULL;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2022-08-05 06:31:25 +02:00
|
|
|
|
|
|
|
errout_with_sdir:
|
|
|
|
kmm_free(sdir);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_closedir
|
|
|
|
*
|
|
|
|
* Description: Close directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int smartfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dir);
|
|
|
|
kmm_free(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_readdir
|
|
|
|
*
|
|
|
|
* Description: Read the next directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
static int smartfs_readdir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *dentry)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
FAR struct smartfs_dir_s *sdir;
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct smartfs_chain_header_s *header;
|
|
|
|
struct smart_read_write_s readwrite;
|
|
|
|
FAR struct smartfs_entry_header_s *entry;
|
|
|
|
uint16_t entrysize;
|
|
|
|
int ret;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
2022-08-05 06:31:25 +02:00
|
|
|
sdir = (FAR struct smartfs_dir_s *)dir;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Read sectors and search entries until one found or no more */
|
|
|
|
|
2014-04-14 00:22:22 +02:00
|
|
|
entrysize = sizeof(struct smartfs_entry_header_s) +
|
2013-05-01 04:13:30 +02:00
|
|
|
fs->fs_llformat.namesize;
|
2022-09-05 01:02:24 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
while (sdir->fs_currsector != SMARTFS_ERASEDSTATE_16BIT)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
|
|
|
/* Read the logical sector */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
readwrite.logsector = sdir->fs_currsector;
|
2013-05-01 04:13:30 +02:00
|
|
|
readwrite.count = fs->fs_llformat.availbytes;
|
2022-09-06 08:18:45 +02:00
|
|
|
readwrite.buffer = (FAR uint8_t *)fs->fs_rwbuffer;
|
2013-05-01 04:13:30 +02:00
|
|
|
readwrite.offset = 0;
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = FS_IOCTL(fs, BIOC_READSECT, (unsigned long)&readwrite);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now search for entries, starting at curroffset */
|
|
|
|
|
2022-09-05 01:02:24 +02:00
|
|
|
/* Note: directories don't use the header's used field
|
|
|
|
* so we search all possilble directory entries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (sdir->fs_curroffset + entrysize < ret)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
|
|
|
/* Point to next entry */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
entry = (FAR struct smartfs_entry_header_s *)&fs->fs_rwbuffer[
|
2022-08-05 06:31:25 +02:00
|
|
|
sdir->fs_curroffset];
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Test if this entry is valid and active */
|
|
|
|
|
2022-09-05 01:02:24 +02:00
|
|
|
#ifdef CONFIG_SMARTFS_ALIGNED_ACCESS
|
|
|
|
if (((smartfs_rdle16(&entry->flags)
|
|
|
|
& SMARTFS_DIRENT_EMPTY) ==
|
|
|
|
(SMARTFS_ERASEDSTATE_16BIT & SMARTFS_DIRENT_EMPTY)) ||
|
|
|
|
((smartfs_rdle16(&entry->flags)
|
|
|
|
& SMARTFS_DIRENT_ACTIVE) !=
|
|
|
|
(SMARTFS_ERASEDSTATE_16BIT & SMARTFS_DIRENT_ACTIVE)))
|
|
|
|
#else
|
2013-05-01 04:13:30 +02:00
|
|
|
if (((entry->flags & SMARTFS_DIRENT_EMPTY) ==
|
|
|
|
(SMARTFS_ERASEDSTATE_16BIT & SMARTFS_DIRENT_EMPTY)) ||
|
|
|
|
((entry->flags & SMARTFS_DIRENT_ACTIVE) !=
|
|
|
|
(SMARTFS_ERASEDSTATE_16BIT & SMARTFS_DIRENT_ACTIVE)))
|
2022-09-05 01:02:24 +02:00
|
|
|
#endif
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
|
|
|
/* This entry isn't valid, skip it */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
sdir->fs_curroffset += entrysize;
|
2013-05-01 04:13:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Entry found! Report it */
|
|
|
|
|
2022-09-05 01:02:24 +02:00
|
|
|
#ifdef CONFIG_SMARTFS_ALIGNED_ACCESS
|
|
|
|
if ((smartfs_rdle16(&entry->flags) & SMARTFS_DIRENT_TYPE) ==
|
|
|
|
SMARTFS_DIRENT_TYPE_DIR)
|
|
|
|
#else
|
2020-03-30 12:22:26 +02:00
|
|
|
if ((entry->flags & SMARTFS_DIRENT_TYPE) ==
|
|
|
|
SMARTFS_DIRENT_TYPE_DIR)
|
2022-09-05 01:02:24 +02:00
|
|
|
#endif
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
dentry->d_type = DTYPE_DIRECTORY;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
dentry->d_type = DTYPE_FILE;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the entry name to dirent */
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
strlcpy(dentry->d_name, entry->name, sizeof(dentry->d_name));
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Now advance to the next entry */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
sdir->fs_curroffset += entrysize;
|
|
|
|
if (sdir->fs_curroffset + entrysize >=
|
2020-03-30 12:22:26 +02:00
|
|
|
fs->fs_llformat.availbytes)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
|
|
|
/* We advanced past the end of the sector. Go to next sector */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
sdir->fs_curroffset =
|
2020-03-30 12:22:26 +02:00
|
|
|
sizeof(struct smartfs_chain_header_s);
|
2022-09-06 08:18:45 +02:00
|
|
|
header = (FAR struct smartfs_chain_header_s *)fs->fs_rwbuffer;
|
2022-08-05 06:31:25 +02:00
|
|
|
sdir->fs_currsector = SMARTFS_NEXTSECTOR(header);
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
/* Now exit */
|
|
|
|
|
|
|
|
ret = OK;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No more entries in this sector. Move on to next sector and
|
|
|
|
* continue the search. If no more sectors, then we are all
|
|
|
|
* done and will report ENOENT.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
header = (FAR struct smartfs_chain_header_s *)fs->fs_rwbuffer;
|
2022-08-05 06:31:25 +02:00
|
|
|
sdir->fs_curroffset = sizeof(struct smartfs_chain_header_s);
|
|
|
|
sdir->fs_currsector = SMARTFS_NEXTSECTOR(header);
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we arrive here, then there are no more entries */
|
|
|
|
|
|
|
|
ret = -ENOENT;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_rewindir
|
|
|
|
*
|
|
|
|
* Description: Reset directory read to the first entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static int smartfs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct smartfs_dir_s *sdir = (FAR struct smartfs_dir_s *)dir;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Reset the directory to the first entry */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
sdir->fs_currsector = sdir->fs_firstsector;
|
|
|
|
sdir->fs_curroffset = sizeof(struct smartfs_chain_header_s);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
return 0;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_bind
|
|
|
|
*
|
|
|
|
* Description: This implements a portion of the mount operation. This
|
|
|
|
* function allocates and initializes the mountpoint private data and
|
|
|
|
* binds the blockdriver inode to the filesystem private data. The final
|
|
|
|
* binding of the private data (containing the blockdriver) to the
|
|
|
|
* mountpoint is performed by mount().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static int smartfs_bind(FAR struct inode *blkdriver, FAR const void *data,
|
|
|
|
FAR void **handle)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
2013-05-01 04:13:30 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Open the block driver */
|
|
|
|
|
|
|
|
if (!blkdriver || !blkdriver->u.i_bops)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blkdriver->u.i_bops->open &&
|
|
|
|
blkdriver->u.i_bops->open(blkdriver) != OK)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an instance of the mountpt state structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
fs = (FAR struct smartfs_mountpt_s *)
|
2020-03-30 12:22:26 +02:00
|
|
|
kmm_zalloc(sizeof(struct smartfs_mountpt_s));
|
2013-05-01 04:13:30 +02:00
|
|
|
if (!fs)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
|
|
|
if (ret < 0)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
kmm_free(fs);
|
|
|
|
return ret;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the allocated mountpt state structure. The filesystem is
|
|
|
|
* responsible for one reference ont the blkdriver inode and does not
|
|
|
|
* have to addref() here (but does have to release in ubind().
|
|
|
|
*/
|
|
|
|
|
|
|
|
fs->fs_blkdriver = blkdriver; /* Save the block driver reference */
|
|
|
|
fs->fs_head = NULL;
|
|
|
|
|
|
|
|
/* Now perform the mount. */
|
|
|
|
|
|
|
|
ret = smartfs_mount(fs, true);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2017-06-28 15:20:07 +02:00
|
|
|
kmm_free(fs);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
*handle = fs;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_unbind
|
|
|
|
*
|
|
|
|
* Description: This implements the filesystem portion of the umount
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-15 00:22:02 +01:00
|
|
|
static int smartfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
|
|
|
unsigned int flags)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2015-10-11 19:39:29 +02:00
|
|
|
FAR struct smartfs_mountpt_s *fs = (FAR struct smartfs_mountpt_s *)handle;
|
2013-05-01 04:13:30 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!fs)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if there are sill any files opened on the filesystem. */
|
|
|
|
|
|
|
|
ret = OK; /* Assume success */
|
2020-03-30 12:22:26 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
if (fs->fs_head != NULL)
|
|
|
|
{
|
|
|
|
/* We cannot unmount now.. there are open files */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2015-03-15 00:22:02 +01:00
|
|
|
/* This implementation currently only supports unmounting if there are
|
|
|
|
* no open file references.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (flags != 0) ? -ENOSYS : -EBUSY;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Unmount ... close the block driver */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
ret = smartfs_unmount(fs);
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(fs);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_statfs
|
|
|
|
*
|
|
|
|
* Description: Return filesystem statistics
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static int smartfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
2014-11-25 20:46:14 +01:00
|
|
|
int ret;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Implement the logic!! */
|
|
|
|
|
|
|
|
buf->f_type = SMARTFS_MAGIC;
|
|
|
|
|
|
|
|
/* Re-request the low-level format info to update free blocks */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = FS_IOCTL(fs, BIOC_GETFORMAT, (unsigned long)&fs->fs_llformat);
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
buf->f_namelen = fs->fs_llformat.namesize;
|
|
|
|
buf->f_bsize = fs->fs_llformat.sectorsize;
|
|
|
|
buf->f_blocks = fs->fs_llformat.nsectors;
|
2014-12-09 21:11:15 +01:00
|
|
|
if (buf->f_blocks == 65535)
|
|
|
|
{
|
|
|
|
buf->f_blocks++;
|
|
|
|
}
|
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
buf->f_bfree = fs->fs_llformat.nfreesectors;
|
|
|
|
buf->f_bavail = fs->fs_llformat.nfreesectors;
|
|
|
|
buf->f_files = 0;
|
|
|
|
buf->f_ffree = fs->fs_llformat.nfreesectors;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_unlink
|
|
|
|
*
|
|
|
|
* Description: Remove a file
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static int smartfs_unlink(FAR struct inode *mountpt, FAR const char *relpath)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
int ret;
|
|
|
|
struct smartfs_entry_s entry;
|
|
|
|
FAR const char *filename;
|
|
|
|
uint16_t parentdirsector;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Locate the directory entry for this path */
|
|
|
|
|
|
|
|
entry.name = NULL;
|
|
|
|
ret = smartfs_finddirentry(fs, &entry, relpath, &parentdirsector,
|
|
|
|
&filename);
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* The name exists -- validate it is a file, not a dir */
|
|
|
|
|
|
|
|
if ((entry.flags & SMARTFS_DIRENT_TYPE) == SMARTFS_DIRENT_TYPE_DIR)
|
|
|
|
{
|
|
|
|
ret = -EISDIR;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Need to check permissions? */
|
|
|
|
|
2020-07-02 16:03:27 +02:00
|
|
|
/* Okay, we are clear to delete the file. Use the deleteentry
|
|
|
|
* routine.
|
|
|
|
*/
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
smartfs_deleteentry(fs, &entry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Just report the error */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2016-07-13 01:28:06 +02:00
|
|
|
if (entry.name != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(entry.name);
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_mkdir
|
|
|
|
*
|
|
|
|
* Description: Create a directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static int smartfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
|
2020-03-30 12:22:26 +02:00
|
|
|
mode_t mode)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
int ret;
|
|
|
|
struct smartfs_entry_s entry;
|
|
|
|
uint16_t parentdirsector;
|
|
|
|
FAR const char *filename;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Locate the directory entry for this path */
|
|
|
|
|
|
|
|
entry.name = NULL;
|
|
|
|
ret = smartfs_finddirentry(fs, &entry, relpath, &parentdirsector,
|
|
|
|
&filename);
|
|
|
|
|
|
|
|
/* Three possibililities: (1) a node exists for the relpath and
|
|
|
|
* dirinfo describes the directory entry of the entity, (2) the
|
|
|
|
* node does not exist, or (3) some error occurred.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* The name exists -- can't create */
|
|
|
|
|
|
|
|
ret = -EEXIST;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
else if (ret == -ENOENT)
|
|
|
|
{
|
|
|
|
/* It doesn't exist ... we can create it, but only if we have
|
2019-03-01 22:00:00 +01:00
|
|
|
* the right permissions and if the parentdirsector is valid.
|
|
|
|
*/
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
if (parentdirsector == 0xffff)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Invalid entry in the path (non-existent dir segment) */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check mode */
|
|
|
|
|
|
|
|
/* Create the directory */
|
|
|
|
|
2014-04-14 00:22:22 +02:00
|
|
|
ret = smartfs_createentry(fs, parentdirsector, filename,
|
2019-10-27 18:48:14 +01:00
|
|
|
SMARTFS_DIRENT_TYPE_DIR, mode, &entry, 0xffff, NULL);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2013-05-01 04:13:30 +02:00
|
|
|
if (entry.name != NULL)
|
|
|
|
{
|
|
|
|
/* Free the filename space allocation */
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(entry.name);
|
2013-05-01 04:13:30 +02:00
|
|
|
entry.name = NULL;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_rmdir
|
|
|
|
*
|
|
|
|
* Description: Remove a directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
int smartfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
int ret;
|
|
|
|
struct smartfs_entry_s entry;
|
|
|
|
FAR const char *filename;
|
|
|
|
uint16_t parentdirsector;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the lock */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Locate the directory entry for this path */
|
|
|
|
|
|
|
|
entry.name = NULL;
|
|
|
|
ret = smartfs_finddirentry(fs, &entry, relpath, &parentdirsector,
|
|
|
|
&filename);
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* The name exists -- validate it is a dir, not a file */
|
|
|
|
|
|
|
|
if ((entry.flags & SMARTFS_DIRENT_TYPE) == SMARTFS_DIRENT_TYPE_FILE)
|
|
|
|
{
|
|
|
|
ret = -ENOTDIR;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Need to check permissions? */
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Check if the directory is empty */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
ret = smartfs_countdirentries(fs, &entry);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Only continue if there are zero entries in the directory */
|
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
ret = -ENOTEMPTY;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
2020-07-02 16:03:27 +02:00
|
|
|
/* Okay, we are clear to delete the directory. Use the deleteentry
|
|
|
|
* routine.
|
|
|
|
*/
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
ret = smartfs_deleteentry(fs, &entry);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Just report the error */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2016-07-13 01:28:06 +02:00
|
|
|
if (entry.name != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(entry.name);
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_rename
|
|
|
|
*
|
|
|
|
* Description: Rename a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
int smartfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
|
|
|
|
FAR const char *newrelpath)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
|
|
|
int ret;
|
|
|
|
struct smartfs_entry_s oldentry;
|
|
|
|
uint16_t oldparentdirsector;
|
|
|
|
FAR const char *oldfilename;
|
|
|
|
struct smartfs_entry_s newentry;
|
|
|
|
uint16_t newparentdirsector;
|
|
|
|
FAR const char *newfilename;
|
|
|
|
mode_t mode;
|
|
|
|
uint16_t type;
|
|
|
|
FAR struct smartfs_entry_header_s *direntry;
|
|
|
|
struct smart_read_write_s readwrite;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Search for old entry to validate it exists */
|
|
|
|
|
|
|
|
oldentry.name = NULL;
|
|
|
|
newentry.name = NULL;
|
|
|
|
ret = smartfs_finddirentry(fs, &oldentry, oldrelpath, &oldparentdirsector,
|
2017-02-11 18:18:30 +01:00
|
|
|
&oldfilename);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 18:18:30 +01:00
|
|
|
/* Search for the new entry and validate it DOESN'T exist. */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
ret = smartfs_finddirentry(fs, &newentry, newrelpath, &newparentdirsector,
|
2017-02-11 18:18:30 +01:00
|
|
|
&newfilename);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2017-02-11 18:18:30 +01:00
|
|
|
/* It is an error if the directory entry at newrelpath already
|
|
|
|
* exists. The necessary steps to avoid this case should have been
|
|
|
|
* handled by higher level logic in the VFS.
|
|
|
|
*/
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2017-02-11 18:18:30 +01:00
|
|
|
ret = -EEXIST;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if the new parent directory is valid */
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
if (newparentdirsector != 0xffff)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
|
|
|
/* We can move to the given parent directory */
|
|
|
|
|
|
|
|
mode = oldentry.flags & SMARTFS_DIRENT_MODE;
|
|
|
|
type = oldentry.flags & SMARTFS_DIRENT_TYPE;
|
2014-04-14 00:22:22 +02:00
|
|
|
ret = smartfs_createentry(fs, newparentdirsector, newfilename,
|
2020-03-30 12:22:26 +02:00
|
|
|
type, mode, &newentry, oldentry.firstsector,
|
|
|
|
NULL);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now mark the old entry as inactive */
|
|
|
|
|
|
|
|
readwrite.logsector = oldentry.dsector;
|
|
|
|
readwrite.offset = 0;
|
|
|
|
readwrite.count = fs->fs_llformat.availbytes;
|
2022-09-06 08:18:45 +02:00
|
|
|
readwrite.buffer = (FAR uint8_t *)fs->fs_rwbuffer;
|
|
|
|
ret = FS_IOCTL(fs, BIOC_READSECT, (unsigned long)&readwrite);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Error %d reading sector %d data\n",
|
|
|
|
ret, oldentry.dsector);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
direntry = (FAR struct smartfs_entry_header_s *)
|
2020-03-30 12:22:26 +02:00
|
|
|
&fs->fs_rwbuffer[oldentry.doffset];
|
2019-10-27 18:48:14 +01:00
|
|
|
#if CONFIG_SMARTFS_ERASEDSTATE == 0xff
|
2013-05-01 04:13:30 +02:00
|
|
|
direntry->flags &= ~SMARTFS_DIRENT_ACTIVE;
|
|
|
|
#else
|
|
|
|
direntry->flags |= SMARTFS_DIRENT_ACTIVE;
|
|
|
|
#endif
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
/* Now write the updated flags back to the device */
|
|
|
|
|
|
|
|
readwrite.offset = oldentry.doffset;
|
|
|
|
readwrite.count = sizeof(direntry->flags);
|
2022-09-06 08:18:45 +02:00
|
|
|
readwrite.buffer = (FAR uint8_t *)&direntry->flags;
|
|
|
|
ret = FS_IOCTL(fs, BIOC_WRITESECT, (unsigned long)&readwrite);
|
2013-05-01 04:13:30 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Error %d writing flag bytes for sector %d\n",
|
|
|
|
ret, readwrite.logsector);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Trying to create in a directory that doesn't exist */
|
|
|
|
|
|
|
|
ret = -ENOENT;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2013-05-01 04:13:30 +02:00
|
|
|
if (oldentry.name != NULL)
|
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(oldentry.name);
|
2013-05-01 04:13:30 +02:00
|
|
|
oldentry.name = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newentry.name != NULL)
|
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(newentry.name);
|
2013-05-01 04:13:30 +02:00
|
|
|
newentry.name = NULL;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-14 17:30:11 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_stat_common
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return information about a directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void smartfs_stat_common(FAR struct smartfs_mountpt_s *fs,
|
|
|
|
FAR struct smartfs_entry_s *entry,
|
|
|
|
FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
/* Initialize the stat structure */
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
if (entry->firstsector == fs->fs_rootsector)
|
|
|
|
{
|
|
|
|
/* It's directory name of the mount point */
|
|
|
|
|
|
|
|
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR | S_IWOTH |
|
|
|
|
S_IWGRP | S_IWUSR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-28 15:40:35 +02:00
|
|
|
/* Mask out the file type */
|
|
|
|
|
|
|
|
buf->st_mode = entry->flags & ~S_IFMT;
|
|
|
|
|
|
|
|
/* Add the file type based on the SmartFS entry flags */
|
|
|
|
|
2017-02-14 17:30:11 +01:00
|
|
|
if ((entry->flags & SMARTFS_DIRENT_TYPE) == SMARTFS_DIRENT_TYPE_DIR)
|
|
|
|
{
|
|
|
|
buf->st_mode |= S_IFDIR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf->st_mode |= S_IFREG;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->st_size = entry->datlen;
|
|
|
|
buf->st_blksize = fs->fs_llformat.availbytes;
|
2020-03-30 12:22:26 +02:00
|
|
|
buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
|
|
|
|
buf->st_blksize;
|
2017-02-14 17:30:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 04:13:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: smartfs_stat
|
|
|
|
*
|
2017-02-14 17:30:11 +01:00
|
|
|
* Description:
|
|
|
|
* Return information about a file or directory
|
2013-05-01 04:13:30 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-14 17:30:11 +01:00
|
|
|
static int smartfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct stat *buf)
|
2013-05-01 04:13:30 +02:00
|
|
|
{
|
2017-02-14 17:30:11 +01:00
|
|
|
FAR struct smartfs_mountpt_s *fs;
|
2022-09-06 08:18:45 +02:00
|
|
|
struct smartfs_entry_s entry;
|
2017-02-14 17:30:11 +01:00
|
|
|
FAR const char *filename;
|
|
|
|
uint16_t parentdirsector;
|
|
|
|
int ret;
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/* Find the directory entry corresponding to relpath */
|
|
|
|
|
|
|
|
entry.name = NULL;
|
|
|
|
ret = smartfs_finddirentry(fs, &entry, relpath, &parentdirsector,
|
|
|
|
&filename);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
2017-02-14 17:30:11 +01:00
|
|
|
/* Return information about the directory entry in the stat structure */
|
2013-05-01 04:13:30 +02:00
|
|
|
|
2017-02-14 17:30:11 +01:00
|
|
|
smartfs_stat_common(fs, &entry, buf);
|
2013-05-01 04:13:30 +02:00
|
|
|
ret = OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2013-05-01 04:13:30 +02:00
|
|
|
if (entry.name != NULL)
|
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(entry.name);
|
2013-05-01 04:13:30 +02:00
|
|
|
entry.name = NULL;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2013-05-01 04:13:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|