This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch nandfs
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/nandfs by this push:
     new 0b7ed547db fs/mnemofs: Adds mnemofs and mnemofs journal Adds basic 
setup for mnemofs NAND Flash file system, its journal and some required 
utilities.
0b7ed547db is described below

commit 0b7ed547dbe4f7a7d0a4f9eadf917bbc7257d1ae
Author: Saurav Pal <[email protected]>
AuthorDate: Sat May 25 04:52:11 2024 +0000

    fs/mnemofs: Adds mnemofs and mnemofs journal
    Adds basic setup for mnemofs NAND Flash file system, its journal and some 
required utilities.
    
    Signed-off-by: Saurav Pal <[email protected]>
---
 fs/Kconfig                    |   1 +
 fs/Makefile                   |   1 +
 fs/mnemofs/CMakeLists.txt     |  24 ++
 fs/mnemofs/Kconfig            |  20 ++
 fs/mnemofs/Make.defs          |  35 ++
 fs/mnemofs/mnemofs.c          | 175 ++++++++++
 fs/mnemofs/mnemofs.h          | 548 +++++++++++++++++++++++++++++
 fs/mnemofs/mnemofs_blkalloc.c |  63 ++++
 fs/mnemofs/mnemofs_journal.c  | 781 ++++++++++++++++++++++++++++++++++++++++++
 fs/mnemofs/mnemofs_rw.c       |  78 +++++
 fs/mnemofs/mnemofs_util.c     | 128 +++++++
 fs/mount/fs_mount.c           |  10 +-
 12 files changed, 1862 insertions(+), 2 deletions(-)

diff --git a/fs/Kconfig b/fs/Kconfig
index 6115ed19c7..17f5f52749 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -134,3 +134,4 @@ source "fs/userfs/Kconfig"
 source "fs/hostfs/Kconfig"
 source "fs/rpmsgfs/Kconfig"
 source "fs/zipfs/Kconfig"
+source "fs/mnemofs/Kconfig"
\ No newline at end of file
diff --git a/fs/Makefile b/fs/Makefile
index 2f62d7bea8..db747317bd 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -57,6 +57,7 @@ include hostfs/Make.defs
 include littlefs/Make.defs
 include rpmsgfs/Make.defs
 include zipfs/Make.defs
+include mnemofs/Make.defs
 
 endif
 
diff --git a/fs/mnemofs/CMakeLists.txt b/fs/mnemofs/CMakeLists.txt
new file mode 100644
index 0000000000..9a0ec87971
--- /dev/null
+++ b/fs/mnemofs/CMakeLists.txt
@@ -0,0 +1,24 @@
+# 
##############################################################################
+# fs/mnemofs/CMakeLists.txt
+#
+# 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.
+#
+# 
##############################################################################
+
+if(CONFIG_FS_MNEMOFS)
+  target_sources(fs PRIVATE mnemofs_blkalloc.c mnemofs_journal.c mnemofs_rw.c
+                            mnemofs_util.c mnemofs.c)
+endif()
diff --git a/fs/mnemofs/Kconfig b/fs/mnemofs/Kconfig
new file mode 100644
index 0000000000..023dd81f32
--- /dev/null
+++ b/fs/mnemofs/Kconfig
@@ -0,0 +1,20 @@
+config FS_MNEMOFS
+       bool "MNEMOFS NAND Flash File System"
+       default n
+       depends on !DISABLE_MOUNTPOINT
+       ---help---
+               Build the mnemofs NAND flash file system.
+
+if FS_MNEMOFS
+
+config MNEMOFS_JOURNAL_NBLKS
+       int "MNEMOFS Journal Block Count"
+       default 20
+       range 12 255
+       ---help---
+               Number of blocks that mnemofs will use for the journal. 
Specifying
+               this will only work on formatting a NAND flash using mnemofs. 
If the
+               device is already formatted, the on-flash journal block count 
will
+               be considered instead.
+
+endif # FS_MNEMOFS
\ No newline at end of file
diff --git a/fs/mnemofs/Make.defs b/fs/mnemofs/Make.defs
new file mode 100644
index 0000000000..464633245f
--- /dev/null
+++ b/fs/mnemofs/Make.defs
@@ -0,0 +1,35 @@
+############################################################################
+# fs/mnemofs/Make.defs
+#
+# 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.
+#
+############################################################################
+
+ifeq ($(CONFIG_FS_MNEMOFS),y)
+
+# Add the mnemofs C files to the build
+
+CSRCS += mnemofs_blkalloc.c
+CSRCS += mnemofs_journal.c
+CSRCS += mnemofs_rw.c
+CSRCS += mnemofs_util.c
+CSRCS += mnemofs.c
+
+# Add the mnemofs directory to the build
+
+DEPPATH += --dep-path mnemofs
+VPATH += :mnemofs
+endif
\ No newline at end of file
diff --git a/fs/mnemofs/mnemofs.c b/fs/mnemofs/mnemofs.c
new file mode 100644
index 0000000000..1adfb5df11
--- /dev/null
+++ b/fs/mnemofs/mnemofs.c
@@ -0,0 +1,175 @@
+/****************************************************************************
+ * fs/mnemofs/mnemofs.c
+ * mnemofs: Filesystem for NAND Flash storage devices.
+ *
+ * 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/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <math.h>
+#include <stdio.h>
+
+#include "mnemofs.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int mnemofs_bind(FAR struct inode *driver, FAR const void *data,
+                        FAR void** handle);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: g_mnemofs_operations
+ *
+ * Description:
+ *  The global list of VFS methods implemented by mnemofs
+ *
+ ****************************************************************************/
+
+const struct mountpt_operations g_mnemofs_operations =
+{
+  NULL,         /* open */
+  NULL,         /* close */
+  NULL,         /* read */
+  NULL,         /* write */
+  NULL,         /* seek */
+  NULL,         /* ioctl */
+  NULL,         /* mmap */
+  NULL,         /* truncate */
+  NULL,         /* poll */
+
+  NULL,         /* sync */
+  NULL,         /* dup */
+  NULL,         /* fstat */
+  NULL,         /* fchstat */
+
+  NULL,         /* opendir */
+  NULL,         /* closedir */
+  NULL,         /* readdir */
+  NULL,         /* rewinddir */
+
+  mnemofs_bind, /* bind */
+  NULL,         /* unbind */
+  NULL,         /* statfs */
+
+  NULL,         /* unlink */
+  NULL,         /* mkdir */
+  NULL,         /* rmdir */
+  NULL,         /* rename */
+  NULL,         /* stat */
+  NULL          /* chstat */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: mnemofs_bind
+ *
+ * Description:
+ *   Mounts the file system.
+ *
+ * Input Parameters:
+ *   driver - MTD driver
+ *   data   - Mount options
+ *   handle - To be updated with file system information.
+ *
+ * Returned Value:
+ *   0   - OK
+ *   < 0 - Error
+ *
+ ****************************************************************************/
+
+static int mnemofs_bind(FAR struct inode *driver, FAR const void *data,
+                        FAR void** handle)
+{
+  int ret = OK;
+  struct mfs_sb_info *sb = NULL;
+
+  sb = kmm_zalloc(sizeof(*sb));
+  if (!sb)
+    {
+      ret = -ENOMEM;
+      goto errout;
+    }
+
+  if (driver && INODE_IS_MTD(driver))
+    {
+      ret = MTD_IOCTL(driver->u.i_mtd, MTDIOC_GEOMETRY,
+                      (unsigned long) &sb->geo);
+    }
+  else
+    {
+      ret = -ENODEV;
+      goto errout_with_sb;
+    }
+
+  nxmutex_init(&sb->fs_lock);
+
+  sb->drv           = driver;
+  sb->pg_sz         = sb->geo.blocksize;
+  sb->blk_sz        = sb->geo.erasesize;
+  sb->n_blks        = sb->geo.neraseblocks;
+  sb->pg_in_blk     = sb->blk_sz / sb->pg_sz;
+  sb->j_nblks       = CONFIG_MNEMOFS_JOURNAL_NBLKS;
+  sb->log_blk_sz    = log2(sb->blk_sz);
+  sb->log_pg_sz     = log2(sb->pg_sz);
+  sb->log_pg_in_blk = log2(sb->pg_in_blk);
+  sb->master_node   = 0; /* Placeholder code for testing. */
+
+  ret = mfs_jrnl_fmt(sb);
+  if (ret != OK)
+    {
+      goto errout_with_sb;
+    }
+
+  handle = (FAR void *)sb;
+
+  return ret;
+
+errout_with_sb:
+  kmm_free(sb);
+
+errout:
+  return ret;
+}
diff --git a/fs/mnemofs/mnemofs.h b/fs/mnemofs/mnemofs.h
new file mode 100644
index 0000000000..dc1881cfc9
--- /dev/null
+++ b/fs/mnemofs/mnemofs.h
@@ -0,0 +1,548 @@
+/****************************************************************************
+ * fs/mnemofs/mnemofs.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __FS_MNEMOFS_MNEMOFS_H
+#define __FS_MNEMOFS_MNEMOFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/list.h>
+#include <nuttx/mtd/mtd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MFS_JRNL_MAGIC  "-mfs!j!-"
+
+#define MFS_BLK2PG(sb, blk)     ((blk) << (sb)->log_pg_in_blk)
+#define MFS_PG2BLK(sb, pg)      ((pg) >> (sb)->log_pg_in_blk)
+#define MFS_PG2BLKPGOFF(sb, pg) ((pg) % (1 << (sb)->log_pg_in_blk))
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+typedef uint32_t  mfs_t;
+
+struct mfs_ctz_store_s
+{
+  mfs_t pg_e;
+  mfs_t idx_e;
+};
+
+struct mfs_jrnl_info
+{
+  struct list_node            list;
+  mfs_t                       depth;
+  FAR struct mfs_ctz_store_s *path;
+  struct mfs_ctz_store_s      new;
+};
+
+struct mfs_jrnl_state
+{
+  uint8_t     n_blks; /* Does not consider master blocks. */
+  FAR mfs_t   *idxarr;
+  mfs_t       s_off; /* Writeable area start offset */
+  mfs_t       w_off; /* Read pointer */
+  mfs_t       r_off; /* Write pointer */
+};
+
+struct mfs_sb_info
+{
+  mutex_t               fs_lock;
+  FAR struct inode      *drv;
+  struct mtd_geometry_s geo;
+  mfs_t                 pg_sz;         /* In bytes */
+  mfs_t                 blk_sz;        /* In bytes */
+  uint8_t               log_blk_sz;
+  mfs_t                 n_blks;
+  uint8_t               log_pg_sz;
+  uint16_t              pg_in_blk;
+  uint8_t               log_pg_in_blk;
+  uint8_t               j_nblks;
+  mfs_t                 master_node;
+  struct mfs_jrnl_state j_state; /* Journal State */
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: mfs_blkremsz
+ *
+ * Description:
+ *   Given a page and a page offset, it returns the bytes left in the entire
+ *   block after the offset location.
+ *
+ * Input Parameters:
+ *   sb    - Superblock instance of the device.
+ *   pg    - Page number.
+ *   pgoff - Page offset.
+ *
+ * Returned Value:
+ *   Bytes left in the block.
+ *
+ ****************************************************************************/
+
+static mfs_t inline mfs_blkremsz(FAR const struct mfs_sb_info * const sb,
+                                  mfs_t pg, mfs_t pgoff)
+{
+  return sb->blk_sz - (MFS_PG2BLKPGOFF(sb, pg) * sb->pg_sz + pgoff);
+}
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/* mnemofs_journal.c */
+
+/****************************************************************************
+ * Name: mfs_jrnl_fmt
+ *
+ * Description:
+ *   Formats a journal into a NAND flash device.
+ *
+ * Input Parameters:
+ *   sb - Superblock instance of the device.
+ *
+ * Returned Value:
+ *   0        - OK
+ *   -ENOMEM  - No memory left
+ *
+ * Assumptions/Limitations:
+ *   This is used as part of the whole format process for the device.
+ *
+ ****************************************************************************/
+
+int mfs_jrnl_fmt(FAR struct mfs_sb_info * const sb);
+
+/****************************************************************************
+ * Name: mfs_jrnl_init
+ *
+ * Description:
+ *   Reads a journal from the NAND device and initializes it's in-memory
+ *   metadata.
+ *
+ * Input Parameters:
+ *   sb     - Superblock instance of the device.
+ *   blk    - The block number of the very first block of the journal.
+ *   m_node - Populates it with the most recently written master node.
+ *
+ * Returned Value:
+ *   0        - OK
+ *   -EINVAL  - Invalid first block
+ *   -ENOMEM  - No memory left
+ *
+ * Assumptions/Limitations:
+ *   This is used as part of the whole format process for the device.
+ *
+ ****************************************************************************/
+
+int mfs_jrnl_init(FAR struct mfs_sb_info * const sb, mfs_t blk,
+                  mfs_t *m_node);
+
+/****************************************************************************
+ * Name: mfs_jrnl_init
+ *
+ * Description:
+ *   Reset the state of the journal's read pointer.
+ *
+ * Input Parameters:
+ *   sb - Superblock instance of the device.
+ *
+ * Assumptions/Limitations:
+ *   This assumes all read traversals over the journal is done in from start
+ *   to the end, and this is called at the start of every such traversal.
+ *
+ ****************************************************************************/
+
+void mfs_jrnl_statereset(FAR struct mfs_sb_info * const sb);
+
+/****************************************************************************
+ * Name: mfs_jrnl_radv
+ *
+ * Description:
+ *   Read and advance the read pointer of the journal by one log.
+ *
+ * Input Parameters:
+ *   sb   - Superblock instance of the device.
+ *   info - If not null, it will be populated by the log that is read.
+ *
+ * Returned Value:
+ *   0        - OK
+ *   -ENOMEM  - No memory left
+ *   -EINVAL  - Invalid current state.
+ *
+ * Assumptions/Limitations:
+ *   This assumes all read traversals over the journal is done in from start
+ *   to the end, and this is called at the start of every such traversal.
+ *
+ ****************************************************************************/
+
+int mfs_jrnl_radv(FAR struct mfs_sb_info * const sb,
+                  FAR struct mfs_jrnl_info * info);
+
+/****************************************************************************
+ * Name: mfs_jrnl_wadv
+ *
+ * Description:
+ *   Write log at current journal state and advance the write pointer of the
+ *   journal by one log.
+ *
+ * Input Parameters:
+ *   sb   - Superblock instance of the device.
+ *   info - Log to be written. Needs to be non-NULL.
+ *
+ * Returned Value:
+ *   0        - OK
+ *   -ENOMEM  - No memory left
+ *   -EINVAL  - Invalid current state.
+ *
+ ****************************************************************************/
+
+int mfs_jrnl_wadv(FAR struct mfs_sb_info * const sb,
+                  FAR const struct mfs_jrnl_info * const info);
+
+/****************************************************************************
+ * Name: mfs_jrnl_nwadv
+ *
+ * Description:
+ *   Write n logs at current journal state and advance the write pointer of
+ *   the journal by n logs.
+ *
+ * Input Parameters:
+ *   sb   - Superblock instance of the device.
+ *   list - List of logs to be written. The value of n is the length of this
+ *          list.
+ *
+ * Returned Value:
+ *   0        - OK
+ *   -ENOMEM  - No memory left
+ *   -EINVAL  - Invalid current state.
+ *
+ ****************************************************************************/
+
+int mfs_jrnl_nwadv(FAR struct mfs_sb_info * const sb,
+                  FAR struct list_node list);
+
+/****************************************************************************
+ * Name: mfs_jrml_updatectz
+ *
+ * Description:
+ *   Given a CTZ list from on-flash location, this traverses through the
+ *   entire journal and finally provided the updated position of the list,
+ *   which is yet to be commited (hence in the journal).
+ *
+ * Input Parameters:
+ *   sb    - Superblock instance of the device.
+ *   path  - CTZ array representation of the path.
+ *   depth - The depth of the fs object in the path.
+ *
+ ****************************************************************************/
+
+void mfs_jrml_updatectz(FAR struct mfs_sb_info * const sb,
+                        FAR struct mfs_ctz_store_s * const path,
+                        const mfs_t depth);
+
+/* mnemofs_blkalloc.c */
+
+/****************************************************************************
+ * Name: mfs_get_blk
+ *
+ * Description:
+ *   Returns a block location on request.
+ *
+ * Input Parameters:
+ *   sb - Superblock instance of the device.
+ *
+ * Returned Value:
+ *   0        - OK
+ *   -ENOMEM  - No memory left
+ *
+ * Assumptions/Limitations:
+ *   This assumes that the entire block is as good as being completely
+ *   written to by the caller. No pages inside this block will be allocated
+ *   to anybody else till this block is not erased.
+ *
+ ****************************************************************************/
+
+mfs_t mfs_get_blk(FAR struct mfs_sb_info * const sb);
+
+/* mnemofs_rw.c */
+
+/****************************************************************************
+ * Name: mfs_write_data
+ *
+ * Description:
+ *   Writes an array starting from a particular page and its page offset.
+ *
+ * Input Parameters:
+ *   sb       - Superblock instance of the device.
+ *   data     - Array.
+ *   datalen  - Length of the array.
+ *   pg       - Page number to start writing.
+ *   pgoff    - Offset inside page to start writing from.
+ *
+ * Returned Value:
+ *   Number of bytes written.
+ *
+ * Assumptions/Limitations:
+ *   If the remaining size of the page is not enough to hold the entire data,
+ *   it will write it in the next page, and so on.
+ *
+ ****************************************************************************/
+
+ssize_t mfs_write_data(FAR const struct mfs_sb_info * const sb,
+                      char *data, uint64_t datalen, uint32_t page,
+                      uint8_t pgoff);
+
+/****************************************************************************
+ * Name: mfs_read_data
+ *
+ * Description:
+ *   Reads an array starting from a particular page and its page offset.
+ *
+ * Input Parameters:
+ *   sb       - Superblock instance of the device.
+ *   data     - Array.
+ *   datalen  - Length of the array.
+ *   pg       - Page number to start reading.
+ *   pgoff    - Offset inside page to start reading from.
+ *
+ * Returned Value:
+ *   Number of bytes read.
+ *
+ * Assumptions/Limitations:
+ *   If the remaining size of the page is not enough to read the entire data,
+ *   it will read from the next page as well, and so on.
+ *
+ ****************************************************************************/
+
+ssize_t mfs_read_data(FAR const struct mfs_sb_info * const sb,
+                      char *data, uint64_t datalen, uint32_t pg,
+                      uint8_t pgoff);
+
+/****************************************************************************
+ * Name: mfs_read_page
+ *
+ * Description:
+ *   Reads an array starting from a particular page and its page offset to
+ *   either the end of the array or the end of the page, whichever occurs
+ *   first.
+ *
+ * Input Parameters:
+ *   sb       - Superblock instance of the device.
+ *   data     - Array.
+ *   data     - Length of the array..
+ *   pg       - Page number to start reading.
+ *   pgoff    - Offset inside page to start reading from.
+ *
+ * Returned Value:
+ *   Number of bytes read.
+ *
+ ****************************************************************************/
+
+ssize_t mfs_read_page(FAR const struct mfs_sb_info * const sb,
+                      char *data, uint64_t datalen, uint32_t pg,
+                      uint8_t pgoff);
+
+/* mnemofs_util.c */
+
+/****************************************************************************
+ * Name: mfs_arrhash
+ *
+ * Description:
+ *   Returns an 8-bit hash of an entire array of data.
+ *
+ * Input Parameters:
+ *   arr - Data array.
+ *   len - Length of the array.
+ *
+ * Returned Value:
+ *   16-bit hash of the array.
+ *
+ ****************************************************************************/
+
+char mfs_arrhash(FAR const char *arr, ssize_t len);
+
+/****************************************************************************
+ * Name: mfs_ser_8
+ *
+ * Description:
+ *   Serialize a 8 bit type into output.
+ *
+ * Input Parameters:
+ *   n   - 8 bit to serialize
+ *   out - Output array where to serialize.
+ *
+ * Returned Value:
+ *   Pointer to byte after the end of serialized value.
+ *
+ ****************************************************************************/
+
+char *mfs_ser_8(const char n, char * const out);
+
+/****************************************************************************
+ * Name: mfs_deser_8
+ *
+ * Description:
+ *   Deserialize a 8 bit type from input.
+ *
+ * Input Parameters:
+ *   in - Input array from where to deserialize.
+ *   n  - 8 bit to deserialize
+ *
+ * Returned Value:
+ *   Pointer to byte after the end of serialized value.
+ *
+ ****************************************************************************/
+
+const char *mfs_deser_8(const char * const in, char *n);
+
+/****************************************************************************
+ * Name: mfs_ser_str
+ *
+ * Description:
+ *   Serialize a string into output.
+ *
+ * Input Parameters:
+ *   str - String to serialize
+ *   len - Length of string
+ *   out - Output array where to serialize.
+ *
+ * Returned Value:
+ *   Pointer to byte after the end of serialized value.
+ *
+ ****************************************************************************/
+
+char *mfs_ser_str(const char * const str, const mfs_t len, char * const out);
+
+/****************************************************************************
+ * Name: mfs_deser_str
+ *
+ * Description:
+ *   Deserialize a string from intput.
+ *
+ * Input Parameters:
+ *   in  - Intput array from where to deserialize.
+ *   str - String to deserialize
+ *   len - Length of string
+ *
+ * Returned Value:
+ *   Pointer to byte after the end of serialized value.
+ *
+ ****************************************************************************/
+
+const char *mfs_deser_str(const char * const in, char * const str,
+                          const mfs_t len);
+
+/****************************************************************************
+ * Name: mfs_ser_mfs
+ *
+ * Description:
+ *   Serialize a mfs_t type into output.
+ *
+ * Input Parameters:
+ *   n   - mfs_t to serialize
+ *   out - Output array where to serialize.
+ *
+ * Returned Value:
+ *   Pointer to byte after the end of serialized value.
+ *
+ ****************************************************************************/
+
+char *mfs_ser_mfs(const mfs_t n, char * const out);
+
+/****************************************************************************
+ * Name: mfs_deser_mfs
+ *
+ * Description:
+ *   Deserialize a mfs_t type from input..
+ *
+ * Input Parameters:
+ *   in - Input array from where to deserialize.
+ *   n  - mfs_t to deserialize
+ *
+ * Returned Value:
+ *   Pointer to byte after the end of serialized value.
+ *
+ ****************************************************************************/
+
+const char *mfs_deser_mfs(const char * const in, mfs_t * const n);
+
+/****************************************************************************
+ * Name: mfs_ser_ctz_store
+ *
+ * Description:
+ *   Serialize a mfs_ctz_store_s type into output.
+ *
+ * Input Parameters:
+ *   x   - mfs_ctz_store_s to serialize
+ *   out - Output array where to serialize.
+ *
+ * Returned Value:
+ *   Pointer to byte after the end of serialized value.
+ *
+ ****************************************************************************/
+
+char *mfs_ser_ctz_store(const struct mfs_ctz_store_s * const x,
+                        char * const out);
+
+/****************************************************************************
+ * Name: mfs_deser_ctz_store
+ *
+ * Description:
+ *   Deserialize a mfs_ctz_store_s type into output.
+ *
+ * Input Parameters:
+ *   in - Input array from where to deserialize.
+ *   x  - mfs_ctz_store_s to deserialize
+ *
+ * Returned Value:
+ *   Pointer to byte after the end of serialized value.
+ *
+ ****************************************************************************/
+
+const char *mfs_deser_ctz_store(const char * const in,
+                                struct mfs_ctz_store_s * const x);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __FS_MNEMOFS_MNEMOFS_H */
\ No newline at end of file
diff --git a/fs/mnemofs/mnemofs_blkalloc.c b/fs/mnemofs/mnemofs_blkalloc.c
new file mode 100644
index 0000000000..b769de5255
--- /dev/null
+++ b/fs/mnemofs/mnemofs_blkalloc.c
@@ -0,0 +1,63 @@
+/****************************************************************************
+ * fs/mnemofs/mnemofs_blkalloc.c
+ * Block Allocator for mnemofs
+ *
+ * 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 <stdlib.h>
+
+#include "mnemofs.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+mfs_t mfs_get_blk(FAR struct mfs_sb_info * const sb)
+{
+  /* TODO */
+
+  return 0;
+}
diff --git a/fs/mnemofs/mnemofs_journal.c b/fs/mnemofs/mnemofs_journal.c
new file mode 100644
index 0000000000..0a981a426a
--- /dev/null
+++ b/fs/mnemofs/mnemofs_journal.c
@@ -0,0 +1,781 @@
+/****************************************************************************
+ * fs/mnemofs/mnemofs_journal.c
+ * Journal of mnemofs.
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/* Journal on-flash has a structure like:
+ * struct mfs_jrnl_node {
+ *   union {
+ *     struct {
+ *       char magic[7];
+ *       uint8_t n_blocks;
+ *       char index_array[ceil((n_blocks * sizeof(mfs_t))/pg_sz)],
+ *       writeable_area[pg_sz - (ceil((n_blocks * sizeof(mfs_t))/pg_sz) + 1)]
+ *     }, // First block.
+ *     char writeable_area[pg_sz];
+ *     char masterblocks[pg_sz]; // Last two blocks.
+ *   }
+ * };
+ *
+ */
+
+/* TODO: On-flash offsets for the journal structure (index array, n_blks,
+ * etc.)
+ */
+
+/* Journal will have `sb->jrnl_nblks + 2` blocks.
+ *
+ * The first block will start with a 7 byte-long magic number that identifies
+ * a journal of mnemofs.
+ *
+ * Then another byte will be used to write the number of blocks in the
+ * journal.
+ *
+ * This is done so that, if the journal is found during mount, it will be
+ * quick to point to the master node as well.
+ * NOTE: This count will NOT include the 2 master blocks.
+ *
+ * Then the next `(sb->jrnl_nblks + 2) * sizeof(mfs_t)` bytes will be used to
+ * store an array of block numbers. These block numbers are of each of the
+ * blocks of the journal. This is called the journal index array. If the
+ * array extends to be partially occupying a page, then the rest of the page
+ * is left unused.
+ *
+ * Then all the rest of the pages that are part of the journal are writeable
+ * for the logs except the master blocks.
+ * Advantages of journal index array over a singly linked list:
+ *   - Multiple block numbers can be stored together. In singly linked list,
+ *     there was only one block number in the last page of the block, which
+ *     wasted a lot of space.
+ *   - Page caching can cache the array as it is used often, and then
+ *     traversal would be quicker.
+ *   - While mounting, if journal is reached first during scan of the device,
+ *     to get the root node, all that is required is to scan the second byte
+ *     to get the array size, and then jump to the array, get the master
+ *     block locations, find the most recent master block and then we have
+ *     the master node. This removes the problem of reaching the master node
+ *     through linked list traversal, which would be slower due to less
+ *     probability of caching.
+ *   - The entire journal moves together, so there is no reason for linked
+ *     list except to not deal with the contrainst of having contiguous block
+ *     problems like uneven wear which might lead to bad blocks right inside
+ *     the journal among other things, etc.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <endian.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/list.h>
+#include <sys/param.h>
+
+#include "mnemofs.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct mfs_jrnl_log
+{
+  mfs_t size;
+  mfs_t depth;
+  struct mfs_ctz_store_s new;
+  struct mfs_ctz_store_s *path; /* path has depth no. of elements */
+  char hash;
+  char magic[sizeof(MFS_JRNL_MAGIC)];
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* NOTE: Journal assumes no page is less than 16B in size for its
+ *        initialization.
+ *        And assumes all page sizes are multiples of 4B.
+ */
+
+static int jrnl_off2details(FAR const struct mfs_sb_info * const sb,
+                            mfs_t off, mfs_t *blk, mfs_t *pg, mfs_t *pgoff);
+static mfs_t inline mfs_blkremsz(FAR const struct mfs_sb_info * const sb,
+                                  mfs_t pg, mfs_t pgoff);
+
+static char * ser_jrnl_log(const struct mfs_jrnl_log * const log,
+                          char * out);
+static const char * deser_jrnl_log(const char * const in,
+                                  struct mfs_jrnl_log * const log);
+static uint8_t jrnl_log_hash(const struct mfs_jrnl_log * const log);
+static mfs_t jrnl_info2logsz(const struct mfs_jrnl_info * const info);
+struct mfs_jrnl_log jrnl_info2log(const struct mfs_jrnl_info *info);
+
+struct mfs_jrnl_info jrnl_log2info(const struct mfs_jrnl_log *log);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ser_jrnl_log
+ *
+ * Description:
+ *   Serialize a log to store it on flash.
+ *
+ * Input Parameters:
+ *   log - Journal log.
+ *   out - Output buffer to serialize to.
+ *
+ * Returned Value:
+ *   Pointer to byte after the end of serialized value.
+ *
+ ****************************************************************************/
+
+static char * ser_jrnl_log(const struct mfs_jrnl_log * const log, char * out)
+{
+  mfs_t i = 0;
+
+  out = mfs_ser_mfs(log->size, out);
+  out = mfs_ser_mfs(log->depth, out);
+  out = mfs_ser_ctz_store(&log->new, out);
+  for (i = 0; i < log->depth; i++)
+    {
+      out = mfs_ser_ctz_store(&log->path[i], out);
+    }
+
+  out = mfs_ser_8(log->hash, out);
+  out = mfs_ser_str(log->magic, sizeof(MFS_JRNL_MAGIC), out);
+  return out;
+}
+
+/****************************************************************************
+ * Name: deser_jrnl_log
+ *
+ * Description:
+ *   Serialize a log to store it on flash.
+ *
+ * Input Parameters:
+ *   log - Journal log.
+ *   out - Output buffer to serialize to.
+ *
+ * Returned Value:
+ *   Pointer to byte after the end of serialized value.
+ *
+ * Assumptions/Limitations:
+ *   Does not check checksum (hash) match.
+ *
+ ****************************************************************************/
+
+static const char * deser_jrnl_log(const char * const in,
+                                  struct mfs_jrnl_log * const log)
+{
+  mfs_t idx = 0;
+  const char * i = in;
+
+  i = mfs_deser_mfs(i, &log->size);
+  i = mfs_deser_mfs(i, &log->depth);
+  i = mfs_deser_ctz_store(i, &log->new);
+
+  for (idx = 0; idx < log->depth; idx++)
+    {
+      i = mfs_deser_ctz_store(i, &log->path[idx]);
+    }
+
+  i = mfs_deser_8(i, &log->hash);
+  i = mfs_deser_str(i, log->magic, sizeof(MFS_JRNL_MAGIC));
+  return i;
+}
+
+/****************************************************************************
+ * Name: jrnl_log_hash
+ *
+ * Description:
+ *   Get hash of the members of log representation that come before hash.
+ *   Calculation of hash of a log is done by assuming the hash field will
+ *   be 0. It is also calculated by XOR of hash of all members in it. For
+ *   array members, the hash is computed as the XOR of individual elements
+ *   in it.
+ *
+ * Input Parameters:
+ *   log - Journal log.
+ *
+ * Returned Value:
+ *   Hash of the log.
+ *
+ ****************************************************************************/
+
+static uint8_t jrnl_log_hash(const struct mfs_jrnl_log * const log)
+{
+  int i = 0;
+  uint8_t hash = 0;
+
+  hash ^= mfs_arrhash((char *) &log->size, sizeof(log->size));
+  hash ^= mfs_arrhash((char *) &log->depth, sizeof(log->depth));
+  hash ^= mfs_arrhash((char *) &log->new, sizeof(log->new));
+
+  for (i = 0; i < log->depth; i++)
+    {
+      hash ^= mfs_arrhash((char *) &log->path[i], sizeof(log->path[i]));
+    }
+
+  hash ^= mfs_arrhash((char *) &log->magic, sizeof(MFS_JRNL_MAGIC));
+  return hash;
+}
+
+/****************************************************************************
+ * Name: jrnl_info2logsz
+ *
+ * Description:
+ *   Get total size of log equivalent from a jouranl info.
+ *
+ * Input Parameters:
+ *   info - Journal info from memory.
+ *
+ * Returned Value:
+ *   Size of the log representation.
+ *
+ ****************************************************************************/
+
+static mfs_t jrnl_info2logsz(const struct mfs_jrnl_info * const info)
+{
+  /* TODO */
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: jrnl_info2log
+ *
+ * Description:
+ *   Get a log equivalent from a jouranl info.
+ *
+ * Input Parameters:
+ *   info - Journal info from memory.
+ *
+ * Returned Value:
+ *   The log representation.
+ *
+ ****************************************************************************/
+
+struct mfs_jrnl_log jrnl_info2log(const struct mfs_jrnl_info *info)
+{
+  struct mfs_jrnl_log log;
+  log.size  = jrnl_info2logsz(info);
+  log.depth = info->depth;
+  log.new   = info->new;
+  log.path  = info->path;
+  log.hash  = jrnl_log_hash(&log);
+  memcpy(log.magic, MFS_JRNL_MAGIC, sizeof(MFS_JRNL_MAGIC));
+  return log;
+}
+
+/****************************************************************************
+ * Name: jrnl_log2info
+ *
+ * Description:
+ *   Get an info equivalent from a retrieved jouranl log.
+ *
+ * Input Parameters:
+ *   log - Retrieved journal log from the flash.
+ *
+ * Returned Value:
+ *   The info representation.
+ *
+ ****************************************************************************/
+
+struct mfs_jrnl_info jrnl_log2info(const struct mfs_jrnl_log *log)
+{
+  struct mfs_jrnl_info info;
+
+  info.depth = log->depth;
+  info.new = log->new;
+  info.path = log->path;
+  list_initialize(&info.list);
+  return info;
+}
+
+/****************************************************************************
+ * Name: jrnl_off2details
+ *
+ * Description:
+ *   Given an offset in the journal (from the very start), it will provide
+ *   the on-flash details of the offset.
+ *
+ * Input Parameters:
+ *   sb    - Superblock instance of the device.
+ *   off   - Journal offset.
+ *   blk   - Block number of the on-flash location of the offset.
+ *   pg    - Page number of the on-flash location of the offset.
+ *   pgoff - Page offset in the page of the on-flash location of the offset.
+ *
+ * Returned Value:
+ *   0        - OK
+ *   -EINVAL  - Out of bounds offset.
+ *
+ ****************************************************************************/
+
+static int jrnl_off2details(FAR const struct mfs_sb_info * const sb,
+                            mfs_t off, mfs_t *blk, mfs_t *pg, mfs_t *pgoff)
+{
+  mfs_t tmp;
+
+  if (sb->blk_sz * sb->j_nblks <= off)
+    {
+      return -EINVAL;
+    }
+
+  tmp     = off / sb->blk_sz;
+  *blk    = sb->j_state.idxarr[tmp];
+  tmp     = (off % sb->blk_sz) / sb->pg_sz;
+  *pg     = MFS_BLK2PG(sb, *blk) + tmp;
+  *pgoff  = off % sb->pg_sz;
+  return OK;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/* Mount */
+
+int mfs_jrnl_fmt(FAR struct mfs_sb_info * const sb)
+{
+  int ret = OK;
+  int i;
+  mfs_t tmp;
+  mfs_t wr_s_pg;                      /* Start page of writeable area */
+  mfs_t *idxarr        = NULL;
+  char *buf            = NULL;
+  uint8_t idxarr_idx   = 0;
+  const uint8_t n_blks = sb->j_nblks; /* Not counting master blocks. */
+  const int idxarr_sz  = (n_blks + 2) * sizeof(mfs_t);
+  const int buf_sz     = idxarr_sz + 8;
+
+  idxarr = kmm_zalloc(idxarr_sz);
+  if (!idxarr)
+    {
+      ret = -ENOMEM;
+      goto errout;
+    }
+
+  buf    = kmm_zalloc(buf_sz);
+  if (!buf)
+    {
+      ret = -ENOMEM;
+      goto errout_with_idxarr;
+    }
+
+  nxmutex_lock(&sb->fs_lock);
+  for (idxarr_idx = 0; idxarr_idx < n_blks + 2; idxarr_idx++)
+    {
+      tmp = mfs_get_blk(sb);
+      if (tmp == 0)
+        {
+          ret = -ENOMEM;
+          goto errout_with_lock;
+        }
+
+      idxarr[idxarr_idx] = tmp;
+    }
+
+  memcpy(buf, MFS_JRNL_MAGIC, 7);
+  memcpy(buf + 7, &idxarr_idx, 1);
+  memcpy(buf + 8, idxarr, idxarr_sz);
+
+  /* Handles the case where the block size may be smaller than the buffer. */
+
+  if (predict_true(sb->blk_sz >= buf_sz))
+    {
+      mfs_write_data(sb, buf, buf_sz, MFS_BLK2PG(sb, idxarr[0]), 0);
+      wr_s_pg = MFS_BLK2PG(sb, idxarr[0]) +
+                ((buf_sz + (sb->pg_sz - 1)) / sb->pg_sz); /* ceil */
+    }
+  else
+    {
+      /* Split between journal blocks if the array is bigger than a block. */
+
+      tmp         = 0;
+      i           = 0;
+      wr_s_pg     = MFS_BLK2PG(sb, idxarr[i]);
+      while (i < n_blks)
+        {
+          if (predict_true(tmp + sb->blk_sz < buf_sz))
+            {
+              tmp += mfs_write_data(sb, buf + tmp, sb->blk_sz, wr_s_pg++, 0);
+            }
+          else
+            {
+              tmp += mfs_write_data(sb, buf + tmp, buf_sz - tmp,
+                                    wr_s_pg++, 0);
+              break;
+            }
+
+          if (wr_s_pg - idxarr[i] == sb->pg_in_blk)
+            {
+              i++;
+              wr_s_pg = MFS_BLK2PG(sb, idxarr[i]);
+            }
+        }
+
+      if (wr_s_pg - idxarr[i] == sb->pg_in_blk)
+        {
+          i++;
+          wr_s_pg = MFS_BLK2PG(sb, idxarr[i]);
+        }
+
+      /* TODO: Asserts. wr_s_pg needs to work properly. Also, at this point,
+       * it needs to point to a valid location where writes can be made. If
+       * not such location is found, errout out before this point.
+       */
+
+      /* If this is true, the journal has not enough space. */
+
+      if (i == n_blks && tmp < buf_sz)
+        {
+          ret = -ENOMEM;
+          goto errout_with_lock;
+        }
+    }
+
+  kmm_free(buf);
+  sb->j_state.n_blks = n_blks;
+  sb->j_state.idxarr = idxarr; /* Ownership transferred to sb */
+  sb->j_state.s_off  = buf_sz;
+  sb->j_state.r_off  = buf_sz;
+  sb->j_state.w_off  = buf_sz;
+
+  nxmutex_unlock(&sb->fs_lock);
+  return ret;
+
+errout_with_lock:
+  nxmutex_unlock(&sb->fs_lock);
+
+  kmm_free(buf);
+
+errout_with_idxarr:
+  kmm_free(idxarr);
+
+errout:
+  return ret;
+}
+
+/* Read and initialize journal from the flash. */
+
+/* Provides information for the location of the latest master node. */
+
+int mfs_jrnl_init(FAR struct mfs_sb_info * const sb, mfs_t blk,
+                  mfs_t *m_node)
+{
+  char magic[7];
+  int idxarr_sz;
+  int buf_sz;
+  uint8_t i;
+  mfs_t tmp;
+  mfs_t pg;
+  int ret         = OK;
+  char *buf       = NULL;
+
+  memset(&sb->j_state, 0, sizeof(sb->j_state));
+
+  nxmutex_lock(&sb->fs_lock);
+
+  mfs_read_data(sb, magic, 7, MFS_BLK2PG(sb, blk), 0);
+  if (!strcmp(magic, MFS_JRNL_MAGIC))
+    {
+      ret = -EINVAL;
+      goto errout_with_lock;
+    }
+
+  mfs_read_data(sb, (char *) &sb->j_state.n_blks, 1, MFS_BLK2PG(sb, blk), 7);
+  idxarr_sz = (sb->j_state.n_blks + 2) * sizeof(mfs_t);
+  buf_sz    = idxarr_sz + 8;
+
+  sb->j_state.idxarr = kmm_zalloc(idxarr_sz);
+  if (!sb->j_state.idxarr)
+    {
+      ret = -ENOMEM;
+      goto errout_with_lock;
+    }
+
+  buf = kmm_zalloc(buf_sz);
+  if (!buf)
+    {
+      ret = -ENOMEM;
+      goto errout_with_idxarr;
+    }
+
+  if (predict_true(sb->blk_sz >= buf_sz))
+    {
+        mfs_read_data(sb, buf, buf_sz, MFS_BLK2PG(sb, blk), 0);
+
+        sb->j_state.idxarr = (mfs_t *) (buf + 8);
+    }
+  else
+    {
+      i   = 0;
+      tmp = 0;
+      tmp += mfs_read_data(sb, buf, sb->blk_sz, MFS_BLK2PG(sb, blk), 0);
+      sb->j_state.idxarr = (mfs_t *) (buf + 8);
+
+      i++;
+      while (tmp < buf_sz)
+        {
+          pg = MFS_BLK2PG(sb, sb->j_state.idxarr[i]);
+          if (predict_true(tmp + sb->blk_sz < buf_sz))
+            {
+              mfs_read_data(sb, buf, sb->blk_sz, pg, 0);
+            }
+          else
+            {
+              tmp = mfs_read_data(sb, buf, buf_sz - tmp, pg, 0);
+              pg  += ((tmp + (sb->pg_sz - 1)) / sb->pg_sz);
+              break;
+            }
+
+          i++;
+        }
+    }
+
+  sb->j_state.s_off = buf_sz;
+  sb->j_state.r_off = buf_sz;
+  sb->j_state.w_off = buf_sz;
+
+  /* TODO: Need to traverse the journal from end to get sb->j_state.w_* */
+
+  nxmutex_unlock(&sb->fs_lock);
+  return ret;
+
+errout_with_idxarr:
+  kmm_free(sb->j_state.idxarr);
+  sb->j_state.idxarr = NULL;
+
+errout_with_lock:
+  nxmutex_unlock(&sb->fs_lock);
+
+  return ret;
+}
+
+/* Traversal */
+
+/* Reset journal state to pre-traversal. */
+
+void mfs_jrnl_statereset(FAR struct mfs_sb_info * const sb)
+{
+  nxmutex_lock(&sb->fs_lock);
+  sb->j_state.r_off = sb->j_state.s_off;
+  nxmutex_unlock(&sb->fs_lock);
+}
+
+/* Advance the read pointer to the next log. -1 if reached writable
+ * pointer.
+ */
+
+int mfs_jrnl_radv(FAR struct mfs_sb_info * const sb,
+                  FAR struct mfs_jrnl_info * info)
+{
+  int ret;
+  mfs_t blk;
+  mfs_t pg;
+  mfs_t pgoff;
+  mfs_t sz;
+  mfs_t rem_sz;
+  mfs_t rem_blk_sz;
+  char *tmp;
+  char *buf;
+  struct mfs_jrnl_log log;
+
+  ret = OK;
+  buf = NULL;
+
+  nxmutex_lock(&sb->fs_lock);
+
+  ret = jrnl_off2details(sb, sb->j_state.r_off, &blk, &pg, &pgoff);
+  if (ret != 0)
+    {
+      goto errout;
+    }
+
+  mfs_read_page(sb, (char *) &sz, sizeof(mfs_t), pg, pgoff);
+
+  if (predict_true(info != NULL))
+    {
+      buf = kmm_zalloc(sz);
+      if (!buf)
+        {
+          ret = -ENOMEM;
+          goto errout;
+        }
+
+      rem_sz = sz;
+      tmp    = buf;
+
+      while (1)
+        {
+          jrnl_off2details(sb, sb->j_state.w_off, &blk, &pg, &pgoff);
+          rem_blk_sz        = mfs_blkremsz(sb, pg, pgoff);
+          mfs_read_data(sb, tmp, MIN(rem_blk_sz, rem_sz), pg, pgoff);
+          tmp               += MIN(rem_blk_sz, rem_sz);
+          sb->j_state.w_off += MIN(rem_blk_sz, rem_sz);
+
+          if (predict_false(rem_blk_sz > rem_sz))
+            {
+              break;
+            }
+        }
+
+      deser_jrnl_log(buf, &log);
+      *info = jrnl_log2info(&log);
+
+      kmm_free(buf);
+    }
+
+  if (predict_false(sb->j_state.r_off + sz > sb->j_state.w_off))
+    {
+      ret = -1;
+      goto errout;
+    }
+
+  sb->j_state.r_off += sz;
+
+errout:
+  nxmutex_unlock(&sb->fs_lock);
+  return ret;
+}
+
+/* Write */
+
+/* Can only advance the write pointer by writing a log. */
+
+int mfs_jrnl_wadv(FAR struct mfs_sb_info * const sb,
+                  FAR const struct mfs_jrnl_info * const info)
+{
+  int ret;
+  char *tmp;
+  char *buf;
+  mfs_t blk;
+  mfs_t pg;
+  mfs_t pgoff;
+  mfs_t rem_sz;
+  mfs_t rem_blk_sz;
+  struct mfs_jrnl_log log;
+
+  buf = NULL;
+  ret = OK;
+
+  if (info == NULL)
+    {
+      ret = -1;
+      goto errout;
+    }
+
+  rem_sz = jrnl_info2logsz(info);
+  buf    = kmm_zalloc(rem_sz);
+  if (!buf)
+    {
+      ret = -ENOMEM;
+      goto errout;
+    }
+
+  log = jrnl_info2log(info);
+  ser_jrnl_log(&log, buf);
+
+  tmp = buf;
+
+  nxmutex_lock(&sb->fs_lock);
+
+  while (1)
+    {
+      jrnl_off2details(sb, sb->j_state.w_off, &blk, &pg, &pgoff);
+      rem_blk_sz        = mfs_blkremsz(sb, pg, pgoff);
+      mfs_write_data(sb, tmp, MIN(rem_blk_sz, rem_sz), pg, pgoff);
+      tmp               += MIN(rem_blk_sz, rem_sz);
+      sb->j_state.w_off += MIN(rem_blk_sz, rem_sz);
+
+      if (predict_false(rem_blk_sz > rem_sz))
+        {
+          break;
+        }
+    }
+
+  kmm_free(buf);
+
+  nxmutex_unlock(&sb->fs_lock);
+
+errout:
+  return ret;
+}
+
+int mfs_jrnl_nwadv(FAR struct mfs_sb_info * const sb,
+                  FAR struct list_node list)
+{
+  int tmp;
+  int ret                    = 0;
+  struct mfs_jrnl_info *info = NULL;
+
+  list_for_every_entry(&list, info, struct mfs_jrnl_info, list)
+    {
+      tmp = mfs_jrnl_wadv(sb, info);
+      if (tmp < 0)
+        {
+          return ret;
+        }
+
+      ret++;
+    }
+
+  return ret;
+}
+
+void mfs_jrml_updatectz(FAR struct mfs_sb_info * const sb,
+                        FAR struct mfs_ctz_store_s * const path,
+                        const mfs_t depth)
+{
+  struct mfs_jrnl_info info;
+  struct mfs_ctz_store_s cur = path[depth - 1];
+
+  nxmutex_lock(&sb->fs_lock);
+
+  mfs_jrnl_statereset(sb); /* Just to be sure */
+
+  while (mfs_jrnl_radv(sb, &info) > 0)
+    {
+      if (info.depth == depth && info.path[depth - 1].pg_e == cur.pg_e &&
+          info.path[depth - 1].idx_e == cur.idx_e)
+        {
+          cur = info.new;
+        }
+    }
+
+  mfs_jrnl_statereset(sb);
+  path[depth - 1].idx_e = cur.idx_e;
+  path[depth - 1].pg_e  = cur.pg_e;
+
+  nxmutex_unlock(&sb->fs_lock);
+}
diff --git a/fs/mnemofs/mnemofs_rw.c b/fs/mnemofs/mnemofs_rw.c
new file mode 100644
index 0000000000..06df071e35
--- /dev/null
+++ b/fs/mnemofs/mnemofs_rw.c
@@ -0,0 +1,78 @@
+/****************************************************************************
+ * fs/mnemofs/mnemofs_rw.c
+ * Read/Write utilities for mnemofs
+ *
+ * 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 "mnemofs.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+ssize_t mfs_write_data(FAR const struct mfs_sb_info * const sb,
+                      char *data, uint64_t datalen, uint32_t page,
+                      uint8_t pgoff)
+{
+  /* TODO */ /* Uses an LRU Cache */
+  return 0;
+};
+
+ssize_t mfs_read_data(FAR const struct mfs_sb_info * const sb,
+                      char *data, uint64_t datalen, uint32_t page,
+                      uint8_t pgoff)
+{
+  /* TODO */ /* Uses an LRU Cache */
+  return 0;
+};
+
+ssize_t mfs_read_page(FAR const struct mfs_sb_info * const sb,
+                      char *data, uint64_t datalen, uint32_t page,
+                      uint8_t pgoff)
+{
+  /* TODO */ /* Cache */
+  return 0;
+}
diff --git a/fs/mnemofs/mnemofs_util.c b/fs/mnemofs/mnemofs_util.c
new file mode 100644
index 0000000000..a27ae78fde
--- /dev/null
+++ b/fs/mnemofs/mnemofs_util.c
@@ -0,0 +1,128 @@
+/****************************************************************************
+ * fs/mnemofs/mnemofs_util.c
+ * Utilities for mnemofs
+ *
+ * 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 "mnemofs.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+char mfs_arrhash(FAR const char *arr, ssize_t len)
+{
+  ssize_t l = 0;
+  ssize_t r = len - 1;
+  uint16_t hash = 0;
+
+  while (l <= r)
+    {
+      hash += arr[l] * arr[r] * l * r;
+      l++;
+      r--;
+      hash %= (1 << 8);
+    }
+
+  return hash % (1 << 8);
+}
+
+char *mfs_ser_8(const char n, char * const out)
+{
+  memcpy(out, &n, sizeof(n));
+  return out + sizeof(n);
+}
+
+const char *mfs_deser_8(const char * const in, char *n)
+{
+  memcpy(&n, in, sizeof(n));
+  return in + sizeof(n);
+}
+
+char *mfs_ser_str(const char * const str, const mfs_t len, char * const out)
+{
+  memcpy(out, str, len);
+  return out + len;
+}
+
+const char *mfs_deser_str(const char * const in, char * const str,
+                          const mfs_t len)
+{
+  memcpy(str, in, len);
+  return in + len;
+}
+
+char *mfs_ser_mfs(const mfs_t n, char * const out)
+{
+  memcpy(out, &n, sizeof(n));
+  return out + sizeof(n);
+}
+
+const char *mfs_deser_mfs(const char * const in, mfs_t * const n)
+{
+  memcpy(n, in, sizeof(*n));
+  return in + sizeof(n);
+}
+
+char *mfs_ser_ctz_store(const struct mfs_ctz_store_s * const x,
+                        char * const out)
+{
+  char *o = out;
+
+  o = mfs_ser_mfs(x->pg_e, o);
+  o = mfs_ser_mfs(x->idx_e, o);
+  return o;
+}
+
+const char *mfs_deser_ctz_store(const char * const in,
+                                struct mfs_ctz_store_s * const x)
+{
+  const char *i = in;
+
+  i = mfs_deser_mfs(i, &x->pg_e);
+  i = mfs_deser_mfs(i, &x->idx_e);
+  return i;
+}
diff --git a/fs/mount/fs_mount.c b/fs/mount/fs_mount.c
index 2bbef28958..4780f34613 100644
--- a/fs/mount/fs_mount.c
+++ b/fs/mount/fs_mount.c
@@ -57,8 +57,8 @@
 
 /* These file systems require MTD drivers */
 
-#if (defined(CONFIG_FS_SPIFFS) || defined(CONFIG_FS_LITTLEFS)) && \
-    defined(CONFIG_MTD)
+#if (defined(CONFIG_FS_SPIFFS) || defined(CONFIG_FS_LITTLEFS) || \
+    defined(CONFIG_FS_MNEMOFS)) && defined(CONFIG_MTD)
 #  define MDFS_SUPPORT 1
 #endif
 
@@ -132,6 +132,9 @@ extern const struct mountpt_operations g_spiffs_operations;
 #ifdef CONFIG_FS_LITTLEFS
 extern const struct mountpt_operations g_littlefs_operations;
 #endif
+#ifdef CONFIG_FS_MNEMOFS
+extern const struct mountpt_operations g_mnemofs_operations;
+#endif
 
 static const struct fsmap_t g_mdfsmap[] =
 {
@@ -143,6 +146,9 @@ static const struct fsmap_t g_mdfsmap[] =
 #endif
 #ifdef CONFIG_FS_LITTLEFS
     { "littlefs", &g_littlefs_operations },
+#endif
+#ifdef CONFIG_FS_MNEMOFS
+    { "mnemofs", &g_mnemofs_operations },
 #endif
     { NULL,   NULL },
 };

Reply via email to