Commit 08496ff6 authored by Adrien Ricciardi's avatar Adrien Ricciardi Committed by Alberto Escolar
Browse files

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.

Signed-off-by: default avatarAdrien Ricciardi <aricciardi@baylibre.com>
parent 1d1dc7a8
Loading
Loading
Loading
Loading
+16 −6
Original line number Diff line number Diff line
@@ -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;
@@ -1333,12 +1337,18 @@ ssize_t nvs_calc_free_space(struct nvs_fs *fs)
			}
		}

		if ((wlk_addr == step_addr) && step_ate.len &&
		    (nvs_ate_valid(fs, &step_ate))) {
		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) {
			break;