2024-07-09 20:57:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/mnemofs/mnemofs_lru.c
|
|
|
|
* LRU cache 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.
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* the BSD-3-Clause license:
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
|
|
|
* Copyright (c) 2024 Saurav Pal
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the author nor the names of its contributors may
|
|
|
|
* be used to endorse or promote products derived from this software
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* LRU (Least Recently Used) cache takes in all the changes the user wants
|
|
|
|
* to do to the on-flash storage, and stores them in memory. When a
|
|
|
|
* significant amount of changes are accumulated, the LRU writes the new
|
|
|
|
* information to the flash.
|
|
|
|
*
|
|
|
|
* LRU is a kernel list of nodes. Each node represents a CTZ list. Each node
|
|
|
|
* contains a kernel list of changes requested for the CTZ list, called as
|
|
|
|
* deltas.
|
|
|
|
*
|
|
|
|
* When LRU is full the last node is flushed (it can be explicitly flushed as
|
|
|
|
* well) and all the changes are written at once on the flash, and the new
|
|
|
|
* location is noted down in the journal, and an entry for the location
|
|
|
|
* update is added to the LRU for the parent.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/list.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include "mnemofs.h"
|
2024-09-09 14:28:16 +02:00
|
|
|
#include "fs_heap.h"
|
2024-07-09 20:57:16 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-07-12 16:13:48 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MFS_LRU_UPD,
|
|
|
|
MFS_LRU_DEL,
|
|
|
|
};
|
|
|
|
|
2024-07-09 20:57:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
static void lru_nodesearch(FAR const struct mfs_sb_s * const sb,
|
2024-07-12 16:13:48 +02:00
|
|
|
FAR const struct mfs_path_s * const path,
|
|
|
|
const mfs_t depth, FAR struct mfs_node_s **node);
|
|
|
|
static bool lru_islrufull(FAR struct mfs_sb_s * const sb);
|
2024-08-17 08:40:07 +02:00
|
|
|
static bool lru_isnodefull(FAR struct mfs_sb_s * const sb,
|
|
|
|
FAR struct mfs_node_s *node);
|
2024-08-08 18:38:56 +02:00
|
|
|
static int lru_nodeflush(FAR struct mfs_sb_s * const sb,
|
|
|
|
FAR struct mfs_path_s * const path,
|
|
|
|
const mfs_t depth, FAR struct mfs_node_s *node,
|
2024-08-17 08:40:07 +02:00
|
|
|
bool rm_node);
|
2024-08-08 18:38:56 +02:00
|
|
|
static int lru_wrtooff(FAR struct mfs_sb_s * const sb, const mfs_t data_off,
|
|
|
|
mfs_t bytes, int op,
|
|
|
|
FAR struct mfs_path_s * const path,
|
|
|
|
const mfs_t depth, FAR const char *buf);
|
|
|
|
static int lru_updatesz(FAR struct mfs_sb_s * sb,
|
2024-07-12 16:13:48 +02:00
|
|
|
FAR struct mfs_path_s * const path,
|
2024-08-08 18:38:56 +02:00
|
|
|
const mfs_t depth, const mfs_t new_sz);
|
2024-08-17 08:40:07 +02:00
|
|
|
static void lru_node_free(FAR struct mfs_node_s *node);
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-07-09 20:57:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-07-12 16:13:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lru_nodesearch
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Searches a node by the `path` and `depth` in the LRU.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sb - Superblock instance of the device.
|
|
|
|
* path - CTZ representation of the relpath.
|
|
|
|
* depth - Depth of `path`.
|
|
|
|
* node - To populate with the node corresponding to the CTZ.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
static void lru_nodesearch(FAR const struct mfs_sb_s * const sb,
|
2024-07-12 16:13:48 +02:00
|
|
|
FAR const struct mfs_path_s * const path,
|
|
|
|
const mfs_t depth, FAR struct mfs_node_s **node)
|
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
bool found = false;
|
|
|
|
mfs_t i;
|
2024-07-12 16:13:48 +02:00
|
|
|
FAR struct mfs_node_s *n;
|
|
|
|
|
|
|
|
*node = NULL;
|
2024-08-08 18:38:56 +02:00
|
|
|
|
2024-07-12 16:13:48 +02:00
|
|
|
list_for_every_entry(&MFS_LRU(sb), n, struct mfs_node_s, list)
|
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
/* We need this loop to specifically check the parents in case these
|
|
|
|
* entries are all new, and have not been allocated any pages for
|
|
|
|
* being stored in the flash. Also we know that the root (depth 1) will
|
|
|
|
* be at least common in their paths.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(depth > 0);
|
2024-07-12 16:13:48 +02:00
|
|
|
|
|
|
|
if (n->depth != depth)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
found = true;
|
|
|
|
for (i = n->depth; i >= 1 && found; i--)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
if (path[i - 1].ctz.idx_e == 0 && path[i - 1].ctz.pg_e == 0 &&
|
|
|
|
mfs_ctz_eq(&n->path[i - 1].ctz, &path[i - 1].ctz) &&
|
|
|
|
n->path[i - 1].off == path[i - 1].off)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
/* OK */
|
|
|
|
}
|
|
|
|
else if (path[i - 1].ctz.pg_e != 0 &&
|
|
|
|
mfs_ctz_eq(&n->path[i - 1].ctz, &path[i - 1].ctz))
|
|
|
|
{
|
|
|
|
/* OK */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
found = false;
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
*node = n;
|
2024-08-08 18:38:56 +02:00
|
|
|
break;
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
finfo("Node search ended with match of node %p at depth %u"
|
2024-09-02 16:24:52 +02:00
|
|
|
" for CTZ of %" PRIu32 " size with range [%" PRIu32 ", %" PRIi32
|
|
|
|
").", n, n->depth, n->sz, n->range_min, n->range_max);
|
2024-08-08 18:38:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
finfo("Node search ended without match.");
|
|
|
|
*node = NULL;
|
|
|
|
}
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lru_islrufull
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check whether the number of nodes in the LRU has reaches its limit.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2024-08-08 18:38:56 +02:00
|
|
|
* sb - Superblock instance of the device.
|
2024-07-12 16:13:48 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* true - LRU is full
|
|
|
|
* false - LRU is not full.
|
|
|
|
*
|
2024-08-17 08:40:07 +02:00
|
|
|
* Assumptions/Limitations:
|
|
|
|
* When the journal is being flushed, LRU memory limiters will be turned
|
|
|
|
* off.
|
|
|
|
*
|
2024-07-12 16:13:48 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool lru_islrufull(FAR struct mfs_sb_s * const sb)
|
|
|
|
{
|
2024-08-17 08:40:07 +02:00
|
|
|
return !MFS_FLUSH(sb) && list_length(&MFS_LRU(sb)) == CONFIG_MNEMOFS_NLRU;
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lru_isnodefull
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check whether the number of deltas in an LRU node has reaches its limit.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* node - LRU node.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* true - LRU node is full
|
|
|
|
* false - LRU node is not full.
|
|
|
|
*
|
2024-08-17 08:40:07 +02:00
|
|
|
* Assumptions/Limitations:
|
|
|
|
* When the journal is being flushed, LRU memory limiters will be turned
|
|
|
|
* off.
|
|
|
|
*
|
2024-07-12 16:13:48 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
static bool lru_isnodefull(FAR struct mfs_sb_s * const sb,
|
|
|
|
FAR struct mfs_node_s *node)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-17 08:40:07 +02:00
|
|
|
return !MFS_FLUSH(sb) && node->n_list == CONFIG_MNEMOFS_NLRUDELTA;
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lru_free_delta
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a node's delta.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* delta - LRU delta.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-07-12 16:13:48 +02:00
|
|
|
static void lru_free_delta(FAR struct mfs_delta_s *delta)
|
|
|
|
{
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(delta->upd);
|
|
|
|
fs_heap_free(delta);
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lru_nodeflush
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clear out the deltas in a node by writing them to the flash, and adding
|
2024-08-17 08:40:07 +02:00
|
|
|
* a log about it to the journal. Does not flush the journal, and assumes
|
|
|
|
* enough space is in the journal to handle a log.
|
2024-07-12 16:13:48 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2024-08-17 08:40:07 +02:00
|
|
|
* sb - Superblock instance of the device.
|
|
|
|
* path - CTZ representation of the relpath.
|
|
|
|
* depth - Depth of `path`.
|
|
|
|
* node - LRU node to flush.
|
|
|
|
* rm_node - To remove node out of LRU (true), or just clear the deltas
|
|
|
|
* (false).
|
2024-07-12 16:13:48 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2024-08-17 22:21:32 +02:00
|
|
|
* 0 - OK
|
|
|
|
* < 0 - Error
|
2024-07-12 16:13:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lru_nodeflush(FAR struct mfs_sb_s * const sb,
|
|
|
|
FAR struct mfs_path_s * const path,
|
2024-08-08 18:38:56 +02:00
|
|
|
const mfs_t depth, FAR struct mfs_node_s *node,
|
2024-08-17 08:40:07 +02:00
|
|
|
bool rm_node)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
int ret = OK;
|
2024-08-17 08:40:07 +02:00
|
|
|
struct mfs_ctz_s loc;
|
2024-07-12 16:13:48 +02:00
|
|
|
FAR struct mfs_delta_s *delta = NULL;
|
|
|
|
FAR struct mfs_delta_s *tmp = NULL;
|
|
|
|
|
|
|
|
if (predict_false(node == NULL))
|
|
|
|
{
|
2024-08-17 08:40:07 +02:00
|
|
|
return -EINVAL;
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
ret = mfs_ctz_wrtnode(sb, node, &loc);
|
2024-07-12 16:13:48 +02:00
|
|
|
if (predict_false(ret < 0))
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free deltas after flush. */
|
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
finfo("Removing Deltas.");
|
|
|
|
|
|
|
|
list_for_every_entry_safe(&node->delta, delta, tmp, struct mfs_delta_s,
|
2024-07-12 16:13:48 +02:00
|
|
|
list)
|
|
|
|
{
|
|
|
|
list_delete_init(&delta->list);
|
|
|
|
lru_free_delta(delta);
|
|
|
|
}
|
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
if (rm_node)
|
|
|
|
{
|
2024-09-02 16:24:52 +02:00
|
|
|
finfo("Deleting node. Old size: %zu.", list_length(&MFS_LRU(sb)));
|
2024-08-17 08:40:07 +02:00
|
|
|
list_delete_init(&node->list);
|
2024-09-02 16:24:52 +02:00
|
|
|
finfo("Deleted node. New size: %zu.", list_length(&MFS_LRU(sb)));
|
2024-08-17 08:40:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Reset node stats. */
|
|
|
|
|
|
|
|
finfo("Resetting node.");
|
|
|
|
memset(node, 0, sizeof(struct mfs_node_s));
|
|
|
|
node->range_min = UINT32_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
finfo("Updating CTZ in parent.");
|
|
|
|
ret = mfs_lru_updatectz(sb, node->path, node->depth, loc, node->sz);
|
|
|
|
|
|
|
|
if (rm_node)
|
|
|
|
{
|
|
|
|
finfo("Freeing node.");
|
|
|
|
lru_node_free(node);
|
|
|
|
}
|
|
|
|
|
2024-07-12 16:13:48 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lru_wrtooff
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to offset in LRU.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sb - Superblock instance of the device.
|
|
|
|
* data_off - Offset into the data in the CTZ skip list.
|
|
|
|
* bytes - Number of bytes to write.
|
|
|
|
* ctz_sz - Size of the CTZ skip list.
|
|
|
|
* op - Operation (MFS_LRU_UPD or MFS_LRU_DEL).
|
|
|
|
* path - CTZ representation of the path.
|
|
|
|
* depth - Depth of path.
|
|
|
|
* buf - Buffer containing data.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 - OK
|
|
|
|
* < 0 - Error
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lru_wrtooff(FAR struct mfs_sb_s * const sb, const mfs_t data_off,
|
|
|
|
mfs_t bytes, int op,
|
|
|
|
FAR struct mfs_path_s * const path, const mfs_t depth,
|
|
|
|
FAR const char *buf)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
int ret = OK;
|
|
|
|
bool found = true;
|
|
|
|
mfs_t old_sz;
|
2024-07-12 16:13:48 +02:00
|
|
|
FAR struct mfs_node_s *node = NULL;
|
|
|
|
FAR struct mfs_node_s *last_node = NULL;
|
|
|
|
FAR struct mfs_delta_s *delta = NULL;
|
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
DEBUGASSERT(depth > 0);
|
|
|
|
|
2024-07-12 16:13:48 +02:00
|
|
|
lru_nodesearch(sb, path, depth, &node);
|
|
|
|
|
|
|
|
if (node == NULL)
|
|
|
|
{
|
2024-09-09 14:28:16 +02:00
|
|
|
node = fs_heap_zalloc(sizeof(*node));
|
2024-07-12 16:13:48 +02:00
|
|
|
if (predict_false(node == NULL))
|
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
found = false;
|
2024-07-12 16:13:48 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
node->path = fs_heap_zalloc(depth * sizeof(struct mfs_path_s));
|
2024-08-08 18:38:56 +02:00
|
|
|
if (predict_false(node->path == NULL))
|
|
|
|
{
|
|
|
|
found = false;
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->sz = path[depth - 1].sz;
|
2024-07-12 16:13:48 +02:00
|
|
|
node->depth = depth;
|
|
|
|
node->n_list = 0;
|
|
|
|
node->range_max = 0;
|
|
|
|
node->range_min = UINT32_MAX;
|
|
|
|
list_initialize(&node->delta);
|
|
|
|
memcpy(node->path, path, depth * sizeof(struct mfs_path_s));
|
|
|
|
found = false;
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
finfo("Node not found. Allocated at %p.", node);
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
if (lru_islrufull(sb))
|
|
|
|
{
|
|
|
|
finfo("LRU is full, need to flush a node.");
|
|
|
|
last_node = list_container_of(list_peek_tail(&MFS_LRU(sb)),
|
|
|
|
struct mfs_node_s, list);
|
|
|
|
list_delete_init(&last_node->list);
|
|
|
|
list_add_tail(&MFS_LRU(sb), &node->list);
|
2024-09-02 16:24:52 +02:00
|
|
|
finfo("LRU flushing node complete, now only %zu nodes",
|
2024-08-17 08:40:07 +02:00
|
|
|
list_length(&MFS_LRU(sb)));
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
list_add_tail(&MFS_LRU(sb), &node->list);
|
2024-09-02 16:24:52 +02:00
|
|
|
finfo("Node inserted into LRU, and it now %zu node(s).",
|
2024-08-17 08:40:07 +02:00
|
|
|
list_length(&MFS_LRU(sb)));
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-17 08:40:07 +02:00
|
|
|
else if (found && lru_isnodefull(sb, node))
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-17 08:40:07 +02:00
|
|
|
/* This can be optimized further if needed, but for now, for saftey of
|
|
|
|
* the data, I think it's better to flush the entire thing. It won't
|
|
|
|
* flush ALL of it, just, whatever's required.
|
|
|
|
*/
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
ret = mnemofs_flush(sb);
|
2024-07-12 16:13:48 +02:00
|
|
|
if (predict_false(ret < 0))
|
|
|
|
{
|
|
|
|
goto errout_with_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add delta to node. */
|
|
|
|
|
|
|
|
finfo("Adding delta to the node.");
|
2024-09-09 14:28:16 +02:00
|
|
|
delta = fs_heap_zalloc(sizeof(*delta));
|
2024-07-12 16:13:48 +02:00
|
|
|
if (predict_false(delta == NULL))
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
finfo("Delta allocated.");
|
|
|
|
|
|
|
|
if (op == MFS_LRU_UPD)
|
|
|
|
{
|
2024-09-09 14:28:16 +02:00
|
|
|
delta->upd = fs_heap_zalloc(bytes);
|
2024-07-12 16:13:48 +02:00
|
|
|
if (predict_false(delta->upd == NULL))
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_delta;
|
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
finfo("Delta is of the update type, has %u bytes at offset %u.",
|
|
|
|
bytes, data_off);
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
delta->n_b = bytes;
|
|
|
|
delta->off = data_off;
|
|
|
|
list_add_tail(&node->delta, &delta->list);
|
|
|
|
if (op == MFS_LRU_UPD)
|
|
|
|
{
|
|
|
|
memcpy(delta->upd, buf, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
node->n_list++;
|
2024-08-08 18:38:56 +02:00
|
|
|
node->range_min = MIN(node->range_min, data_off);
|
|
|
|
node->range_max = MAX(node->range_max, data_off + bytes);
|
|
|
|
|
|
|
|
old_sz = node->sz;
|
|
|
|
node->sz = MAX(node->range_max, path[depth - 1].sz);
|
|
|
|
node->path[node->depth - 1].sz = node->sz;
|
|
|
|
|
|
|
|
if (old_sz != node->sz)
|
|
|
|
{
|
|
|
|
ret = lru_updatesz(sb, node->path, node->depth, node->sz);
|
|
|
|
if (predict_false(ret < 0))
|
|
|
|
{
|
|
|
|
goto errout_with_delta;
|
|
|
|
}
|
|
|
|
}
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-09-02 16:24:52 +02:00
|
|
|
finfo("Delta attached to node %p. Now there are %zu nodes and the"
|
|
|
|
" node has %zu deltas. Node with range [%" PRIu32 ", %"
|
|
|
|
PRIu32 ").", node, list_length(&MFS_LRU(sb)),
|
|
|
|
list_length(&node->delta), node->range_min, node->range_max);
|
2024-07-12 16:13:48 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
errout_with_delta:
|
2024-08-08 18:38:56 +02:00
|
|
|
list_delete(&delta->list);
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(delta);
|
2024-07-12 16:13:48 +02:00
|
|
|
|
|
|
|
errout_with_node:
|
|
|
|
if (!found && node != NULL)
|
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
list_delete(&node->list);
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(node);
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lru_updatesz
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Updates size of an fs object in its parent.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sb - Superblock instance of the device.
|
|
|
|
* path - CTZ representation of the path.
|
|
|
|
* depth - Depth of path.
|
|
|
|
* new_sz - New size
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 - OK
|
|
|
|
* < 0 - Error
|
|
|
|
*
|
|
|
|
* Assumptions/Limitations:
|
|
|
|
* Adds an entry for the target's parent to update the child's size, and
|
|
|
|
* updates the size in path of everyone that has this child.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lru_updatesz(FAR struct mfs_sb_s * sb,
|
|
|
|
FAR struct mfs_path_s * const path,
|
|
|
|
const mfs_t depth, const mfs_t new_sz)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
struct mfs_node_s *n = NULL;
|
|
|
|
mfs_t i;
|
|
|
|
bool found;
|
|
|
|
char buf[sizeof(mfs_t)];
|
|
|
|
|
|
|
|
DEBUGASSERT(depth > 0);
|
|
|
|
|
|
|
|
list_for_every_entry(&MFS_LRU(sb), n, struct mfs_node_s, list)
|
|
|
|
{
|
|
|
|
if (n->depth < depth)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
found = false;
|
|
|
|
for (i = depth; i >= 1; i--)
|
|
|
|
{
|
|
|
|
if (path[i - 1].ctz.idx_e == 0 && path[i - 1].ctz.pg_e == 0 &&
|
|
|
|
mfs_ctz_eq(&n->path[i - 1].ctz, &path[i - 1].ctz) &&
|
|
|
|
n->path[i - 1].off == path[i - 1].off)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
else if (path[i - 1].ctz.pg_e != 0 &&
|
|
|
|
mfs_ctz_eq(&n->path[i - 1].ctz, &path[i - 1].ctz))
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
n->path[depth - 1].sz = new_sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depth == 1)
|
|
|
|
{
|
|
|
|
MFS_MN(sb).root_sz = new_sz;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(mfs_t));
|
|
|
|
mfs_ser_mfs(new_sz, buf);
|
|
|
|
|
|
|
|
/* This function will be used by mfs_lru_wr itself, but given that if
|
2024-08-17 08:40:07 +02:00
|
|
|
* there is no change in size, this won't cause an infinite loop (or, in
|
|
|
|
* reality, a recursion till it reaches the top of the tree), this should
|
|
|
|
* be fine.
|
2024-08-08 18:38:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = mfs_lru_wr(sb, path[depth - 2].off + offsetof(struct mfs_dirent_s,
|
2024-08-17 08:40:07 +02:00
|
|
|
sz), sizeof(mfs_t), path, depth - 1, buf);
|
2024-08-08 18:38:56 +02:00
|
|
|
if (predict_false(ret < 0))
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
path[depth - 1].sz = new_sz;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
static void lru_node_free(FAR struct mfs_node_s *node)
|
|
|
|
{
|
|
|
|
mfs_free_patharr(node->path);
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(node);
|
2024-08-17 08:40:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool lru_sort_cmp(FAR struct mfs_node_s * const node,
|
|
|
|
FAR struct mfs_node_s * const pivot)
|
|
|
|
{
|
|
|
|
return node->depth < pivot->depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lru_sort(FAR struct mfs_sb_s * const sb,
|
|
|
|
FAR struct list_node *left,
|
|
|
|
FAR struct list_node *right)
|
|
|
|
{
|
|
|
|
FAR struct mfs_node_s *node = NULL;
|
|
|
|
FAR struct mfs_node_s *next = NULL;
|
|
|
|
FAR struct mfs_node_s *pivot = NULL;
|
|
|
|
FAR struct list_node *aend = NULL; /* After end. */
|
|
|
|
FAR struct list_node *bfirst = NULL; /* Before first. */
|
|
|
|
|
|
|
|
if (left == right)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If left or right is NULL, it means that refers to MFS_LRU(sb). */
|
|
|
|
|
|
|
|
aend = right->next;
|
|
|
|
bfirst = left->prev;
|
|
|
|
|
|
|
|
node = list_container_of(left, struct mfs_node_s, list);
|
|
|
|
pivot = list_container_of(right, struct mfs_node_s, list);
|
|
|
|
|
|
|
|
if (node->list.next == &pivot->list)
|
|
|
|
{
|
|
|
|
/* Only two items in the window...node and pivot, so insertion sort. */
|
|
|
|
|
|
|
|
if (lru_sort_cmp(node, pivot))
|
|
|
|
{
|
|
|
|
/* Add node after the pivot. */
|
|
|
|
|
|
|
|
list_delete_init(&node->list);
|
|
|
|
list_add_after(&pivot->list, &node->list);
|
|
|
|
|
|
|
|
DEBUGASSERT(pivot->list.prev == bfirst);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(!lru_sort_cmp(node, pivot));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_every_entry_safe_from(&MFS_LRU(sb), node, next, struct mfs_node_s,
|
|
|
|
list)
|
|
|
|
{
|
|
|
|
if (node == pivot)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lru_sort_cmp(node, pivot))
|
|
|
|
{
|
|
|
|
/* Add node after the pivot. */
|
|
|
|
|
|
|
|
list_delete_init(&node->list);
|
|
|
|
list_add_after(&pivot->list, &node->list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bfirst->next != &pivot->list)
|
|
|
|
{
|
|
|
|
lru_sort(sb, bfirst->next, pivot->list.prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aend->prev != &pivot->list)
|
|
|
|
{
|
|
|
|
lru_sort(sb, pivot->list.next, aend->prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 20:57:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
int mfs_lru_flush(FAR struct mfs_sb_s * const sb)
|
2024-07-09 20:57:16 +02:00
|
|
|
{
|
2024-08-17 08:40:07 +02:00
|
|
|
int ret = OK;
|
|
|
|
FAR struct mfs_node_s *tmp = NULL;
|
|
|
|
FAR struct mfs_node_s *tmp2 = NULL;
|
|
|
|
FAR struct mfs_node_s *node = NULL;
|
|
|
|
FAR struct mfs_node_s *next = NULL;
|
|
|
|
|
|
|
|
/* Modified quick sort in linked lists. What is wanted is like inverted
|
|
|
|
* topological sort, but all the files (no children) are at the front,
|
|
|
|
* their depths don't matter. BUT, when it comes to directories, they need
|
|
|
|
* to be sorted in a decreasing order of their depths to reduce updates due
|
|
|
|
* to CoW. This will trickle up to the root, such that the root will be the
|
|
|
|
* last to get updated, and then the master node.
|
|
|
|
*
|
|
|
|
* However, since passing the mode all the way requires a lot of change, and
|
|
|
|
* is a redundant piece of information in most cases, the quick sort can
|
|
|
|
* simply be done on the basis of depth, and this adventure can be left as a
|
|
|
|
* TOOD.
|
|
|
|
*
|
|
|
|
* This involves recursion, but given the LRU size is a constant, the depth
|
|
|
|
* of recursion will be log2(n). For an LRU size of even 128 (which is quite
|
|
|
|
* big), the stack depth for this will be 7.
|
|
|
|
*/
|
2024-07-09 20:57:16 +02:00
|
|
|
|
2024-09-02 16:24:52 +02:00
|
|
|
finfo("Sorting the LRU. No. of nodes: %zu", list_length(&MFS_LRU(sb)));
|
2024-08-17 08:40:07 +02:00
|
|
|
|
|
|
|
lru_sort(sb, MFS_LRU(sb).next, MFS_LRU(sb).prev);
|
|
|
|
MFS_FLUSH(sb) = true;
|
|
|
|
|
|
|
|
list_for_every_entry_safe(&MFS_LRU(sb), node, next, struct mfs_node_s,
|
|
|
|
list)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-17 08:40:07 +02:00
|
|
|
finfo("Current node depth: %u.", node->depth);
|
|
|
|
|
|
|
|
if (node->depth != 1)
|
|
|
|
{
|
|
|
|
finfo("Checking for parent.");
|
|
|
|
|
|
|
|
/* Ensuring parent is either present, or inserted into the LRU.
|
|
|
|
* No need of doing this before removing current node from LRU,
|
|
|
|
* however, this allows us to possibly skip allocating path again
|
|
|
|
* after freeing the current node.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* We can not rely on normal LRU node insertions, as they will
|
|
|
|
* not be inserted in a sorted manner, and would need the entire
|
|
|
|
* LRU to be sorted again, so we insert it manually.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lru_nodesearch(sb, node->path, node->depth - 1, &tmp);
|
|
|
|
|
|
|
|
if (tmp == NULL)
|
|
|
|
{
|
|
|
|
finfo("Adding parent to LRU");
|
|
|
|
|
2024-09-09 14:28:16 +02:00
|
|
|
tmp = fs_heap_zalloc(sizeof(struct mfs_node_s));
|
2024-08-17 08:40:07 +02:00
|
|
|
if (predict_false(tmp == NULL))
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp->range_max = 0;
|
|
|
|
tmp->range_min = UINT32_MAX;
|
|
|
|
|
|
|
|
/* TODO: Time fields. in tmp. */
|
|
|
|
|
|
|
|
tmp->depth = node->depth - 1;
|
2024-09-09 14:28:16 +02:00
|
|
|
tmp->path = fs_heap_zalloc((node->depth - 1)
|
2024-08-17 08:40:07 +02:00
|
|
|
* sizeof(struct mfs_path_s));
|
|
|
|
if (predict_false(tmp->path == NULL))
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(tmp->path, node->path,
|
|
|
|
sizeof(struct mfs_path_s) * tmp->depth);
|
|
|
|
list_initialize(&tmp->list);
|
|
|
|
list_initialize(&tmp->delta);
|
|
|
|
|
|
|
|
/* Insert into sorted. */
|
|
|
|
|
|
|
|
list_for_every_entry(&MFS_LRU(sb), tmp2, struct mfs_node_s,
|
|
|
|
list)
|
|
|
|
{
|
|
|
|
if (!lru_sort_cmp(tmp, tmp2))
|
|
|
|
{
|
|
|
|
list_add_before(&tmp2->list, &tmp->list);
|
|
|
|
|
|
|
|
if (tmp2->list.prev == &node->list)
|
|
|
|
{
|
|
|
|
next = tmp2;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
finfo("Parent already in LRU.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
finfo("Root node from LRU.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parent gets updated inside the LRU in the function below. */
|
|
|
|
|
|
|
|
finfo("Flushing node.");
|
|
|
|
ret = lru_nodeflush(sb, node->path, node->depth, node, true);
|
|
|
|
if (predict_true(ret < 0))
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
errout_with_tmp:
|
|
|
|
lru_node_free(node);
|
|
|
|
|
|
|
|
errout:
|
|
|
|
MFS_FLUSH(sb) = false;
|
|
|
|
return ret;
|
2024-07-09 20:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int mfs_lru_del(FAR struct mfs_sb_s * const sb, const mfs_t data_off,
|
2024-08-08 18:38:56 +02:00
|
|
|
mfs_t bytes, FAR struct mfs_path_s * const path,
|
|
|
|
const mfs_t depth)
|
2024-07-09 20:57:16 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
return lru_wrtooff(sb, data_off, bytes, MFS_LRU_DEL, path, depth, NULL);
|
2024-07-09 20:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int mfs_lru_wr(FAR struct mfs_sb_s * const sb, const mfs_t data_off,
|
2024-08-08 18:38:56 +02:00
|
|
|
mfs_t bytes, FAR struct mfs_path_s * const path,
|
2024-07-12 16:13:48 +02:00
|
|
|
const mfs_t depth, FAR const char *buf)
|
2024-07-09 20:57:16 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
return lru_wrtooff(sb, data_off, bytes, MFS_LRU_UPD, path, depth, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mfs_lru_init(FAR struct mfs_sb_s * const sb)
|
|
|
|
{
|
|
|
|
list_initialize(&MFS_LRU(sb));
|
|
|
|
|
|
|
|
finfo("LRU Initialized\n");
|
2024-07-09 20:57:16 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
int mfs_lru_rdfromoff(FAR const struct mfs_sb_s * const sb,
|
|
|
|
const mfs_t data_off,
|
2024-07-09 20:57:16 +02:00
|
|
|
FAR struct mfs_path_s * const path, const mfs_t depth,
|
|
|
|
FAR char *buf, const mfs_t buflen)
|
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
/* Requires updated path from the journal. */
|
|
|
|
|
|
|
|
int ret = OK;
|
|
|
|
mfs_t upper;
|
|
|
|
mfs_t lower;
|
|
|
|
mfs_t rem_sz;
|
|
|
|
mfs_t upper_og;
|
|
|
|
mfs_t upper_upd;
|
|
|
|
mfs_t lower_upd;
|
|
|
|
FAR char *tmp;
|
|
|
|
struct mfs_ctz_s ctz;
|
2024-07-12 16:13:48 +02:00
|
|
|
FAR struct mfs_node_s *node = NULL;
|
|
|
|
FAR struct mfs_delta_s *delta = NULL;
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
/* Node is NOT supposed to be freed by the caller, it's a reference to
|
|
|
|
* the actual node in the LRU and freeing it could break the entire LRU.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tmp = buf;
|
2024-08-17 08:40:07 +02:00
|
|
|
ctz = path[depth - 1].ctz;
|
2024-08-08 18:38:56 +02:00
|
|
|
lower = data_off;
|
|
|
|
upper_og = lower + buflen;
|
|
|
|
upper = upper_og;
|
|
|
|
rem_sz = buflen;
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
lru_nodesearch(sb, path, depth, &node);
|
|
|
|
if (node == NULL)
|
|
|
|
{
|
|
|
|
mfs_ctz_rdfromoff(sb, ctz, 0, buflen, tmp);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
while (rem_sz > 0)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
mfs_ctz_rdfromoff(sb, ctz, lower, rem_sz, tmp);
|
|
|
|
|
2024-07-12 16:13:48 +02:00
|
|
|
list_for_every_entry(&node->delta, delta, struct mfs_delta_s, list)
|
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
if (delta->upd == NULL)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
/* Delete */
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
lower_upd = MAX(lower, delta->off);
|
|
|
|
upper_upd = MIN(upper, delta->off + delta->n_b);
|
|
|
|
|
|
|
|
if (lower_upd >= upper_upd)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
/* Outside range */
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
2024-08-08 18:38:56 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memmove(tmp + (lower - lower_upd),
|
|
|
|
tmp + (upper_upd - lower), upper - upper_upd);
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
upper -= (upper_upd - lower_upd);
|
|
|
|
}
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
/* Update */
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
lower_upd = MAX(lower, delta->off);
|
|
|
|
upper_upd = MIN(upper, delta->off + delta->n_b);
|
2024-07-09 20:57:16 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
if (lower_upd >= upper_upd)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
/* Outside range */
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
memcpy(tmp + (lower_upd - lower),
|
|
|
|
delta->upd + (lower_upd - delta->off),
|
|
|
|
upper_upd - lower_upd);
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
tmp += upper - lower;
|
|
|
|
rem_sz -= upper - lower;
|
|
|
|
lower = upper;
|
|
|
|
upper = upper_og;
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
2024-07-09 20:57:16 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
int mfs_lru_updatedinfo(FAR const struct mfs_sb_s * const sb,
|
|
|
|
FAR struct mfs_path_s * const path,
|
|
|
|
const mfs_t depth)
|
2024-07-09 20:57:16 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
int ret = OK;
|
|
|
|
bool found;
|
|
|
|
mfs_t i;
|
|
|
|
FAR struct mfs_node_s *node = NULL;
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
DEBUGASSERT(depth > 0);
|
2024-07-09 20:57:16 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
list_for_every_entry(&MFS_LRU(sb), node, struct mfs_node_s, list)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
/* TODO: When a directory is newly created, and still in the LRU, its
|
|
|
|
* CTZ is (0, 0), and this can match others as well if at same depth,
|
|
|
|
* so, in these cases, match the parents, and so on up.
|
|
|
|
*/
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
DEBUGASSERT(node->depth > 0);
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
if (node->depth > depth)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
/* We need this loop to specifically check the parents in case these
|
|
|
|
* entries are all new, and have not been allocated any pages for
|
|
|
|
* being stored in the flash. Also we know that the root (depth 1) will
|
|
|
|
* be at least common in their paths.
|
|
|
|
*/
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
found = true;
|
|
|
|
for (i = node->depth; i >= 1 && found; i--)
|
|
|
|
{
|
|
|
|
if (path[i - 1].ctz.idx_e == 0 && path[i - 1].ctz.pg_e == 0 &&
|
|
|
|
mfs_ctz_eq(&node->path[i - 1].ctz, &path[i - 1].ctz) &&
|
|
|
|
node->path[i - 1].off == path[i - 1].off)
|
|
|
|
{
|
|
|
|
/* OK */
|
|
|
|
}
|
|
|
|
else if (path[i - 1].ctz.pg_e != 0 &&
|
|
|
|
mfs_ctz_eq(&node->path[i - 1].ctz, &path[i - 1].ctz))
|
|
|
|
{
|
|
|
|
/* OK */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
found = false;
|
|
|
|
}
|
|
|
|
}
|
2024-07-12 16:13:48 +02:00
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
path[node->depth - 1].sz = node->path[node->depth - 1].sz;
|
|
|
|
}
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mfs_lru_updatectz(FAR struct mfs_sb_s * sb,
|
|
|
|
FAR struct mfs_path_s * const path, const mfs_t depth,
|
2024-08-17 08:40:07 +02:00
|
|
|
const struct mfs_ctz_s new_ctz, mfs_t new_sz)
|
2024-08-08 18:38:56 +02:00
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
char buf[sizeof(struct mfs_ctz_s)];
|
|
|
|
FAR struct mfs_node_s *node = NULL;
|
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
/* TODO: Other attributes like time stamps to be updated as well. */
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
list_for_every_entry(&MFS_LRU(sb), node, struct mfs_node_s, list)
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
if (node->depth >= depth &&
|
2024-08-17 08:40:07 +02:00
|
|
|
mfs_ctz_eq(&node->path[depth - 1].ctz, &path[depth - 1].ctz))
|
2024-07-12 16:13:48 +02:00
|
|
|
{
|
2024-08-08 18:38:56 +02:00
|
|
|
node->path[depth - 1].ctz = new_ctz;
|
2024-08-17 08:40:07 +02:00
|
|
|
node->path[depth - 1].sz = path[depth - 1].sz;
|
2024-07-12 16:13:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 08:40:07 +02:00
|
|
|
if (depth == 1)
|
|
|
|
{
|
|
|
|
MFS_MN(sb).root_sz = new_sz;
|
|
|
|
MFS_MN(sb).root_ctz = new_ctz;
|
|
|
|
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write to LRU. */
|
|
|
|
|
2024-08-08 18:38:56 +02:00
|
|
|
memset(buf, 0, sizeof(struct mfs_ctz_s));
|
|
|
|
mfs_ser_ctz(&new_ctz, buf);
|
|
|
|
ret = mfs_lru_wr(sb, path[depth - 1].off + offsetof(struct mfs_dirent_s,
|
2024-08-17 08:40:07 +02:00
|
|
|
ctz), sizeof(struct mfs_ctz_s), path, depth - 1, buf);
|
|
|
|
if (predict_false(ret < 0))
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = lru_updatesz(sb, path, depth, new_sz);
|
2024-08-08 18:38:56 +02:00
|
|
|
if (predict_false(ret < 0))
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
path[depth - 1].ctz = new_ctz;
|
2024-08-17 08:40:07 +02:00
|
|
|
path[depth - 1].sz = new_sz;
|
2024-08-08 18:38:56 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
2024-07-09 20:57:16 +02:00
|
|
|
}
|
2024-08-17 08:40:07 +02:00
|
|
|
|
|
|
|
bool mfs_lru_isempty(FAR struct mfs_sb_s * const sb)
|
|
|
|
{
|
|
|
|
return list_length(&MFS_LRU(sb)) == 0;
|
|
|
|
}
|