1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2019 Linaro Limited. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Sumit Garg <sumit.garg@linaro.org>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * fsync04
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * It basically tests fsync() to sync test file having large dirty file pages
11f08c3bdfSopenharmony_ci * to block device. Also, it tests all supported filesystems on a test block
12f08c3bdfSopenharmony_ci * device.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#define _GNU_SOURCE
16f08c3bdfSopenharmony_ci#include <errno.h>
17f08c3bdfSopenharmony_ci#include <stdlib.h>
18f08c3bdfSopenharmony_ci#include <stdio.h>
19f08c3bdfSopenharmony_ci#include <sys/types.h>
20f08c3bdfSopenharmony_ci#include "tst_test.h"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#define MNTPOINT	"mnt_point"
23f08c3bdfSopenharmony_ci#define FNAME		MNTPOINT"/test"
24f08c3bdfSopenharmony_ci#define FILE_SIZE_MB	32
25f08c3bdfSopenharmony_ci#define FILE_SIZE	(FILE_SIZE_MB * TST_MB)
26f08c3bdfSopenharmony_ci#define MODE		0644
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic void verify_fsync(void)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	int fd;
31f08c3bdfSopenharmony_ci	unsigned long written;
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(FNAME, O_RDWR|O_CREAT, MODE);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	tst_dev_sync(fd);
36f08c3bdfSopenharmony_ci	tst_dev_bytes_written(tst_device->dev);
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	tst_fill_fd(fd, 0, TST_MB, FILE_SIZE_MB);
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	TEST(fsync(fd));
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	if (TST_RET)
43f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TTERRNO, "fsync(fd) failed");
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	written = tst_dev_bytes_written(tst_device->dev);
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	if (written >= FILE_SIZE)
50f08c3bdfSopenharmony_ci		tst_res(TPASS, "Test file synced to device");
51f08c3bdfSopenharmony_ci	else
52f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Synced %li, expected %i", written, FILE_SIZE);
53f08c3bdfSopenharmony_ci}
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_cistatic struct tst_test test = {
56f08c3bdfSopenharmony_ci	.needs_root = 1,
57f08c3bdfSopenharmony_ci	.mount_device = 1,
58f08c3bdfSopenharmony_ci	.all_filesystems = 1,
59f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
60f08c3bdfSopenharmony_ci	.test_all = verify_fsync,
61f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *[]) {
62f08c3bdfSopenharmony_ci		"tmpfs",
63f08c3bdfSopenharmony_ci		NULL
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci};
66