mirror of
https://github.com/eclipse-threadx/levelx.git
synced 2026-09-14 12:37:01 +08:00
WARNING: This feature is brand new and has not been tested beyond the
existing regression suite. It should be thoroughly tested — including
stress, power-loss, and wear-levelling scenarios — before being used in
any production system.
When LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE is defined and free blocks
exceed LX_NAND_FLASH_SECTOR_RELEASE_LAZY_THRESHOLD (default 10), releasing
a sector from a full block defers the copy+erase. A tombstone page is
written to a freshly-allocated block; the old full block is flagged
COMPACTION_PENDING in its block status and recorded in a new per-LG
compaction table. Compaction (merge + erase) is triggered lazily:
* sector_write: before writing to a full block with a pending source
* block_data_move: intercepts wear-levelling moves for pending LGs
* defragment: iterates all pending LGs (now actually implemented)
* block_allocate: emergency compaction when the free list is empty
Reads transparently fall back to the compaction-pending block when the
primary block does not contain the requested sector.
Crash recovery is handled in open_extended:
- Phase 1: detects COMPACTION_PENDING blocks and either clears the flag
(crash before mapping update — abort) or rebuilds compaction_table
(crash after mapping update — resume deferred compaction).
- Phase 2: erases orphaned ALLOCATED blocks not present in any mapping.
Without the flag, all paths remain unchanged and defragment returns
LX_NOT_SUPPORTED as before.
Fixes: https://github.com/eclipse-threadx/levelx/issues/70
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
130 lines
5.6 KiB
C
130 lines
5.6 KiB
C
/***************************************************************************
|
|
* Copyright (c) 2024 Microsoft Corporation
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors
|
|
*
|
|
* This program and the accompanying materials are made available under the
|
|
* terms of the MIT License which is available at
|
|
* https://opensource.org/licenses/MIT.
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
**************************************************************************/
|
|
|
|
// Some portions generated by Copilot (Sonnet 4.6).
|
|
|
|
/**************************************************************************/
|
|
/**************************************************************************/
|
|
/** */
|
|
/** LevelX Component */
|
|
/** */
|
|
/** NAND Flash */
|
|
/** */
|
|
/**************************************************************************/
|
|
/**************************************************************************/
|
|
|
|
#define LX_SOURCE_CODE
|
|
|
|
|
|
/* Disable ThreadX error checking. */
|
|
|
|
#ifndef LX_DISABLE_ERROR_CHECKING
|
|
#define LX_DISABLE_ERROR_CHECKING
|
|
#endif
|
|
|
|
|
|
/* Include necessary system files. */
|
|
|
|
#include "lx_api.h"
|
|
|
|
|
|
/**************************************************************************/
|
|
/* */
|
|
/* FUNCTION RELEASE */
|
|
/* */
|
|
/* _lx_nand_flash_block_allocate PORTABLE C */
|
|
/* 6.2.1 */
|
|
/* AUTHOR */
|
|
/* */
|
|
/* Xiuwen Cai, Microsoft Corporation */
|
|
/* */
|
|
/* DESCRIPTION */
|
|
/* */
|
|
/* This function attempts to get a block in the free block list. */
|
|
/* */
|
|
/* INPUT */
|
|
/* */
|
|
/* nand_flash NAND flash instance */
|
|
/* block Return block number */
|
|
/* */
|
|
/* OUTPUT */
|
|
/* */
|
|
/* return status */
|
|
/* */
|
|
/* CALLS */
|
|
/* */
|
|
/* None */
|
|
/* */
|
|
/* CALLED BY */
|
|
/* */
|
|
/* Internal LevelX */
|
|
/* */
|
|
/**************************************************************************/
|
|
UINT _lx_nand_flash_block_allocate(LX_NAND_FLASH* nand_flash, ULONG* block)
|
|
{
|
|
|
|
|
|
/* Check if the free block list is empty. */
|
|
if (nand_flash -> lx_nand_flash_free_block_list_tail == 0)
|
|
{
|
|
|
|
#ifdef LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE
|
|
{
|
|
|
|
ULONG lg;
|
|
UINT compact_status;
|
|
|
|
/* Emergency compaction: find any pending compaction and execute it to
|
|
reclaim the old full block. */
|
|
for (lg = 0; lg < nand_flash -> lx_nand_flash_total_blocks; lg++)
|
|
{
|
|
|
|
if (nand_flash -> lx_nand_flash_block_compaction_table[lg] != (USHORT)LX_NAND_BLOCK_UNMAPPED)
|
|
{
|
|
|
|
compact_status = _lx_nand_flash_logical_group_compact(nand_flash, lg);
|
|
|
|
if (compact_status == LX_SUCCESS)
|
|
{
|
|
|
|
/* Retry the allocation after compaction freed a block. */
|
|
if (nand_flash -> lx_nand_flash_free_block_list_tail > 0)
|
|
{
|
|
|
|
nand_flash -> lx_nand_flash_free_block_list_tail--;
|
|
*block = nand_flash -> lx_nand_flash_block_list[nand_flash -> lx_nand_flash_free_block_list_tail];
|
|
return(LX_SUCCESS);
|
|
}
|
|
}
|
|
|
|
/* Attempt only one compaction per allocate call. */
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
#endif /* LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE */
|
|
|
|
/* Empty list, return error. */
|
|
return(LX_NO_BLOCKS);
|
|
}
|
|
|
|
/* Remove one block from the list. */
|
|
nand_flash -> lx_nand_flash_free_block_list_tail--;
|
|
|
|
/* Return the block number. */
|
|
*block = nand_flash -> lx_nand_flash_block_list[nand_flash -> lx_nand_flash_free_block_list_tail];
|
|
|
|
/* Return successful completion. */
|
|
return(LX_SUCCESS);
|
|
}
|
|
|