mirror of
https://github.com/eclipse-threadx/levelx.git
synced 2026-09-14 20:48:51 +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>
This commit is contained in:
co-authored by
Copilot
parent
a46b74fb8a
commit
bfb39676df
@@ -9,6 +9,7 @@
|
||||
* SPDX-License-Identifier: MIT
|
||||
**************************************************************************/
|
||||
|
||||
// Some portions generated by Copilot (Sonnet 4.6).
|
||||
|
||||
/**************************************************************************/
|
||||
/**************************************************************************/
|
||||
@@ -205,6 +206,119 @@ USHORT new_block_status;
|
||||
if (block_status & LX_NAND_BLOCK_STATUS_FULL)
|
||||
{
|
||||
|
||||
#ifdef LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE
|
||||
/* Lazy path: defer the copy+erase when enough free blocks are available.
|
||||
The old block is kept as a compaction source; a new block holds only
|
||||
the tombstone for the released sector. */
|
||||
if (nand_flash -> lx_nand_flash_free_block_list_tail >
|
||||
(ULONG)LX_NAND_FLASH_SECTOR_RELEASE_LAZY_THRESHOLD)
|
||||
{
|
||||
|
||||
/* Allocate a new block for the tombstone. */
|
||||
status = _lx_nand_flash_block_allocate(nand_flash, &new_block);
|
||||
|
||||
/* Check return status. */
|
||||
if (status != LX_SUCCESS)
|
||||
{
|
||||
|
||||
/* Call system error handler. */
|
||||
_lx_nand_flash_system_error(nand_flash, status, new_block, 0);
|
||||
#ifdef LX_THREAD_SAFE_ENABLE
|
||||
|
||||
/* Release the thread safe mutex. */
|
||||
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
||||
#endif
|
||||
/* Return an error. */
|
||||
return(LX_ERROR);
|
||||
}
|
||||
|
||||
/* Set page buffer to all 0xFF (data = erased, sector is released). */
|
||||
LX_MEMSET(nand_flash -> lx_nand_flash_page_buffer, 0xFF,
|
||||
nand_flash -> lx_nand_flash_bytes_per_page +
|
||||
nand_flash -> lx_nand_flash_spare_total_length);
|
||||
|
||||
/* Setup spare buffer pointer. */
|
||||
spare_buffer_ptr = nand_flash -> lx_nand_flash_page_buffer +
|
||||
nand_flash -> lx_nand_flash_bytes_per_page;
|
||||
|
||||
/* Save metadata block number in spare bytes if space allows. */
|
||||
if (nand_flash -> lx_nand_flash_spare_data2_length >= sizeof(USHORT))
|
||||
{
|
||||
LX_UTILITY_SHORT_SET(&spare_buffer_ptr[nand_flash -> lx_nand_flash_spare_data2_offset],
|
||||
nand_flash -> lx_nand_flash_metadata_block_number);
|
||||
}
|
||||
|
||||
/* Set page type to USER_DATA_RELEASED. */
|
||||
LX_UTILITY_LONG_SET(&spare_buffer_ptr[nand_flash -> lx_nand_flash_spare_data1_offset],
|
||||
LX_NAND_PAGE_TYPE_USER_DATA_RELEASED | logical_sector);
|
||||
|
||||
/* Write the tombstone page to page 0 of the new block. */
|
||||
#ifdef LX_NAND_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
|
||||
status = (nand_flash -> lx_nand_flash_driver_pages_write)(nand_flash, new_block, 0,
|
||||
(UCHAR*)nand_flash -> lx_nand_flash_page_buffer, spare_buffer_ptr, 1);
|
||||
#else
|
||||
status = (nand_flash -> lx_nand_flash_driver_pages_write)(new_block, 0,
|
||||
(UCHAR*)nand_flash -> lx_nand_flash_page_buffer, spare_buffer_ptr, 1);
|
||||
#endif
|
||||
|
||||
/* Check for an error from flash driver. */
|
||||
if (status)
|
||||
{
|
||||
_lx_nand_flash_system_error(nand_flash, status, new_block, 0);
|
||||
#ifdef LX_THREAD_SAFE_ENABLE
|
||||
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
||||
#endif
|
||||
return(LX_ERROR);
|
||||
}
|
||||
|
||||
/* Set new block status: allocated, non-sequential, 1 page written. */
|
||||
new_block_status = (USHORT)(LX_NAND_BLOCK_STATUS_ALLOCATED |
|
||||
LX_NAND_BLOCK_STATUS_NON_SEQUENTIAL | 1u);
|
||||
status = _lx_nand_flash_block_status_set(nand_flash, new_block, new_block_status);
|
||||
if (status)
|
||||
{
|
||||
_lx_nand_flash_system_error(nand_flash, status, new_block, 0);
|
||||
#ifdef LX_THREAD_SAFE_ENABLE
|
||||
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
||||
#endif
|
||||
return(LX_ERROR);
|
||||
}
|
||||
|
||||
/* Mark old block as compaction-pending (persisted to flash).
|
||||
Write order: tombstone written → new_block status set →
|
||||
COMPACTION_PENDING set → mapping updated.
|
||||
This ordering ensures safe crash recovery at open time. */
|
||||
status = _lx_nand_flash_block_status_set(nand_flash, block,
|
||||
(USHORT)(block_status | LX_NAND_BLOCK_STATUS_COMPACTION_PENDING));
|
||||
if (status)
|
||||
{
|
||||
_lx_nand_flash_system_error(nand_flash, status, block, 0);
|
||||
#ifdef LX_THREAD_SAFE_ENABLE
|
||||
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
||||
#endif
|
||||
return(LX_ERROR);
|
||||
}
|
||||
|
||||
/* Remove old mapping before updating to new block. */
|
||||
_lx_nand_flash_mapped_block_list_remove(nand_flash,
|
||||
logical_sector / nand_flash -> lx_nand_flash_pages_per_block);
|
||||
|
||||
/* Update logical-to-physical mapping to the new tombstone block. */
|
||||
_lx_nand_flash_block_mapping_set(nand_flash, logical_sector, new_block);
|
||||
|
||||
/* Record the old block as the compaction source (RAM only; rebuilt at open). */
|
||||
nand_flash -> lx_nand_flash_block_compaction_table[logical_sector / nand_flash -> lx_nand_flash_pages_per_block] = (USHORT)block;
|
||||
|
||||
/* Add new tombstone block to the mapped list. */
|
||||
_lx_nand_flash_mapped_block_list_add(nand_flash,
|
||||
logical_sector / nand_flash -> lx_nand_flash_pages_per_block);
|
||||
}
|
||||
else
|
||||
#endif /* LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE */
|
||||
{
|
||||
|
||||
/* Eager path (existing): allocate a new block and copy + erase immediately. */
|
||||
|
||||
/* Allocate a new block. */
|
||||
status = _lx_nand_flash_block_allocate(nand_flash, &new_block);
|
||||
|
||||
@@ -387,6 +501,7 @@ USHORT new_block_status;
|
||||
/* Add the new block to mapped block list. */
|
||||
_lx_nand_flash_mapped_block_list_add(nand_flash, logical_sector / nand_flash -> lx_nand_flash_pages_per_block);
|
||||
}
|
||||
} /* end eager path else block */
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user