From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 09/10] commands: sync: add new command to flush cached writes
Date: Wed, 31 Jul 2024 10:05:09 +0200 [thread overview]
Message-ID: <20240731080510.364706-10-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240731080510.364706-1-a.fatoum@pengutronix.de>
In barebox, the dirty block cache for a given device is flushed when
closing all file descriptors to it, a cached block needs to be evicted
due to pressure or when barebox shuts down.
There are valid uses to do it explicitly however:
- When benchmarking write speed to a block device
- Before forced resets, because a regular reset would run shutdown
code that should be omitted.
Add a sync command that does this. Unlike the userspace variant, we
don't bother to implement a argument-less sync that's global. Users are
supposed to flush the files they are actually interested in, e.g.:
sync /dev/mmc2.0
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
- fix typo in commit message (Sascha)
---
commands/Kconfig | 14 ++++++++++++++
commands/Makefile | 1 +
commands/sync.c | 42 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 57 insertions(+)
create mode 100644 commands/sync.c
diff --git a/commands/Kconfig b/commands/Kconfig
index 5b512f1bbac7..a8b7037618cc 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1871,6 +1871,20 @@ config CMD_DETECT
-e bail out if one device fails to detect
-a detect all devices
+config CMD_SYNC
+ tristate
+ prompt "sync"
+ help
+ sync - flush cached writes
+
+ Usage: sync DEVICE
+
+ Synchronize cached writes to persistent storage of DEVICE
+ immediately instead of waiting for block device to be closed,
+ cache eviction or the regular barebox exit.
+
+ This command is mainly useful for benchmarking.
+
config CMD_FLASH
tristate
prompt "erase, protect and unprotect"
diff --git a/commands/Makefile b/commands/Makefile
index 4ca7ba7eb609..a9dbead4389f 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -41,6 +41,7 @@ obj-$(CONFIG_CMD_REGINFO) += reginfo.o
obj-$(CONFIG_CMD_CRC) += crc.o
obj-$(CONFIG_CMD_CLEAR) += clear.o
obj-$(CONFIG_CMD_TEST) += test.o
+obj-$(CONFIG_CMD_SYNC) += sync.o
obj-$(CONFIG_CMD_FLASH) += flash.o
obj-$(CONFIG_CMD_MEMINFO) += meminfo.o
obj-$(CONFIG_CMD_TIMEOUT) += timeout.o
diff --git a/commands/sync.c b/commands/sync.c
new file mode 100644
index 000000000000..6add67cd0876
--- /dev/null
+++ b/commands/sync.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/* sync - flushing support */
+
+#include <common.h>
+#include <command.h>
+#include <fcntl.h>
+#include <fs.h>
+
+static int do_sync(int argc, char *argv[])
+{
+ int ret, fd;
+
+ if (argc != 2)
+ return COMMAND_ERROR_USAGE;
+
+ fd = open(argv[1], O_WRONLY);
+ if (fd < 0) {
+ printf("open %s: %m\n", argv[1]);
+ return 1;
+ }
+
+ ret = flush(fd);
+
+ close(fd);
+
+ return ret;
+}
+
+BAREBOX_CMD_HELP_START(sync)
+BAREBOX_CMD_HELP_TEXT("Synchronize cached writes to persistent storage of DEVICE")
+BAREBOX_CMD_HELP_TEXT("immediately instead of waiting for block device to be closed,")
+BAREBOX_CMD_HELP_TEXT("cache eviction or the regular barebox exit.")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(sync)
+ .cmd = do_sync,
+ BAREBOX_CMD_DESC("flush cached writes")
+ BAREBOX_CMD_OPTS("DEVICE")
+ BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
+ BAREBOX_CMD_HELP(cmd_sync_help)
+BAREBOX_CMD_END
--
2.39.2
next prev parent reply other threads:[~2024-07-31 8:05 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-31 8:05 [PATCH v2 00/10] mmc: add SD/eMMC erase support Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 01/10] fs: give erase() a new erase_type parameter Ahmad Fatoum
2024-08-01 10:51 ` Sascha Hauer
2024-08-01 10:54 ` Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 02/10] cdev: factor out range identical/overlap check Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 03/10] block: factor out chunk_flush helper Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 04/10] block: allow block devices to implement the cdev erase operation Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 05/10] mci: turn bool members into bitfield in struct mci Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 06/10] mci: describe more command structure in mci.h Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 07/10] mci: core: use CONFIG_MCI_WRITE, not CONFIG_BLOCK_WRITE Ahmad Fatoum
2024-07-31 8:05 ` [PATCH v2 08/10] mci: add support for discarding write blocks Ahmad Fatoum
2024-08-01 11:27 ` Sascha Hauer
2024-07-31 8:05 ` Ahmad Fatoum [this message]
2024-07-31 8:05 ` [PATCH v2 10/10] commands: blkstats: add command to print block device statistics Ahmad Fatoum
2024-08-01 10:49 ` [PATCH v2 00/10] mmc: add SD/eMMC erase support 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=20240731080510.364706-10-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