1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2021 Google
8 * Flush journal log and checkpoint journal for ext4 file system and
9 * optionally, issue discard or zeroout for the journal log blocks.
12 * 1) mount point for device
14 * set --erase=discard to enable discarding journal blocks
15 * set --erase=zeroout to enable zero-filling journal blocks
16 * set --dry-run flag to only perform input checking
19 #include <sys/ioctl.h>
29 #if !defined(EXT4_IOC_CHECKPOINT)
30 #define EXT4_IOC_CHECKPOINT _IOW('f', 43, __u32)
33 #if !defined(EXT4_IOC_CHECKPOINT_FLAG_DISCARD)
34 #define EXT4_IOC_CHECKPOINT_FLAG_DISCARD 1
35 #define EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT 2
36 #define EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN 4
39 int main(int argc, char** argv) {
40 int fd, c, ret = 0, option_index = 0;
42 unsigned int flags = 0;
44 static struct option long_options[] =
46 {"dry-run", no_argument, 0, 'd'},
47 {"erase", required_argument, 0, 'e'},
51 /* get optional flags */
52 while ((c = getopt_long(argc, argv, "de:", long_options,
53 &option_index)) != -1) {
56 flags |= EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN;
59 if (strcmp(optarg, "discard") == 0) {
60 flags |= EXT4_IOC_CHECKPOINT_FLAG_DISCARD;
61 } else if (strcmp(optarg, "zeroout") == 0) {
62 flags |= EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT;
64 fprintf(stderr, "Error: invalid erase option\n");
73 if (optind != argc - 1) {
74 fprintf(stderr, "Error: invalid number of arguments\n");
78 /* get fd to file system */
79 rpath = realpath(argv[optind], NULL);
80 fd = open(rpath, O_RDONLY);
84 fprintf(stderr, "Error: unable to open device %s: %s\n",
85 argv[optind], strerror(errno));
89 ret = ioctl(fd, EXT4_IOC_CHECKPOINT, &flags);
92 fprintf(stderr, "checkpoint ioctl returned error: %s\n", strerror(errno));