mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] state: backend: direct: open backend in read-only mode if possible
@ 2023-01-16 12:54 Ahmad Fatoum
  2023-01-16 14:23 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2023-01-16 12:54 UTC (permalink / raw)
  To: barebox; +Cc: uol, Ahmad Fatoum

We unconditionally open the device backing a direct bucket in read-write
mode. We already populate 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.

This has no functional change for barebox, which calls state_new_from_node
in the DT driver and in the EFI initcall always with readonly=false, but
we can benefit from this in barebox_state to open a device in read-only
mode when possible (e.g. when called to --dump).

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/state/backend_bucket_direct.c | 4 ++--
 common/state/backend_storage.c       | 2 +-
 common/state/state.c                 | 6 +++---
 common/state/state.h                 | 2 +-
 4 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/common/state/backend_bucket_direct.c b/common/state/backend_bucket_direct.c
index 3818c6f0b0ec..f06e14277862 100644
--- a/common/state/backend_bucket_direct.c
+++ b/common/state/backend_bucket_direct.c
@@ -164,12 +164,12 @@ static void state_backend_bucket_direct_free(struct
 
 int state_backend_bucket_direct_create(struct device *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/common/state/backend_storage.c b/common/state/backend_storage.c
index c55d22e37f70..df81902bf7be 100644
--- a/common/state/backend_storage.c
+++ b/common/state/backend_storage.c
@@ -332,7 +332,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/common/state/state.c b/common/state/state.c
index a614c849c74e..cabe285fbdf3 100644
--- a/common/state/state.c
+++ b/common/state/state.c
@@ -653,14 +653,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/common/state/state.h b/common/state/state.h
index 7eb51bbdb57d..f0c5b1de4123 100644
--- a/common/state/state.h
+++ b/common/state/state.h
@@ -226,7 +226,7 @@ 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 *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.30.2




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] state: backend: direct: open backend in read-only mode if possible
  2023-01-16 12:54 [PATCH] state: backend: direct: open backend in read-only mode if possible Ahmad Fatoum
@ 2023-01-16 14:23 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2023-01-16 14:23 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox, uol

On Mon, Jan 16, 2023 at 01:54:43PM +0100, Ahmad Fatoum wrote:
> We unconditionally open the device backing a direct bucket in read-write
> mode. We already populate 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.
> 
> This has no functional change for barebox, which calls state_new_from_node
> in the DT driver and in the EFI initcall always with readonly=false, but
> we can benefit from this in barebox_state to open a device in read-only
> mode when possible (e.g. when called to --dump).
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  common/state/backend_bucket_direct.c | 4 ++--
>  common/state/backend_storage.c       | 2 +-
>  common/state/state.c                 | 6 +++---
>  common/state/state.h                 | 2 +-
>  4 files changed, 7 insertions(+), 7 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/common/state/backend_bucket_direct.c b/common/state/backend_bucket_direct.c
> index 3818c6f0b0ec..f06e14277862 100644
> --- a/common/state/backend_bucket_direct.c
> +++ b/common/state/backend_bucket_direct.c
> @@ -164,12 +164,12 @@ static void state_backend_bucket_direct_free(struct
>  
>  int state_backend_bucket_direct_create(struct device *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/common/state/backend_storage.c b/common/state/backend_storage.c
> index c55d22e37f70..df81902bf7be 100644
> --- a/common/state/backend_storage.c
> +++ b/common/state/backend_storage.c
> @@ -332,7 +332,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/common/state/state.c b/common/state/state.c
> index a614c849c74e..cabe285fbdf3 100644
> --- a/common/state/state.c
> +++ b/common/state/state.c
> @@ -653,14 +653,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/common/state/state.h b/common/state/state.h
> index 7eb51bbdb57d..f0c5b1de4123 100644
> --- a/common/state/state.h
> +++ b/common/state/state.h
> @@ -226,7 +226,7 @@ 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 *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.30.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-01-16 14:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-16 12:54 [PATCH] state: backend: direct: open backend in read-only mode if possible Ahmad Fatoum
2023-01-16 14:23 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox