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