mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] state: treat state with all-invalid buckets as dirty
Date: Thu,  5 Mar 2020 08:40:32 +0100	[thread overview]
Message-ID: <20200305074032.29725-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20200305074032.29725-1-a.fatoum@pengutronix.de>

The state.dirty flag controls whether state_save will actually
persist state. It is cleared when we successfully load or save
state and set on writing a state parameter.

When the state however becomes corrupt during barebox runtime and
state.dirty == 0, reinitializing the state to defaults is quite
cumbersome:

1. We reset twice. After the first reset, the dirty flag is reset
   and before the second, state_save will reinitialize to defaults
2. We write any state variable and then run the state -s command

Both workarounds are quite obscure, improve the user experience
by having state -l set the dirty flag when it fails, so a subsequent
state -s may persist the default values to state.

Steps to reproduce:

  barebox$ state -l
  state: Using bucket 0@0x00000000
  barebox$ memcpy -s /dev/zero -d /dev/eeprom0.state 0 0 0x400
  barebox$ state -s
  barebox$ state -l
  ERROR: state: No meta data header found
  ERROR: state: No meta data header found
  ERROR: state: No meta data header found
  ERROR: state: Failed to find any valid state copy in any bucket
  ERROR: state: Failed to read state with format raw, -2
  state: No such file or directory

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/state/state.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/common/state/state.c b/common/state/state.c
index b168387eef0b..1822f37f3e29 100644
--- a/common/state/state.c
+++ b/common/state/state.c
@@ -94,7 +94,7 @@ out:
  */
 static int state_do_load(struct state *state, enum state_flags flags)
 {
-	void *buf;
+	void *buf = NULL;
 	ssize_t len;
 	int ret;
 
@@ -103,7 +103,7 @@ static int state_do_load(struct state *state, enum state_flags flags)
 	if (ret) {
 		dev_err(&state->dev, "Failed to read state with format %s, %d\n",
 			state->format->name, ret);
-		return ret;
+		goto out;
 	}
 
 	ret = state->format->unpack(state->format, state, buf, len);
@@ -114,9 +114,8 @@ static int state_do_load(struct state *state, enum state_flags flags)
 	}
 
 	state->init_from_defaults = 0;
-	state->dirty = 0;
-
 out:
+	state->dirty = !!ret; /* mark dirty on error */
 	free(buf);
 	return ret;
 }
-- 
2.25.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2020-03-05  7:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-05  7:40 [PATCH 0/2] state: deal gracefully with runtime bucket corruption Ahmad Fatoum
2020-03-05  7:40 ` [PATCH 1/2] state: backend_storage: " Ahmad Fatoum
2020-03-05  7:40 ` Ahmad Fatoum [this message]
2020-03-09  7:36 ` [PATCH 0/2] state: " Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200305074032.29725-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox