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