Skip to content

Commit 492c993

Browse files
[nrf fromlist] fs: nvs: Improve nvs_calc_free_space() result precision
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. Upstream PR: zephyrproject-rtos/zephyr#74900 Signed-off-by: Adrien Ricciardi <[email protected]>
1 parent 2c2f60d commit 492c993

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

subsys/fs/nvs/nvs.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,11 @@ ssize_t nvs_calc_free_space(struct nvs_fs *fs)
13551355

13561356
free_space = 0;
13571357
for (uint16_t i = 1; i < fs->sector_count; i++) {
1358-
free_space += (fs->sector_size - ate_size);
1358+
/*
1359+
* There is always a closing ATE and a reserved ATE for
1360+
* deletion in each sector
1361+
*/
1362+
free_space += (fs->sector_size - (2 * ate_size));
13591363
}
13601364

13611365
step_addr = fs->ate_wra;
@@ -1379,11 +1383,17 @@ ssize_t nvs_calc_free_space(struct nvs_fs *fs)
13791383
}
13801384
}
13811385

1382-
if ((wlk_addr == step_addr) && step_ate.len &&
1383-
(nvs_ate_valid(fs, &step_ate))) {
1384-
/* count needed */
1385-
free_space -= nvs_al_size(fs, step_ate.len);
1386-
free_space -= ate_size;
1386+
if (nvs_ate_valid(fs, &step_ate)) {
1387+
/* Take into account the GC done ATE if it is present */
1388+
if (step_ate.len == 0) {
1389+
if (step_ate.id == 0xFFFF) {
1390+
free_space -= ate_size;
1391+
}
1392+
} else if (wlk_addr == step_addr) {
1393+
/* count needed */
1394+
free_space -= nvs_al_size(fs, step_ate.len);
1395+
free_space -= ate_size;
1396+
}
13871397
}
13881398

13891399
if (step_addr == fs->ate_wra) {

0 commit comments

Comments
 (0)