2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* fs/romfs/fs_romfs.c
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01: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
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01: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.
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2013-01-15 22:01:37 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
#include "fs_romfs.h"
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Type
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure represents one entry node in the romfs file system */
|
|
|
|
|
|
|
|
struct romfs_dir_s
|
|
|
|
{
|
|
|
|
struct fs_dirent_s base; /* Vfs directory structure */
|
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
|
|
|
FAR struct romfs_nodeinfo_s **firstnode; /* The address of first node in the directory */
|
|
|
|
FAR struct romfs_nodeinfo_s **currnode; /* The address of current node into the directory */
|
|
|
|
#else
|
|
|
|
off_t firstoffset; /* Offset to the first entry in the directory */
|
|
|
|
off_t curroffset; /* Current offset into the directory contents */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_open(FAR struct file *filep, FAR const char *relpath,
|
2008-09-10 21:27:49 +02:00
|
|
|
int oflags, mode_t mode);
|
|
|
|
static int romfs_close(FAR struct file *filep);
|
2013-01-14 16:42:03 +01:00
|
|
|
static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
2008-09-10 21:27:49 +02:00
|
|
|
static off_t romfs_seek(FAR struct file *filep, off_t offset, int whence);
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
2023-01-02 14:02:51 +01:00
|
|
|
static int romfs_mmap(FAR struct file *filep,
|
|
|
|
FAR struct mm_map_entry_s *map);
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2017-02-12 20:42:27 +01:00
|
|
|
static int romfs_dup(FAR const struct file *oldp,
|
|
|
|
FAR struct file *newp);
|
|
|
|
static int romfs_fstat(FAR const struct file *filep,
|
|
|
|
FAR struct stat *buf);
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_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 romfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_readdir(FAR struct inode *mountpt,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry);
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
|
|
|
|
FAR void **handle);
|
2015-03-15 00:22:02 +01:00
|
|
|
static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
|
|
|
unsigned int flags);
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_statfs(FAR struct inode *mountpt,
|
|
|
|
FAR struct statfs *buf);
|
|
|
|
|
2017-02-12 23:25:12 +01:00
|
|
|
static int romfs_stat_common(uint8_t type, uint32_t size,
|
|
|
|
uint16_t sectorsize, FAR struct stat *buf);
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct stat *buf);
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2008-09-10 21:27:49 +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_romfs_operations =
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
romfs_open, /* open */
|
|
|
|
romfs_close, /* close */
|
|
|
|
romfs_read, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
romfs_seek, /* seek */
|
2008-09-12 16:34:06 +02:00
|
|
|
romfs_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
romfs_mmap, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2013-01-14 16:42:03 +01:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
NULL, /* sync */
|
2013-01-14 16:42:03 +01:00
|
|
|
romfs_dup, /* dup */
|
2017-02-12 20:42:27 +01:00
|
|
|
romfs_fstat, /* fstat */
|
2021-07-09 07:43:26 +02:00
|
|
|
NULL, /* fchstat */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
romfs_opendir, /* opendir */
|
2022-08-05 06:31:25 +02:00
|
|
|
romfs_closedir, /* closedir */
|
2008-09-10 21:27:49 +02:00
|
|
|
romfs_readdir, /* readdir */
|
|
|
|
romfs_rewinddir, /* rewinddir */
|
|
|
|
|
|
|
|
romfs_bind, /* bind */
|
|
|
|
romfs_unbind, /* unbind */
|
|
|
|
romfs_statfs, /* statfs */
|
|
|
|
|
|
|
|
NULL, /* unlink */
|
|
|
|
NULL, /* mkdir */
|
|
|
|
NULL, /* rmdir */
|
|
|
|
NULL, /* rename */
|
2021-07-09 07:43:26 +02:00
|
|
|
romfs_stat, /* stat */
|
|
|
|
NULL /* chstat */
|
2008-09-10 21:27:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-02-21 09:27:01 +01:00
|
|
|
struct romfs_nodeinfo_s nodeinfo;
|
2013-01-14 16:42:03 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm;
|
|
|
|
FAR struct romfs_file_s *rf;
|
2023-02-08 04:29:09 +01:00
|
|
|
size_t size;
|
2013-01-14 16:42:03 +01:00
|
|
|
int ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Open '%s'\n", relpath);
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_priv == NULL && filep->f_inode != NULL);
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
/* Get mountpoint private data from the inode reference from the file
|
2008-09-12 16:34:06 +02:00
|
|
|
* structure
|
2008-09-10 21:27:49 +02:00
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
rm = (FAR struct romfs_mountpt_s *)filep->f_inode->i_private;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(rm != NULL);
|
|
|
|
|
|
|
|
/* Check if the mount is still healthy */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = romfs_checkmount(rm);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_checkmount failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ROMFS is read-only. Any attempt to open with any kind of write
|
|
|
|
* access is not permitted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Only O_RDONLY supported\n");
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = -EACCES;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Locate the directory entry for this path */
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
ret = romfs_finddirentry(rm, &nodeinfo, relpath);
|
2008-09-10 21:27:49 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Failed to find directory directory entry for '%s': %d\n",
|
2009-06-25 01:33:33 +02:00
|
|
|
relpath, ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The full path exists -- but is the final component a file
|
2017-02-13 14:20:39 +01:00
|
|
|
* or a directory? Or some other Unix file type that is not
|
2021-02-25 13:48:46 +01:00
|
|
|
* appropriate in this context.
|
2017-02-13 13:55:06 +01:00
|
|
|
*
|
|
|
|
* REVISIT: This logic should follow hard/soft link file
|
|
|
|
* types. At present, it returns the ENXIO.
|
2008-09-10 21:27:49 +02:00
|
|
|
*/
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
if (IS_DIRECTORY(nodeinfo.rn_next))
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
/* It is a directory */
|
|
|
|
|
|
|
|
ret = -EISDIR;
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: '%s' is a directory\n", relpath);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
2022-02-21 09:27:01 +01:00
|
|
|
else if (!IS_FILE(nodeinfo.rn_next))
|
2017-02-13 13:55:06 +01:00
|
|
|
{
|
|
|
|
/* ENXIO indicates "The named file is a character special or
|
|
|
|
* block special file, and the device associated with this
|
|
|
|
* special file does not exist."
|
|
|
|
*
|
|
|
|
* Here we also return ENXIO if the file is not a directory
|
|
|
|
* or a regular file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENXIO;
|
|
|
|
ferr("ERROR: '%s' is a special file\n", relpath);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2017-02-13 13:55:06 +01:00
|
|
|
}
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2009-06-25 01:33:33 +02:00
|
|
|
/* Create an instance of the file private data to describe the opened
|
2008-09-10 21:27:49 +02:00
|
|
|
* file.
|
|
|
|
*/
|
|
|
|
|
2023-02-08 04:29:09 +01:00
|
|
|
size = strlen(relpath);
|
|
|
|
rf = kmm_zalloc(sizeof(struct romfs_file_s) + size);
|
2008-09-10 21:27:49 +02:00
|
|
|
if (!rf)
|
|
|
|
{
|
2020-11-20 04:02:05 +01:00
|
|
|
ferr("ERROR: Failed to allocate private data\n");
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = -ENOMEM;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2008-09-12 21:17:15 +02:00
|
|
|
/* Initialize the file private data (only need to initialize
|
|
|
|
* non-zero elements)
|
|
|
|
*/
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
rf->rf_size = nodeinfo.rn_size;
|
|
|
|
rf->rf_type = (uint8_t)(nodeinfo.rn_next & RFNEXT_ALLMODEMASK);
|
2023-02-08 04:29:09 +01:00
|
|
|
strlcpy(rf->rf_path, relpath, size + 1);
|
2008-09-12 16:34:06 +02:00
|
|
|
|
2008-09-12 21:17:15 +02:00
|
|
|
/* Get the start of the file data */
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
ret = romfs_datastart(rm, &nodeinfo, &rf->rf_startoffset);
|
2008-09-12 21:17:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Failed to locate start of file data: %d\n", ret);
|
2020-01-07 16:06:02 +01:00
|
|
|
kmm_free(rf);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-12 21:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure buffering to support access to this file */
|
2008-09-12 16:34:06 +02:00
|
|
|
|
|
|
|
ret = romfs_fileconfigure(rm, rf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Failed configure buffering: %d\n", ret);
|
2020-01-07 16:06:02 +01:00
|
|
|
kmm_free(rf);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-12 16:34:06 +02:00
|
|
|
}
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* Attach the private date to the struct file instance */
|
|
|
|
|
|
|
|
filep->f_priv = rf;
|
|
|
|
|
2021-12-25 12:08:17 +01:00
|
|
|
rm->rm_refs++;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2008-09-10 21:27:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int romfs_close(FAR struct file *filep)
|
|
|
|
{
|
2013-01-14 16:42:03 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm;
|
|
|
|
FAR struct romfs_file_s *rf;
|
2021-12-25 12:08:17 +01:00
|
|
|
int ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Closing\n");
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2009-06-25 01:33:33 +02:00
|
|
|
rf = filep->f_priv;
|
|
|
|
rm = filep->f_inode->i_private;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(rm != NULL);
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2021-12-25 12:08:17 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
rm->rm_refs--;
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2021-12-25 12:08:17 +01:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Do not check if the mount is healthy. We must support closing of
|
|
|
|
* the file even when there is healthy mount.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Deallocate the memory structures created when the open method
|
|
|
|
* was called.
|
|
|
|
*
|
|
|
|
* Free the sector buffer that was used to manage partial sector
|
|
|
|
* accesses.
|
|
|
|
*/
|
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
if (!rm->rm_xipbase && rf->rf_buffer)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(rf->rf_buffer);
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then free the file structure itself. */
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(rf);
|
2008-09-10 21:27:49 +02:00
|
|
|
filep->f_priv = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2013-01-14 16:42:03 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm;
|
|
|
|
FAR struct romfs_file_s *rf;
|
|
|
|
unsigned int bytesread;
|
2022-02-10 04:41:50 +01:00
|
|
|
unsigned int readsize = 0;
|
2013-01-14 16:42:03 +01:00
|
|
|
unsigned int nsectors;
|
|
|
|
uint32_t offset;
|
|
|
|
size_t bytesleft;
|
|
|
|
off_t sector;
|
2015-10-11 19:39:29 +02:00
|
|
|
FAR uint8_t *userbuffer = (FAR uint8_t *)buffer;
|
2013-01-14 16:42:03 +01:00
|
|
|
int sectorndx;
|
|
|
|
int ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2020-11-22 02:52:05 +01:00
|
|
|
finfo("Read %zu bytes from offset %jd\n", buflen, (intmax_t)filep->f_pos);
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2009-06-25 01:33:33 +02:00
|
|
|
rf = filep->f_priv;
|
|
|
|
rm = filep->f_inode->i_private;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(rm != NULL);
|
|
|
|
|
|
|
|
/* Make sure that the mount is still healthy */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = romfs_checkmount(rm);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_checkmount failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the number of bytes left in the file */
|
|
|
|
|
|
|
|
bytesleft = rf->rf_size - filep->f_pos;
|
|
|
|
|
|
|
|
/* Truncate read count so that it does not exceed the number
|
|
|
|
* of bytes left in the file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (buflen > bytesleft)
|
|
|
|
{
|
|
|
|
buflen = bytesleft;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until either (1) all data has been transferred, or (2) an
|
|
|
|
* error occurs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (buflen > 0)
|
|
|
|
{
|
2008-09-11 23:51:41 +02:00
|
|
|
/* Get the first sector and index to read from. */
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
offset = rf->rf_startoffset + filep->f_pos;
|
|
|
|
sector = SEC_NSECTORS(rm, offset);
|
|
|
|
sectorndx = offset & SEC_NDXMASK(rm);
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* Check if the user has provided a buffer large enough to
|
|
|
|
* hold one or more complete sectors -AND- the read is
|
|
|
|
* aligned to a sector boundary.
|
|
|
|
*/
|
|
|
|
|
2008-09-13 00:28:29 +02:00
|
|
|
nsectors = SEC_NSECTORS(rm, buflen);
|
2022-01-04 11:33:11 +01:00
|
|
|
if (nsectors >= rf->rf_ncachesector && sectorndx == 0)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
/* Read maximum contiguous sectors directly to the user's
|
|
|
|
* buffer without using our tiny read buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Read all of the sectors directly into user memory */
|
|
|
|
|
2020-11-22 02:52:05 +01:00
|
|
|
finfo("Read %d sectors starting with %jd\n", nsectors,
|
|
|
|
(intmax_t)sector);
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = romfs_hwread(rm, userbuffer, sector, nsectors);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_hwread failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
bytesread = nsectors * rm->rm_hwsectorsize;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We are reading a partial sector. First, read the whole sector
|
|
|
|
* into the file data buffer. This is a caching buffer so if
|
|
|
|
* it is already there then all is well.
|
|
|
|
*/
|
|
|
|
|
2020-11-22 02:52:05 +01:00
|
|
|
finfo("Read sector %jd\n", (intmax_t)sector);
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = romfs_filecacheread(rm, rf, sector);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_filecacheread failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the partial sector into the user buffer */
|
|
|
|
|
2022-01-04 11:33:11 +01:00
|
|
|
bytesread = (rf->rf_cachesector + rf->rf_ncachesector - sector) *
|
|
|
|
rm->rm_hwsectorsize - sectorndx;
|
|
|
|
sectorndx = rf->rf_ncachesector * rm->rm_hwsectorsize - bytesread;
|
2008-09-10 21:27:49 +02:00
|
|
|
if (bytesread > buflen)
|
|
|
|
{
|
|
|
|
/* We will not read to the end of the buffer */
|
|
|
|
|
|
|
|
bytesread = buflen;
|
|
|
|
}
|
|
|
|
|
2020-03-30 12:22:26 +02:00
|
|
|
finfo("Return %d bytes from sector offset %d\n",
|
|
|
|
bytesread, sectorndx);
|
2008-09-10 21:27:49 +02:00
|
|
|
memcpy(userbuffer, &rf->rf_buffer[sectorndx], bytesread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up for the next sector read */
|
|
|
|
|
|
|
|
userbuffer += bytesread;
|
|
|
|
filep->f_pos += bytesread;
|
|
|
|
readsize += bytesread;
|
|
|
|
buflen -= bytesread;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2022-02-10 04:41:50 +01:00
|
|
|
return ret < 0 ? ret : readsize;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_seek
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static off_t romfs_seek(FAR struct file *filep, off_t offset, int whence)
|
|
|
|
{
|
2013-01-14 16:42:03 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm;
|
|
|
|
FAR struct romfs_file_s *rf;
|
|
|
|
off_t position;
|
|
|
|
int ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2020-11-22 02:52:05 +01:00
|
|
|
finfo("Seek to offset: %jd whence: %d\n", (intmax_t)offset, whence);
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
rf = filep->f_priv;
|
|
|
|
rm = filep->f_inode->i_private;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(rm != NULL);
|
|
|
|
|
|
|
|
/* Map the offset according to the whence option */
|
2008-09-12 16:34:06 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
switch (whence)
|
|
|
|
{
|
|
|
|
case SEEK_SET: /* The offset is set to offset bytes. */
|
|
|
|
position = offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR: /* The offset is set to its current location plus
|
|
|
|
* offset bytes. */
|
|
|
|
|
|
|
|
position = offset + filep->f_pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END: /* The offset is set to the size of the file plus
|
|
|
|
* offset bytes. */
|
|
|
|
|
|
|
|
position = offset + rf->rf_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Whence is invalid: %d\n", whence);
|
2008-09-10 21:27:49 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the mount is still healthy */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return (off_t)ret;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = romfs_checkmount(rm);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_checkmount failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Limit positions to the end of the file. */
|
|
|
|
|
|
|
|
if (position > rf->rf_size)
|
|
|
|
{
|
|
|
|
/* Otherwise, the position is limited to the file size */
|
|
|
|
|
|
|
|
position = rf->rf_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set file position and return success */
|
|
|
|
|
2009-06-25 01:33:33 +02:00
|
|
|
filep->f_pos = position;
|
2020-11-22 02:52:05 +01:00
|
|
|
finfo("New file position: %jd\n", (intmax_t)filep->f_pos);
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2008-09-10 21:27:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int romfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
2023-01-02 18:27:25 +01:00
|
|
|
FAR struct romfs_file_s *rf;
|
2008-09-12 16:34:06 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("cmd: %d arg: %08lx\n", cmd, arg);
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2009-06-25 01:33:33 +02:00
|
|
|
rf = filep->f_priv;
|
2008-09-12 16:34:06 +02:00
|
|
|
|
|
|
|
/* Only one ioctl command is supported */
|
|
|
|
|
2023-01-02 14:02:51 +01:00
|
|
|
if (cmd == FIOC_FILEPATH)
|
2022-02-18 13:36:52 +01:00
|
|
|
{
|
|
|
|
FAR char *ptr = (FAR char *)((uintptr_t)arg);
|
2023-03-05 14:13:32 +01:00
|
|
|
inode_getpath(filep->f_inode, ptr, PATH_MAX);
|
|
|
|
strlcat(ptr, rf->rf_path, PATH_MAX);
|
2022-02-18 13:36:52 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2008-09-12 16:34:06 +02:00
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
ferr("ERROR: Invalid cmd: %d\n", cmd);
|
2008-09-12 16:34:06 +02:00
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
|
2023-01-02 14:02:51 +01:00
|
|
|
static int romfs_mmap(FAR struct file *filep, FAR struct mm_map_entry_s *map)
|
|
|
|
{
|
|
|
|
FAR struct romfs_mountpt_s *rm;
|
|
|
|
FAR struct romfs_file_s *rf;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
rf = filep->f_priv;
|
|
|
|
rm = filep->f_inode->i_private;
|
|
|
|
|
|
|
|
/* Return the address on the media corresponding to the start of
|
|
|
|
* the file.
|
|
|
|
*/
|
|
|
|
|
2023-01-02 18:27:25 +01:00
|
|
|
if (rm->rm_xipbase && map->offset >= 0 && map->offset < rf->rf_size &&
|
|
|
|
map->length != 0 && map->offset + map->length <= rf->rf_size)
|
2023-01-02 14:02:51 +01:00
|
|
|
{
|
|
|
|
map->vaddr = rm->rm_xipbase + rf->rf_startoffset + map->offset;
|
2023-03-10 04:20:14 +01:00
|
|
|
return OK;
|
2023-01-02 14:02:51 +01:00
|
|
|
}
|
|
|
|
|
2023-03-10 04:20:14 +01:00
|
|
|
return -ENOTTY;
|
2023-01-02 14:02:51 +01:00
|
|
|
}
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_dup
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int romfs_dup(FAR const struct file *oldp, FAR struct file *newp)
|
|
|
|
{
|
|
|
|
FAR struct romfs_mountpt_s *rm;
|
|
|
|
FAR struct romfs_file_s *oldrf;
|
|
|
|
FAR struct romfs_file_s *newrf;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
2013-01-14 16:42:03 +01:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(oldp->f_priv != NULL &&
|
|
|
|
newp->f_priv == NULL &&
|
|
|
|
newp->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Get mountpoint private data from the inode reference from the file
|
|
|
|
* structure
|
|
|
|
*/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
rm = newp->f_inode->i_private;
|
2013-01-14 16:42:03 +01:00
|
|
|
DEBUGASSERT(rm != NULL);
|
|
|
|
|
|
|
|
/* Check if the mount is still healthy */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
ret = romfs_checkmount(rm);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_checkmount failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-01-14 16:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Recover the old private data from the old struct file instance */
|
|
|
|
|
|
|
|
oldrf = oldp->f_priv;
|
|
|
|
|
|
|
|
/* Create an new instance of the file private data to describe the new
|
|
|
|
* dup'ed file.
|
|
|
|
*/
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
newrf = kmm_malloc(sizeof(struct romfs_file_s));
|
2013-01-14 16:42:03 +01:00
|
|
|
if (!newrf)
|
|
|
|
{
|
2020-11-20 04:02:05 +01:00
|
|
|
ferr("ERROR: Failed to allocate private data\n");
|
2013-01-14 16:42:03 +01:00
|
|
|
ret = -ENOMEM;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-01-14 16:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy all file private data (except for the buffer) */
|
|
|
|
|
|
|
|
newrf->rf_startoffset = oldrf->rf_startoffset;
|
|
|
|
newrf->rf_size = oldrf->rf_size;
|
|
|
|
|
|
|
|
/* Configure buffering to support access to this file */
|
|
|
|
|
|
|
|
ret = romfs_fileconfigure(rm, newrf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-08-31 02:31:58 +02:00
|
|
|
kmm_free(newrf);
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Failed configure buffering: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-01-14 16:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach the new private date to the new struct file instance */
|
|
|
|
|
|
|
|
newp->f_priv = newrf;
|
|
|
|
|
2021-12-25 12:08:17 +01:00
|
|
|
rm->rm_refs++;
|
2013-01-14 16:42:03 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2013-01-14 16:42:03 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-12 20:42:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_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 romfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
|
|
|
|
{
|
2017-02-12 23:25:12 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm;
|
|
|
|
FAR struct romfs_file_s *rf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
finfo("fstat\n");
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2017-02-13 21:06:39 +01:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
2017-02-12 23:25:12 +01:00
|
|
|
|
|
|
|
/* Get mountpoint private data from the inode reference from the file
|
|
|
|
* structure
|
|
|
|
*/
|
|
|
|
|
|
|
|
rf = filep->f_priv;
|
2022-02-10 04:41:50 +01:00
|
|
|
rm = filep->f_inode->i_private;
|
2017-02-12 23:25:12 +01:00
|
|
|
DEBUGASSERT(rm != NULL);
|
|
|
|
|
|
|
|
/* Check if the mount is still healthy */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-12 23:25:12 +01:00
|
|
|
ret = romfs_checkmount(rm);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Return information about the directory entry */
|
|
|
|
|
2017-02-13 22:33:52 +01:00
|
|
|
ret = romfs_stat_common(rf->rf_type, rf->rf_size,
|
2017-02-12 23:25:12 +01:00
|
|
|
rm->rm_hwsectorsize, buf);
|
|
|
|
}
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2017-02-12 23:25:12 +01:00
|
|
|
return ret;
|
2017-02-12 20:42:27 +01:00
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_opendir
|
|
|
|
*
|
2008-09-12 16:34:06 +02:00
|
|
|
* Description:
|
|
|
|
* Open a directory for read access
|
2008-09-10 21:27:49 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2013-01-14 16:42:03 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct romfs_dir_s *rdir;
|
2022-02-21 09:27:01 +01:00
|
|
|
struct romfs_nodeinfo_s nodeinfo;
|
2013-01-14 16:42:03 +01:00
|
|
|
int ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("relpath: '%s'\n", relpath);
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
|
|
|
rm = mountpt->i_private;
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
rdir = kmm_zalloc(sizeof(*rdir));
|
|
|
|
if (rdir == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Make sure that the mount is still healthy */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
goto errout_with_rdir;
|
2020-03-30 12:22:26 +02:00
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = romfs_checkmount(rm);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_checkmount failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the requested directory */
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
ret = romfs_finddirentry(rm, &nodeinfo, relpath);
|
2008-09-10 21:27:49 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Failed to find directory '%s': %d\n", relpath, ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that it is some kind of directory */
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
if (!IS_DIRECTORY(nodeinfo.rn_next))
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2009-06-25 01:33:33 +02:00
|
|
|
/* The entry is not a directory */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2020-11-20 04:02:05 +01:00
|
|
|
ferr("ERROR: '%s' is not a directory\n", relpath);
|
2009-06-25 01:33:33 +02:00
|
|
|
ret = -ENOTDIR;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The entry is a directory */
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
2022-08-05 06:31:25 +02:00
|
|
|
rdir->firstnode = nodeinfo.rn_child;
|
|
|
|
rdir->currnode = nodeinfo.rn_child;
|
2022-02-09 15:24:04 +01:00
|
|
|
#else
|
2022-08-05 06:31:25 +02:00
|
|
|
rdir->firstoffset = nodeinfo.rn_offset;
|
|
|
|
rdir->curroffset = nodeinfo.rn_offset;
|
2022-02-09 15:24:04 +01:00
|
|
|
#endif
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
*dir = &rdir->base;
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2022-08-05 06:31:25 +02:00
|
|
|
return OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2022-08-05 06:31:25 +02:00
|
|
|
|
|
|
|
errout_with_rdir:
|
|
|
|
kmm_free(rdir);
|
2014-02-11 01:08:49 +01:00
|
|
|
return ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_closedir
|
|
|
|
*
|
|
|
|
* Description: Close the directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int romfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dir);
|
|
|
|
kmm_free(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_readdir
|
|
|
|
*
|
|
|
|
* Description: Read the next directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_readdir(FAR struct inode *mountpt,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2013-01-14 16:42:03 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct romfs_dir_s *rdir;
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifndef CONFIG_FS_ROMFS_CACHE_NODE
|
2013-01-14 16:42:03 +01:00
|
|
|
uint32_t linkoffset;
|
|
|
|
uint32_t info;
|
|
|
|
uint32_t size;
|
2022-02-09 15:24:04 +01:00
|
|
|
#endif
|
|
|
|
uint32_t next;
|
2013-01-14 16:42:03 +01:00
|
|
|
int ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
|
|
|
rm = mountpt->i_private;
|
2022-08-05 06:31:25 +02:00
|
|
|
rdir = (FAR struct romfs_dir_s *)dir;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* Make sure that the mount is still healthy */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = romfs_checkmount(rm);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: omfs_checkmount failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop, skipping over unsupported items in the file system */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
/* Have we reached the end of the directory */
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
2022-08-05 06:31:25 +02:00
|
|
|
if (!rdir->currnode || !(*rdir->currnode))
|
2022-02-09 15:24:04 +01:00
|
|
|
#else
|
2022-08-05 06:31:25 +02:00
|
|
|
if (!rdir->curroffset)
|
2022-02-09 15:24:04 +01:00
|
|
|
#endif
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
/* We signal the end of the directory by returning the
|
|
|
|
* special error -ENOENT
|
|
|
|
*/
|
|
|
|
|
2016-06-12 01:14:02 +02:00
|
|
|
finfo("End of directory\n");
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = -ENOENT;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
2022-08-05 06:31:25 +02:00
|
|
|
next = (*rdir->currnode)->rn_next;
|
|
|
|
strlcpy(entry->d_name, (*rdir->currnode)->rn_name,
|
2022-07-26 08:56:41 +02:00
|
|
|
sizeof(entry->d_name));
|
2022-08-05 06:31:25 +02:00
|
|
|
rdir->currnode++;
|
2022-02-09 15:24:04 +01:00
|
|
|
#else
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Parse the directory entry */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ret = romfs_parsedirentry(rm, rdir->curroffset, &linkoffset,
|
2008-09-10 21:27:49 +02:00
|
|
|
&next, &info, &size);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_parsedirentry failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the filename */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ret = romfs_parsefilename(rm, rdir->curroffset,
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_name);
|
2008-09-10 21:27:49 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_parsefilename failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the next directory entry offset */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
rdir->curroffset = next & RFNEXT_OFFSETMASK;
|
2022-02-09 15:24:04 +01:00
|
|
|
#endif
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* Check the file type */
|
|
|
|
|
|
|
|
if (IS_DIRECTORY(next))
|
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_DIRECTORY;
|
2008-09-10 21:27:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-09-11 07:21:36 +02:00
|
|
|
else if (IS_FILE(next))
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_FILE;
|
2008-09-10 21:27:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-07-05 18:34:32 +02:00
|
|
|
else if (IS_SOFTLINK(next))
|
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_LINK;
|
2020-07-05 18:34:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2008-09-10 21:27:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_rewindir
|
|
|
|
*
|
|
|
|
* Description: Reset directory read to the first entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2013-01-14 16:42:03 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct romfs_dir_s *rdir;
|
2008-09-10 21:27:49 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
|
|
|
rm = mountpt->i_private;
|
2022-08-05 06:31:25 +02:00
|
|
|
rdir = (FAR struct romfs_dir_s *)dir;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* Make sure that the mount is still healthy */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = romfs_checkmount(rm);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
2022-08-05 06:31:25 +02:00
|
|
|
rdir->currnode = rdir->firstnode;
|
2022-02-09 15:24:04 +01:00
|
|
|
#else
|
2022-08-05 06:31:25 +02:00
|
|
|
rdir->curroffset = rdir->firstoffset;
|
2022-02-09 15:24:04 +01:00
|
|
|
#endif
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2008-09-10 21:27:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_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().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
|
|
|
|
FAR void **handle)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
struct romfs_mountpt_s *rm;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Open the block driver */
|
|
|
|
|
2019-01-27 19:07:37 +01:00
|
|
|
if (blkdriver == NULL)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: No block driver/ops\n");
|
2008-09-10 21:27:49 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2019-01-27 19:07:37 +01:00
|
|
|
if (INODE_IS_BLOCK(blkdriver) &&
|
|
|
|
blkdriver->u.i_bops->open != NULL &&
|
2008-09-10 21:27:49 +02:00
|
|
|
blkdriver->u.i_bops->open(blkdriver) != OK)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: No open method\n");
|
2008-09-10 21:27:49 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an instance of the mountpt state structure */
|
|
|
|
|
2022-02-10 04:41:50 +01:00
|
|
|
rm = kmm_zalloc(sizeof(struct romfs_mountpt_s));
|
2008-09-10 21:27:49 +02:00
|
|
|
if (!rm)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Failed to allocate mountpoint structure\n");
|
2008-09-10 21:27:49 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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().
|
|
|
|
*/
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_init(&rm->rm_lock); /* Initialize the mutex that controls access */
|
2022-09-06 08:18:45 +02:00
|
|
|
rm->rm_blkdriver = blkdriver; /* Save the block driver reference */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
/* Get the hardware configuration and setup buffering appropriately */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
ret = romfs_hwconfigure(rm);
|
2008-09-10 21:27:49 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_hwconfigure failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
/* Then complete the mount by getting the ROMFS configuratrion from
|
|
|
|
* the ROMF header
|
|
|
|
*/
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
ret = romfs_fsconfigure(rm);
|
2008-09-10 21:27:49 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_fsconfigure failed: %d\n", ret);
|
2008-09-10 21:27:49 +02:00
|
|
|
goto errout_with_buffer;
|
|
|
|
}
|
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
/* Mounted! */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
*handle = rm;
|
2008-09-10 21:27:49 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_buffer:
|
2008-09-12 16:34:06 +02:00
|
|
|
if (!rm->rm_xipbase)
|
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(rm->rm_buffer);
|
2008-09-12 16:34:06 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout:
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_destroy(&rm->rm_lock);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(rm);
|
2008-09-10 21:27:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_unbind
|
|
|
|
*
|
|
|
|
* Description: This implements the filesystem portion of the umount
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-15 00:22:02 +01:00
|
|
|
static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
|
|
|
unsigned int flags)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2022-02-10 04:41:50 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm = handle;
|
2008-09-10 21:27:49 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-09-10 21:27:49 +02:00
|
|
|
if (!rm)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2009-06-25 01:33:33 +02:00
|
|
|
#endif
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* Check if there are sill any files opened on the filesystem. */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 12:08:17 +01:00
|
|
|
if (rm->rm_refs)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
|
|
|
/* We cannot unmount now.. there are open files */
|
|
|
|
|
2016-06-12 01:14:02 +02:00
|
|
|
fwarn("WARNING: There are open files\n");
|
2015-03-15 00:22:02 +01:00
|
|
|
|
|
|
|
/* This implementation currently only supports unmounting if there are
|
|
|
|
* no open file references.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = (flags != 0) ? -ENOSYS : -EBUSY;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Unmount ... close the block driver */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
if (rm->rm_blkdriver)
|
|
|
|
{
|
2022-02-10 04:41:50 +01:00
|
|
|
FAR struct inode *inode = rm->rm_blkdriver;
|
2008-09-10 21:27:49 +02:00
|
|
|
if (inode)
|
|
|
|
{
|
2019-01-27 19:07:37 +01:00
|
|
|
if (INODE_IS_BLOCK(inode) && inode->u.i_bops->close != NULL)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
inode->u.i_bops->close(inode);
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We hold a reference to the block driver but should
|
2020-03-30 12:22:26 +02:00
|
|
|
* not but mucking with inodes in this context. So, we will
|
|
|
|
* just return our contained reference to the block driver
|
|
|
|
* inode and let the umount logic dispose of it.
|
2008-09-10 21:27:49 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (blkdriver)
|
|
|
|
{
|
|
|
|
*blkdriver = inode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the mountpoint private data */
|
|
|
|
|
2008-09-12 16:34:06 +02:00
|
|
|
if (!rm->rm_xipbase && rm->rm_buffer)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(rm->rm_buffer);
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 15:24:04 +01:00
|
|
|
#ifdef CONFIG_FS_ROMFS_CACHE_NODE
|
|
|
|
romfs_freenode(rm->rm_root);
|
|
|
|
#endif
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_destroy(&rm->rm_lock);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(rm);
|
2011-03-28 02:05:58 +02:00
|
|
|
return OK;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2008-09-10 21:27:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_statfs
|
|
|
|
*
|
|
|
|
* Description: Return filesystem statistics
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2013-01-14 16:42:03 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm;
|
|
|
|
int ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
|
|
|
|
|
|
|
rm = mountpt->i_private;
|
|
|
|
|
|
|
|
/* Check if the mount is still healthy */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = romfs_checkmount(rm);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_checkmount failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in the statfs info */
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(struct statfs));
|
|
|
|
buf->f_type = ROMFS_MAGIC;
|
|
|
|
|
|
|
|
/* We will claim that the optimal transfer size is the size of one sector */
|
|
|
|
|
|
|
|
buf->f_bsize = rm->rm_hwsectorsize;
|
|
|
|
|
|
|
|
/* Everything else follows in units of sectors */
|
|
|
|
|
|
|
|
buf->f_blocks = SEC_NSECTORS(rm, rm->rm_volsize + SEC_NDXMASK(rm));
|
|
|
|
buf->f_bfree = 0;
|
|
|
|
buf->f_bavail = 0;
|
|
|
|
buf->f_namelen = NAME_MAX;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2008-09-10 21:27:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-12 23:25:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_stat_common
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int romfs_stat_common(uint8_t type, uint32_t size,
|
|
|
|
uint16_t sectorsize, FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
if (IS_DIRECTORY(type))
|
|
|
|
{
|
2017-02-13 23:56:55 +01:00
|
|
|
/* It's a read-execute directory name */
|
2017-02-12 23:25:12 +01:00
|
|
|
|
2017-02-13 23:56:55 +01:00
|
|
|
buf->st_mode = S_IFDIR | S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP |
|
|
|
|
S_IRUSR | S_IXUSR;
|
2017-02-12 23:25:12 +01:00
|
|
|
}
|
2020-07-05 17:30:43 +02:00
|
|
|
else if (IS_FILE(type) || IS_SOFTLINK(type))
|
2017-02-12 23:25:12 +01:00
|
|
|
{
|
2020-07-05 17:30:43 +02:00
|
|
|
if (IS_FILE(type))
|
|
|
|
{
|
|
|
|
buf->st_mode = S_IFREG;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf->st_mode = S_IFLNK;
|
|
|
|
}
|
|
|
|
|
2017-02-12 23:25:12 +01:00
|
|
|
/* It's a read-only file name */
|
|
|
|
|
2020-07-05 17:30:43 +02:00
|
|
|
buf->st_mode |= S_IROTH | S_IRGRP | S_IRUSR;
|
2017-02-12 23:25:12 +01:00
|
|
|
if (IS_EXECUTABLE(type))
|
|
|
|
{
|
2017-02-13 23:56:55 +01:00
|
|
|
/* It's a read-execute file name */
|
|
|
|
|
2017-02-12 23:25:12 +01:00
|
|
|
buf->st_mode |= S_IXOTH | S_IXGRP | S_IXUSR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, pretend like the unsupported type does not exist */
|
|
|
|
|
|
|
|
finfo("Unsupported type: %d\n", type);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* File/directory size, access block size */
|
|
|
|
|
|
|
|
buf->st_size = size;
|
|
|
|
buf->st_blksize = sectorsize;
|
|
|
|
buf->st_blocks = (buf->st_size + sectorsize - 1) / sectorsize;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: romfs_stat
|
|
|
|
*
|
|
|
|
* Description: Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-14 16:42:03 +01:00
|
|
|
static int romfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct stat *buf)
|
2008-09-10 21:27:49 +02:00
|
|
|
{
|
2013-01-14 16:42:03 +01:00
|
|
|
FAR struct romfs_mountpt_s *rm;
|
2022-02-21 09:27:01 +01:00
|
|
|
struct romfs_nodeinfo_s nodeinfo;
|
2017-02-12 23:25:12 +01:00
|
|
|
uint8_t type;
|
2013-01-14 16:42:03 +01:00
|
|
|
int ret;
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2009-06-25 01:33:33 +02:00
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
|
|
|
|
|
|
|
rm = mountpt->i_private;
|
|
|
|
|
|
|
|
/* Check if the mount is still healthy */
|
|
|
|
|
2022-11-15 11:32:51 +01:00
|
|
|
ret = nxrmutex_lock(&rm->rm_lock);
|
2020-03-30 12:22:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:27:49 +02:00
|
|
|
ret = romfs_checkmount(rm);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: romfs_checkmount failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the directory entry corresponding to relpath. */
|
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
ret = romfs_finddirentry(rm, &nodeinfo, relpath);
|
2008-09-10 21:27:49 +02:00
|
|
|
|
|
|
|
/* If nothing was found, then we fail with EEXIST */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Failed to find directory: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2008-09-10 21:27:49 +02:00
|
|
|
}
|
|
|
|
|
2017-02-12 23:25:12 +01:00
|
|
|
/* Return information about the directory entry */
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2022-02-21 09:27:01 +01:00
|
|
|
type = (uint8_t)(nodeinfo.rn_next & RFNEXT_ALLMODEMASK);
|
|
|
|
ret = romfs_stat_common(type, nodeinfo.rn_size, rm->rm_hwsectorsize, buf);
|
2008-09-10 21:27:49 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-11-15 11:32:51 +01:00
|
|
|
nxrmutex_unlock(&rm->rm_lock);
|
2008-09-10 21:27:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|