XinStellaris commented on code in PR #6829: URL: https://github.com/apache/incubator-nuttx/pull/6829#discussion_r966582021
########## drivers/mtd/mtd_config_fs.c: ########## @@ -0,0 +1,2262 @@ +/**************************************************************************** + * drivers/mtd/mtd_config_fs.c + * + * 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. + * + * NVS: non volatile storage in flash + * + * Copyright (c) 2018 Laczen + * + * SPDX-License-Identifier: Apache-2.0 + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <debug.h> +#include <fcntl.h> +#include <errno.h> +#include <sys/poll.h> +#include <nuttx/crc8.h> +#include <nuttx/kmalloc.h> +#include <nuttx/mtd/mtd.h> +#include <nuttx/mtd/configdata.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MIN(a, b) ((a) > (b) ? (b) : (a)) + +/* MASKS AND SHIFT FOR ADDRESSES + * an address in nvs is an uint32_t where: + * high 2 bytes represent the block number + * low 2 bytes represent the offset in a block + */ + +#define ADDR_BLOCK_MASK 0xFFFF0000 +#define ADDR_BLOCK_SHIFT 16 +#define ADDR_OFFS_MASK 0x0000FFFF + +/* we don't want to store all the read content in stack or heap, + * so we make a buffer to do compare or move. + */ + +#define NVS_BUFFER_SIZE 32 + +/* if data is written after last ate, and power loss happens, + * we need to find a clean offset by skipping dirty data. + * This macro defines how many bytes to skip when dirty data + * is spotted(may take several skips). + * Normally 1 byte is okay, such process only happens when + * nvs is started, and it is acceptable to take some time during + * starting. + */ + +#define NVS_CORRUPT_DATA_SKIP_STEP 1 + +#define NVS_LOOKUP_CACHE_NO_ADDR 0xffffffff + +/* gc done or close ate has the id of 0xffffffff. + * We can tell if the ate is special by looking at its id. + */ + +#define NVS_SPECIAL_ATE_ID 0xffffffff + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* Non-volatile Storage File system structure */ + +struct nvs_fs +{ + FAR struct mtd_dev_s *mtd; /* mtd device */ + struct mtd_geometry_s geo; + uint32_t ate_wra; /* next alloc table entry + * write address + */ + uint32_t data_wra; /* next data write address */ + uint32_t step_addr; /* for traverse */ + mutex_t nvs_lock; + +#if CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE_SIZE > 0 + uint32_t lookup_cache[CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE_SIZE]; +#endif +}; + +/* Allocation Table Entry */ + +begin_packed_struct struct nvs_ate +{ + uint32_t id; /* data id */ + uint16_t offset; /* data offset within block */ + uint16_t len; /* data len within block */ + uint16_t key_len; /* key string len */ + uint8_t part; /* part of a multipart data - future extension */ + uint8_t crc8; /* crc8 check of the ate entry */ + uint8_t expired; /* 0xFF-newest entry, others-old entry */ + uint8_t reserved[3]; /* for future extension */ +} end_packed_struct; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* MTD NVS opeation api */ + +static int mtdconfig_open(FAR struct file *filep); +static int mtdconfig_close(FAR struct file *filep); +static ssize_t mtdconfig_read(FAR struct file *filep, FAR char *buffer, + size_t buflen); +static int mtdconfig_ioctl(FAR struct file *filep, int cmd, + unsigned long arg); +static int mtdconfig_poll(FAR struct file *filep, FAR struct pollfd *fds, + bool setup); + +/* NVS operations needed by lookup cache */ + +static int nvs_prev_ate(FAR struct nvs_fs *fs, FAR uint32_t *addr, + FAR struct nvs_ate *ate); +static int nvs_ate_valid(FAR struct nvs_fs *fs, + FAR const struct nvs_ate *entry); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct file_operations g_mtdnvs_fops = +{ + mtdconfig_open, /* open */ + mtdconfig_close, /* close */ + mtdconfig_read, /* read */ + NULL, /* write */ + NULL, /* seek */ + mtdconfig_ioctl, /* ioctl */ + mtdconfig_poll /* poll */ +#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS + , NULL /* unlink */ +#endif +}; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nvs_fnv_hash + ****************************************************************************/ + +static uint32_t nvs_fnv_hash(FAR const uint8_t *input, uint32_t len) +{ + uint32_t i = 0; + uint32_t hval = 2166136261; + + /* FNV-1 hash each octet in the buffer */ + + while (i++ < len) + { + /* multiply by the 32 bit FNV magic prime mod 2^32 */ + + hval *= 0x01000193; + + /* xor the bottom with the current octet */ + + hval ^= *input++; + } + + return hval; +} + +#if CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE_SIZE > 0 + +/**************************************************************************** + * Name: nvs_lookup_cache_pos + ****************************************************************************/ + +static inline size_t nvs_lookup_cache_pos(uint32_t id) +{ + return id % CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE_SIZE; +} + +/**************************************************************************** + * Name: nvs_lookup_cache_rebuild + ****************************************************************************/ + +static int nvs_lookup_cache_rebuild(FAR struct nvs_fs *fs) +{ + int rc; + uint32_t addr; + uint32_t ate_addr; + FAR uint32_t *cache_entry; + struct nvs_ate ate; + int i; + + for (i < 0; i < CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE_SIZE; i++) + { + fs->lookup_cache[i] = NVS_LOOKUP_CACHE_NO_ADDR; + } + + addr = fs->ate_wra; + + do + { + /* Make a copy of 'addr' as it will be advanced by nvs_prev_ate() */ + + ate_addr = addr; + rc = nvs_prev_ate(fs, &addr, &ate); + if (rc) + { + return rc; + } + + cache_entry = &fs->lookup_cache[nvs_lookup_cache_pos(ate.id)]; + + if (ate.id != NVS_SPECIAL_ATE_ID + && *cache_entry == NVS_LOOKUP_CACHE_NO_ADDR + && nvs_ate_valid(fs, &ate)) Review Comment: delete cache -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org