Skip to content

Commit

Permalink
util.c: add fsync_close() helper, use where appropriate
Browse files Browse the repository at this point in the history
Using genimage directly on an eMMC on target, I'm seeing that the
BLKRRPART ioctl fails with EBUSY, presumably because there's still
lots of data in flight, and then subsequent parts of my bootstrap
procedure fail because the expected partitions can't be found.

Whenever we've written some data, we really should ensure that all
data has hit the disk before proceeding, and close() itself is not
synchronous.

Add a fsync_close() helper that does exactly what it says on the
tin. Use that wherever we close an fd that has been open for writing
and actually written to (i.e., no point in doing that in the
reload_partitions() function).

Currently, the return value of these close() calls are ignored, so at
least for now continue to do that, but at least we do see an error
message in case something went wrong.

Signed-off-by: Rasmus Villemoes <[email protected]>
  • Loading branch information
ravi-prevas committed Nov 1, 2024
1 parent 92cc7c5 commit 046159d
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ void debug(const char *fmt, ...)
va_end(args);
}

static int fsync_close(struct image *image, int fd)
{
int a, b;

a = fsync(fd);
if (a)
image_error(image, "fsync() failed: %s\n", strerror(errno));
b = close(fd);
if (b)
image_error(image, "close() failed: %s\n", strerror(errno));
return (a || b) ? -1 : 0;
}

/*
* printf wrapper around 'system'
*/
Expand Down Expand Up @@ -519,7 +532,7 @@ int prepare_image(struct image *image, unsigned long long size)
* than necessary.
*/
ret = ftruncate(fd, size);
close(fd);
fsync_close(image, fd);
if (ret < 0) {
ret = -errno;
image_error(image, "failed to truncate %s to %lld: %s\n",
Expand Down Expand Up @@ -634,7 +647,7 @@ int insert_image(struct image *image, struct image *sub,

out:
if (fd >= 0)
close(fd);
fsync_close(image, fd);
if (in_fd >= 0)
close(in_fd);
free(extents);
Expand Down Expand Up @@ -671,7 +684,7 @@ int insert_data(struct image *image, const void *_data, const char *outfile,
data += now;
}
err_out:
close(outf);
fsync_close(image, outf);

return ret;
}
Expand Down Expand Up @@ -709,7 +722,7 @@ int extend_file(struct image *image, size_t size)
}
ret = 0;
out:
close(f);
fsync_close(image, f);
return ret;
}

Expand Down

0 comments on commit 046159d

Please sign in to comment.