Skip to content

Commit

Permalink
fs: nvs: Improve nvs_calc_free_space() result precision
Browse files Browse the repository at this point in the history
The nvs_calc_free_space() function does not return 0 when the NVS
is considered full, because some special ATEs are not taken into account.

This commit takes into account the ATE that is reserved for deletion in
each sector, in addition of the 'GC done' ATE when present.

(cherry picked from commit 08496ff)

Original-Signed-off-by: Adrien Ricciardi <aricciardi@baylibre.com>
GitOrigin-RevId: 08496ff
Change-Id: I53ec160b59b5f9038d5ca363367cdc8e6cb7435d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/5663173
Tested-by: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
Tested-by: Ting Shen <phoenixshen@chromium.org>
Reviewed-by: Ting Shen <phoenixshen@chromium.org>
Commit-Queue: Ting Shen <phoenixshen@chromium.org>
  • Loading branch information
RICCIARDI-Adrien authored and Chromeos LUCI committed Jun 27, 2024
1 parent ae84679 commit a44dac8
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions subsys/fs/nvs/nvs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,11 @@ ssize_t nvs_calc_free_space(struct nvs_fs *fs)

free_space = 0;
for (uint16_t i = 1; i < fs->sector_count; i++) {
free_space += (fs->sector_size - ate_size);
/*
* There is always a closing ATE and a reserved ATE for
* deletion in each sector
*/
free_space += (fs->sector_size - (2 * ate_size));
}

step_addr = fs->ate_wra;
Expand All @@ -1333,11 +1337,17 @@ ssize_t nvs_calc_free_space(struct nvs_fs *fs)
}
}

if ((wlk_addr == step_addr) && step_ate.len &&
(nvs_ate_valid(fs, &step_ate))) {
/* count needed */
free_space -= nvs_al_size(fs, step_ate.len);
free_space -= ate_size;
if (nvs_ate_valid(fs, &step_ate)) {
/* Take into account the GC done ATE if it is present */
if (step_ate.len == 0) {
if (step_ate.id == 0xFFFF) {
free_space -= ate_size;
}
} else if (wlk_addr == step_addr) {
/* count needed */
free_space -= nvs_al_size(fs, step_ate.len);
free_space -= ate_size;
}
}

if (step_addr == fs->ate_wra) {
Expand Down

0 comments on commit a44dac8

Please sign in to comment.