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 * Test syncfs
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * It basically tests syncfs() to sync filesystem having large dirty file
11f08c3bdfSopenharmony_ci * pages to block device. Also, it tests all supported filesystems on a test
12f08c3bdfSopenharmony_ci * block device.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#define _GNU_SOURCE
16f08c3bdfSopenharmony_ci#include <stdlib.h>
17f08c3bdfSopenharmony_ci#include <stdio.h>
18f08c3bdfSopenharmony_ci#include <sys/types.h>
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci#include "lapi/syncfs.h"
21f08c3bdfSopenharmony_ci#include "check_syncfs.h"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#define MNTPOINT	"mnt_point"
24f08c3bdfSopenharmony_ci#define FNAME		MNTPOINT"/test"
25f08c3bdfSopenharmony_ci#define FILE_SIZE_MB	32
26f08c3bdfSopenharmony_ci#define FILE_SIZE	(FILE_SIZE_MB * TST_MB)
27f08c3bdfSopenharmony_ci#define MODE		0644
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic void verify_syncfs(void)
30f08c3bdfSopenharmony_ci{
31f08c3bdfSopenharmony_ci	int fd;
32f08c3bdfSopenharmony_ci	unsigned long written;
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(FNAME, O_RDWR|O_CREAT, MODE);
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	tst_dev_sync(fd);
37f08c3bdfSopenharmony_ci	tst_dev_bytes_written(tst_device->dev);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	tst_fill_fd(fd, 0, TST_MB, FILE_SIZE_MB);
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	TEST(syncfs(fd));
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	if (TST_RET)
44f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TTERRNO, "syncfs(fd) failed");
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	written = tst_dev_bytes_written(tst_device->dev);
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	if (written >= FILE_SIZE)
51f08c3bdfSopenharmony_ci		tst_res(TPASS, "Test filesystem synced to device");
52f08c3bdfSopenharmony_ci	else
53f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Synced %li, expected %i", written, FILE_SIZE);
54f08c3bdfSopenharmony_ci}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic void setup(void)
57f08c3bdfSopenharmony_ci{
58f08c3bdfSopenharmony_ci	check_syncfs();
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic struct tst_test test = {
62f08c3bdfSopenharmony_ci	.needs_root = 1,
63f08c3bdfSopenharmony_ci	.mount_device = 1,
64f08c3bdfSopenharmony_ci	.all_filesystems = 1,
65f08c3bdfSopenharmony_ci	.skip_filesystems = (const char*[]) {
66f08c3bdfSopenharmony_ci		"tmpfs",
67f08c3bdfSopenharmony_ci		NULL
68f08c3bdfSopenharmony_ci	},
69f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
70f08c3bdfSopenharmony_ci	.setup = setup,
71f08c3bdfSopenharmony_ci	.test_all = verify_syncfs,
72f08c3bdfSopenharmony_ci};
73