* fs/smartfs/smartfs_procfs.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/sched.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/procfs.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/mtd/smart.h>
#include <arch/irq.h>
#include "smartfs.h"
#include "fs_heap.h"
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_SMARTFS)
* Pre-processor Definitions
****************************************************************************/
* Private Types
****************************************************************************/
* accessed via the procfs file system.
*/
struct smartfs_level1_s
{
struct procfs_dir_priv_s base;
* open / read / stat, etc.
*/
FAR struct smartfs_mountpt_s *mount;
uint8_t direntry;
};
struct smartfs_file_s
{
struct procfs_file_s base;
struct smartfs_level1_s level1;
uint16_t offset;
};
struct smartfs_procfs_entry_s
{
const char *name;
size_t (*read)(FAR struct file *filep, FAR char *buffer, size_t buflen);
ssize_t (*write)(FAR struct file *filep,
FAR const char *buffer, size_t buflen);
uint8_t type;
};
* Private Function Prototypes
****************************************************************************/
static int smartfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode);
static int smartfs_close(FAR struct file *filep);
static ssize_t smartfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t smartfs_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int smartfs_opendir(const char *relpath,
FAR struct fs_dirent_s **dir);
static int smartfs_closedir(FAR struct fs_dirent_s *dir);
static int smartfs_readdir(FAR struct fs_dirent_s *dir,
FAR struct dirent *entry);
static int smartfs_rewinddir(FAR struct fs_dirent_s *dir);
static int smartfs_stat(FAR const char *relpath, FAR struct stat *buf);
static ssize_t smartfs_debug_write(FAR struct file *filep,
FAR const char *buffer, size_t buflen);
static size_t smartfs_status_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
#ifdef CONFIG_MTD_SMART_ALLOC_DEBUG
static size_t smartfs_mem_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
#endif
#ifdef CONFIG_MTD_SMART_SECTOR_ERASE_DEBUG
static size_t smartfs_erasemap_read(FAR struct file *filep,
FAR char *buffer, size_t buflen);
#endif
#ifdef CONFIG_SMARTFS_FILE_SECTOR_DEBUG
static size_t smartfs_files_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
#endif
* Private Data
****************************************************************************/
static const struct smartfs_procfs_entry_s g_direntry[] =
{
{ "debuglevel", NULL, smartfs_debug_write, DTYPE_FILE },
#ifdef CONFIG_MTD_SMART_SECTOR_ERASE_DEBUG
{ "erasemap", smartfs_erasemap_read, NULL, DTYPE_FILE },
#endif
#ifdef CONFIG_MTD_SMART_ALLOC_DEBUG
{ "mem", smartfs_mem_read, NULL, DTYPE_FILE },
#endif
{ "status", smartfs_status_read, NULL, DTYPE_FILE }
};
static const uint8_t g_direntrycount = sizeof(g_direntry) /
sizeof(struct smartfs_procfs_entry_s);
* Public Data
****************************************************************************/
* We use the old-fashioned kind of initializers so that this will compile
* with any compiler.
*/
const struct procfs_operations g_smartfs_procfs_operations =
{
smartfs_open,
smartfs_close,
smartfs_read,
smartfs_write,
NULL,
smartfs_opendir,
smartfs_closedir,
smartfs_readdir,
smartfs_rewinddir,
smartfs_stat
};
* Private Functions
****************************************************************************/
* Name: smartfs_find_dirref
*
* Description:
* Analyse relpath to find the directory reference entry it represents,
* if any.
*
****************************************************************************/
static int smartfs_find_dirref(FAR const char *relpath,
FAR struct smartfs_level1_s *level1)
{
int ret = -ENOENT;
FAR struct smartfs_mountpt_s *mount;
uint16_t x;
FAR char * str;
mount = smartfs_get_first_mount();
if (strncmp(relpath, "fs/smartfs", 10) == 0)
{
relpath += 10;
}
if (relpath[0] == '/')
{
relpath++;
}
if (relpath[0] == '\0')
{
level1->mount = mount;
level1->base.level = 1;
level1->base.nentries = 0;
while (mount != NULL)
{
level1->base.nentries++;
mount = mount->fs_next;
}
level1->base.index = 0;
ret = OK;
}
else
{
str = strchr(relpath, '/');
if (str)
{
x = str - relpath;
}
else
{
x = strlen(relpath);
}
while (mount)
{
if (strncmp(mount->fs_blkdriver->i_name, relpath, x) == 0)
{
break;
}
mount = mount->fs_next;
}
if (mount)
{
ret = OK;
level1->mount = mount;
relpath += strlen(mount->fs_blkdriver->i_name);
if (relpath[0] == '/')
{
relpath++;
}
if (relpath[0] == '\0')
{
* entry
*/
level1->base.level = 2;
level1->base.nentries = g_direntrycount;
level1->base.index = 0;
}
else
{
level1->base.level = 3;
level1->base.nentries = 1;
level1->base.index = 0;
level1->direntry = 0;
while (level1->direntry < g_direntrycount)
{
if (!strcmp(relpath, g_direntry[level1->direntry].name))
{
break;
}
level1->direntry++;
}
if (level1->direntry == g_direntrycount)
{
ret = -ENOENT;
}
}
}
}
return ret;
}
* Name: smartfs_open
****************************************************************************/
static int smartfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
{
FAR struct smartfs_file_s *priv;
int ret;
finfo("Open '%s'\n", relpath);
* access is not permitted.
*
* REVISIT: Write-able proc files could be quite useful.
*/
if (((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0) &&
(g_smartfs_procfs_operations.write == NULL))
{
ferr("ERROR: Only O_RDONLY supported\n");
return -EACCES;
}
priv = fs_heap_malloc(sizeof(struct smartfs_file_s));
if (!priv)
{
ferr("ERROR: Failed to allocate file attributes\n");
return -ENOMEM;
}
ret = smartfs_find_dirref(relpath, &priv->level1);
if (ret == -ENOENT)
{
fs_heap_free(priv);
return ret;
}
priv->offset = 0;
filep->f_priv = (FAR void *)priv;
return OK;
}
* Name: smartfs_close
****************************************************************************/
static int smartfs_close(FAR struct file *filep)
{
FAR struct smartfs_file_s *priv;
priv = (FAR struct smartfs_file_s *)filep->f_priv;
DEBUGASSERT(priv);
fs_heap_free(priv);
filep->f_priv = NULL;
return OK;
}
* Name: smartfs_read
****************************************************************************/
static ssize_t smartfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct smartfs_file_s *priv;
ssize_t ret;
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
priv = (FAR struct smartfs_file_s *)filep->f_priv;
DEBUGASSERT(priv);
ret = 0;
if (priv->level1.base.level == 3)
{
if (priv->level1.direntry < g_direntrycount)
{
if (g_direntry[priv->level1.direntry].read)
{
ret = g_direntry[priv->level1.direntry].read(filep,
buffer, buflen);
}
}
}
if (ret > 0)
{
filep->f_pos += ret;
}
return ret;
}
* Name: smartfs_write
****************************************************************************/
static ssize_t smartfs_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
{
FAR struct smartfs_file_s *priv;
ssize_t ret;
priv = (FAR struct smartfs_file_s *)filep->f_priv;
DEBUGASSERT(priv);
ret = 0;
if (priv->level1.base.level == 3)
{
if (priv->level1.direntry < g_direntrycount)
{
if (g_direntry[priv->level1.direntry].write)
{
ret = g_direntry[priv->level1.direntry].write(filep,
buffer, buflen);
}
}
}
if (ret > 0)
{
filep->f_pos += ret;
}
return ret;
}
* Name: smartfs_opendir
*
* Description:
* Open a directory for read access
*
****************************************************************************/
static int smartfs_opendir(FAR const char *relpath,
FAR struct fs_dirent_s **dir)
{
FAR struct smartfs_level1_s *level1;
int ret;
finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
DEBUGASSERT(relpath);
* dirent structure.
*/
level1 = (FAR struct smartfs_level1_s *)
fs_heap_malloc(sizeof(struct smartfs_level1_s));
if (!level1)
{
ferr("ERROR: Failed to allocate the level1 directory structure\n");
return -ENOMEM;
}
ret = smartfs_find_dirref(relpath, level1);
if (ret == OK)
{
*dir = (FAR struct fs_dirent_s *)level1;
}
else
{
fs_heap_free(level1);
}
return ret;
}
* Name: smartfs_closedir
*
* Description: Close the directory listing
*
****************************************************************************/
static int smartfs_closedir(FAR struct fs_dirent_s *dir)
{
DEBUGASSERT(dir);
fs_heap_free(dir);
return OK;
}
* Name: smartfs_readdir
*
* Description: Read the next directory entry
*
****************************************************************************/
static int smartfs_readdir(FAR struct fs_dirent_s *dir,
FAR struct dirent *entry)
{
FAR struct smartfs_level1_s *level1;
int ret;
int index;
DEBUGASSERT(dir);
level1 = (FAR struct smartfs_level1_s *)dir;
index = level1->base.index;
if (index >= level1->base.nentries)
{
* error -ENOENT
*/
finfo("Entry %d: End of directory\n", index);
ret = -ENOENT;
}
else
{
DEBUGASSERT(level1->base.level >= 1);
if (level1->base.level == 1)
{
if (!level1->mount)
{
return -ENOENT;
}
entry->d_type = DTYPE_DIRECTORY;
strlcpy(entry->d_name, level1->mount->fs_blkdriver->i_name,
sizeof(entry->d_name));
level1->base.index++;
level1->mount = level1->mount->fs_next;
}
else if (level1->base.level == 2)
{
entry->d_type = g_direntry[level1->base.index].type;
strlcpy(entry->d_name, g_direntry[level1->base.index++].name,
sizeof(entry->d_name));
}
else if (level1->base.level == 3)
{
entry->d_type = g_direntry[level1->base.index].type;
strlcpy(entry->d_name, g_direntry[level1->direntry].name,
sizeof(entry->d_name));
level1->base.index++;
}
* standard f_pos instead of our own private index.
*/
ret = OK;
}
return ret;
}
* Name: smartfs_rewindir
*
* Description: Reset directory read to the first entry
*
****************************************************************************/
static int smartfs_rewinddir(struct fs_dirent_s *dir)
{
FAR struct smartfs_level1_s *priv;
DEBUGASSERT(dir);
priv = (FAR struct smartfs_level1_s *)dir;
priv->base.index = 0;
return OK;
}
* Name: smartfs_stat
*
* Description: Return information about a file or directory
*
****************************************************************************/
static int smartfs_stat(const char *relpath, struct stat *buf)
{
int ret;
struct smartfs_level1_s level1;
* or a directory and set it's permissions.
*/
ret = smartfs_find_dirref(relpath, &level1);
buf->st_mode = S_IROTH | S_IRGRP | S_IRUSR;
if (ret == OK)
{
if (level1.base.level < 3)
{
buf->st_mode |= S_IFDIR;
}
else
{
if (g_direntry[level1.direntry].type == DTYPE_DIRECTORY)
{
buf->st_mode |= S_IFDIR;
}
else
{
buf->st_mode |= S_IFREG;
}
if (g_direntry[level1.direntry].write != NULL)
{
buf->st_mode |= S_IWOTH | S_IWGRP | S_IWUSR;
}
}
}
buf->st_size = 0;
buf->st_blksize = 0;
buf->st_blocks = 0;
return ret;
}
* Name: smartfs_debug_write
*
* Description: Performs the write operation for the "debug" file
*
****************************************************************************/
static ssize_t smartfs_debug_write(FAR struct file *filep,
FAR const char *buffer, size_t buflen)
{
struct mtd_smart_debug_data_s debug_data;
FAR struct smartfs_file_s *priv;
priv = (FAR struct smartfs_file_s *) filep->f_priv;
debug_data.debugcmd = SMART_DEBUG_CMD_SET_DEBUG_LEVEL;
debug_data.debugdata = atoi(buffer);
priv->level1.mount->fs_blkdriver->u.i_bops->ioctl(
priv->level1.mount->fs_blkdriver, BIOC_DEBUGCMD,
(unsigned long) &debug_data);
return buflen;
}
* Name: smartfs_status_read
*
* Description: Performs the read operation for the "status" dir entry.
*
****************************************************************************/
static size_t smartfs_status_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
struct mtd_smart_procfs_data_s procfs_data;
FAR struct smartfs_file_s *priv;
int ret;
size_t len;
int utilization;
priv = (FAR struct smartfs_file_s *) filep->f_priv;
* end of the file (i.e. already read the data.
*/
len = 0;
if (priv->offset == 0)
{
ret = priv->level1.mount->fs_blkdriver->u.i_bops->ioctl(
priv->level1.mount->fs_blkdriver, BIOC_GETPROCFSD,
(unsigned long) &procfs_data);
if (ret == OK)
{
if (procfs_data.blockerases == 0)
{
utilization = 100;
}
else
{
utilization = 100 * (procfs_data.blockerases *
procfs_data.sectorsperblk -
procfs_data.unusedsectors) /
(procfs_data.blockerases *
procfs_data.sectorsperblk);
}
len = snprintf(buffer, buflen,
"Format version: %d\nName Len: %d\n"
"Total Sectors: %d\nSector Size: %d\n"
"Format Sector: %d\nDir Sector: %d\n"
"Free Sectors: %d\nReleased Sectors: %d\n"
"Unused Sectors: %" PRIu32 "\n"
"Block Erases: %" PRIu32 "\n"
"Sectors Per Block: %d\nSector Utilization:%d%%\n"
#ifdef CONFIG_MTD_SMART_WEAR_LEVEL
"Uneven Wear Count: %" PRIu32 "\n"
#endif
,
procfs_data.formatversion, procfs_data.namelen,
procfs_data.totalsectors, procfs_data.sectorsize,
procfs_data.formatsector, procfs_data.dirsector,
procfs_data.freesectors, procfs_data.releasesectors,
procfs_data.unusedsectors, procfs_data.blockerases,
procfs_data.sectorsperblk, utilization
#ifdef CONFIG_MTD_SMART_WEAR_LEVEL
, procfs_data.uneven_wearcount
#endif
);
}
priv->offset = 0xff;
}
return len;
}
* Name: smartfs_mem_read
*
* Description: Performs the read operation for the "mem" dir entry.
*
****************************************************************************/
#ifdef CONFIG_MTD_SMART_ALLOC_DEBUG
static size_t smartfs_mem_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
struct mtd_smart_procfs_data_s procfs_data;
FAR struct smartfs_file_s *priv;
int ret;
uint16_t x;
size_t len;
size_t total;
priv = (FAR struct smartfs_file_s *) filep->f_priv;
* end of the file (i.e. already read the data.
*/
len = 0;
if (priv->offset == 0)
{
ret = priv->level1.mount->fs_blkdriver->u.i_bops->ioctl(
priv->level1.mount->fs_blkdriver, BIOC_GETPROCFSD,
(unsigned long) &procfs_data);
if (ret == OK)
{
total = 0;
len = snprintf(buffer, buflen, "Allocations:\n");
buflen -= len;
for (x = 0; x < procfs_data.alloccount; x++)
{
if (procfs_data.allocs[x].ptr != NULL)
{
len += snprintf(&buffer[len], buflen - len, " %s: %d\n",
procfs_data.allocs[x].name, procfs_data.allocs[x].size);
total += procfs_data.allocs[x].size;
}
}
len += snprintf(&buffer[len], buflen - len,
"\nTotal: %d\n", total);
}
priv->offset = 0xff;
}
return len;
}
#endif
* Name: smartfs_erasemap_read
*
* Description: Performs the read operation for the "erase" dir entry.
*
****************************************************************************/
#ifdef CONFIG_MTD_SMART_SECTOR_ERASE_DEBUG
static size_t smartfs_erasemap_read(FAR struct file *filep,
FAR char *buffer, size_t buflen)
{
struct mtd_smart_procfs_data_s procfs_data;
FAR struct smartfs_file_s *priv;
int ret;
int rows;
int cols;
size_t x;
size_t y;
size_t len;
size_t copylen;
priv = (FAR struct smartfs_file_s *) filep->f_priv;
ret = priv->level1.mount->fs_blkdriver->u.i_bops->ioctl(
priv->level1.mount->fs_blkdriver, BIOC_GETPROCFSD,
(unsigned long) &procfs_data);
if (ret != OK)
{
return 0;
}
* end of the file (i.e. already read the data).
*/
len = 0;
rows = 32;
cols = procfs_data.neraseblocks / rows;
while (rows >= 4 && (cols < 64 || cols > 128))
{
rows >>= 1;
cols = procfs_data.neraseblocks / rows;
}
* account for the \n at the end of each line.
*/
if (priv->offset < procfs_data.neraseblocks + rows)
{
* equal to or greater than the offset, we start sending data
* again. Basically we are starting at the beginning each time
* and only sending where we left off and discarding the rest.
*/
copylen = 0;
for (y = 0; y < rows; y++)
{
for (x = 0; x < cols; x++)
{
if (copylen >= priv->offset)
{
buffer[len++] =
procfs_data.erasecounts[y * cols + x] + 'A';
priv->offset++;
if (len >= buflen)
{
return len;
}
}
copylen++;
}
if (copylen >= priv->offset)
{
buffer[len++] = '\n';
priv->offset++;
if (len >= buflen)
{
return len;
}
}
if (copylen >= priv->offset)
{
buffer[len++] = '\0';
priv->offset++;
}
copylen++;
}
}
return len;
}
#endif
* Public Functions
****************************************************************************/
#endif