From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Wed, 31 May 2023 17:23:00 +0200 Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by lore.white.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q4NfB-004QPQ-IX for lore@lore.pengutronix.de; Wed, 31 May 2023 17:23:00 +0200 Received: from localhost ([127.0.0.1] helo=metis.ext.pengutronix.de) by metis.ext.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1q4Nf7-0005h3-QT; Wed, 31 May 2023 17:22:57 +0200 Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q4Nf6-0005eu-G2; Wed, 31 May 2023 17:22:56 +0200 Received: from [2a0a:edc0:0:1101:1d::54] (helo=dude05.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1q4Nf5-0049An-R8; Wed, 31 May 2023 17:22:55 +0200 Received: from afa by dude05.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1q4Nf4-005uDm-A7; Wed, 31 May 2023 17:22:54 +0200 From: Ahmad Fatoum To: oss-tools@pengutronix.de Date: Wed, 31 May 2023 17:22:46 +0200 Message-Id: <20230531152253.1407395-2-a.fatoum@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230531152253.1407395-1-a.fatoum@pengutronix.de> References: <20230531152253.1407395-1-a.fatoum@pengutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [OSS-Tools] [PATCH 1/8] state: backend: direct: open block device in read-only mode if possible X-BeenThere: oss-tools@pengutronix.de X-Mailman-Version: 2.1.29 Precedence: list List-Id: Pengutronix Public Open-Source-Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "OSS-Tools" X-SA-Exim-Connect-IP: 127.0.0.1 X-SA-Exim-Mail-From: oss-tools-bounces@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false We unconditionally open the device backing a direct bucket in read-write mode. The barebox_state utility already populates struct state_backend_storage::readonly though, which we could consult at device open time. Do so. This could possibly be done for MTD as well, but until that's tested, for now, we do it only for the direct backend meant for use with block devices. Tested-by: Ulrich Ölmann Signed-off-by: Ahmad Fatoum --- src/barebox-state/backend_bucket_direct.c | 5 +++-- src/barebox-state/backend_storage.c | 2 +- src/barebox-state/state.c | 6 +++--- src/barebox-state/state.h | 3 ++- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/barebox-state/backend_bucket_direct.c b/src/barebox-state/backend_bucket_direct.c index 4522f0170f3d..48c1596c9add 100644 --- a/src/barebox-state/backend_bucket_direct.c +++ b/src/barebox-state/backend_bucket_direct.c @@ -164,12 +164,13 @@ static void state_backend_bucket_direct_free(struct int state_backend_bucket_direct_create(struct device_d *dev, const char *path, struct state_backend_storage_bucket **bucket, - off_t offset, ssize_t max_size) + off_t offset, ssize_t max_size, + bool readonly) { int fd; struct state_backend_storage_bucket_direct *direct; - fd = open(path, O_RDWR); + fd = open(path, readonly ? O_RDONLY : O_RDWR); if (fd < 0) { dev_err(dev, "Failed to open file '%s', %d\n", path, -errno); return -errno; diff --git a/src/barebox-state/backend_storage.c b/src/barebox-state/backend_storage.c index 458f2a91552b..4c5e1783c199 100644 --- a/src/barebox-state/backend_storage.c +++ b/src/barebox-state/backend_storage.c @@ -326,7 +326,7 @@ static int state_storage_file_buckets_init(struct state_backend_storage *storage offset = storage->offset + n * stridesize; ret = state_backend_bucket_direct_create(storage->dev, storage->path, &bucket, offset, - stridesize); + stridesize, storage->readonly); if (ret) { dev_warn(storage->dev, "Failed to create direct bucket at '%s' offset %lld\n", storage->path, (long long) offset); diff --git a/src/barebox-state/state.c b/src/barebox-state/state.c index 4779574d2ee9..3174c84b8ded 100644 --- a/src/barebox-state/state.c +++ b/src/barebox-state/state.c @@ -649,14 +649,14 @@ struct state *state_new_from_node(struct device_node *node, bool readonly) if (ret) goto out_release_state; + if (readonly) + state_backend_set_readonly(state); + ret = state_storage_init(state, state->backend_path, offset, size, stridesize, storage_type); if (ret) goto out_release_state; - if (readonly) - state_backend_set_readonly(state); - ret = state_from_node(state, node, 1); if (ret) { goto out_release_state; diff --git a/src/barebox-state/state.h b/src/barebox-state/state.h index 719f7e43c94c..4abfa84285c3 100644 --- a/src/barebox-state/state.h +++ b/src/barebox-state/state.h @@ -225,7 +225,8 @@ void state_backend_set_readonly(struct state *state); void state_storage_free(struct state_backend_storage *storage); int state_backend_bucket_direct_create(struct device_d *dev, const char *path, struct state_backend_storage_bucket **bucket, - off_t offset, ssize_t max_size); + off_t offset, ssize_t max_size, + bool readonly); int state_storage_write(struct state_backend_storage *storage, const void * buf, ssize_t len); int state_storage_read(struct state_backend_storage *storage, -- 2.39.2