diff mbox series

[libubootenv] Always fsync file writes

Message ID 20210302184857.6618-1-brandon.maier@rockwellcollins.com
State Accepted
Headers show
Series [libubootenv] Always fsync file writes | expand

Commit Message

Brandon Maier March 2, 2021, 6:48 p.m. UTC
Currently fsync() only gets called at the end of fileprotect(), which
only occurs on mmcblk devices. I am storing a U-Boot environment inside
a fat32 filesystem, so the fsync() call does not get hit, and so the new
environment may be lost if power is lost.

Instead always fsync after the write has completed, regardless of the
device type.

Signed-off-by: Brandon Maier <brandon.maier@rockwellcollins.com>
---
 src/uboot_env.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Stefano Babic March 2, 2021, 7:07 p.m. UTC | #1
Hi Brandon,

On 02.03.21 19:48, Brandon Maier wrote:
> Currently fsync() only gets called at the end of fileprotect(), which
> only occurs on mmcblk devices. I am storing a U-Boot environment inside
> a fat32 filesystem, so the fsync() call does not get hit, and so the new
> environment may be lost if power is lost.
> 
> Instead always fsync after the write has completed, regardless of the
> device type.
> 
> Signed-off-by: Brandon Maier <brandon.maier@rockwellcollins.com>
> ---
>   src/uboot_env.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/src/uboot_env.c b/src/uboot_env.c
> index 2cc5028..30c39eb 100644
> --- a/src/uboot_env.c
> +++ b/src/uboot_env.c
> @@ -646,7 +646,6 @@ static int fileprotect(struct uboot_flash_env *dev, bool on)
>   	if(on == false){
>   		ret_int = write(fd_force_ro, &c_unprot_char, 1);
>   	} else {
> -		fsync(dev->fd);
>   		ret_int = write(fd_force_ro, &c_prot_char, 1);
>   	}
>   	close(fd_force_ro);
> @@ -688,6 +687,8 @@ static int filewrite(struct uboot_flash_env *dev, void *data)
>   		}
>   	}
>   
> +	fsync(dev->fd);
> +
>   	fileprotect(dev, true);  // no error handling, keep ret from write
>   
>   	return ret;
> 

Acked-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/src/uboot_env.c b/src/uboot_env.c
index 2cc5028..30c39eb 100644
--- a/src/uboot_env.c
+++ b/src/uboot_env.c
@@ -646,7 +646,6 @@  static int fileprotect(struct uboot_flash_env *dev, bool on)
 	if(on == false){
 		ret_int = write(fd_force_ro, &c_unprot_char, 1);
 	} else {
-		fsync(dev->fd);
 		ret_int = write(fd_force_ro, &c_prot_char, 1);
 	}
 	close(fd_force_ro);
@@ -688,6 +687,8 @@  static int filewrite(struct uboot_flash_env *dev, void *data)
 		}
 	}
 
+	fsync(dev->fd);
+
 	fileprotect(dev, true);  // no error handling, keep ret from write
 
 	return ret;