xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sync/sync01.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  * sync03
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * It basically tests sync() to sync test file having large dirty file pages
11*49cdfc7eSAndroid Build Coastguard Worker  * to block device. Also, it tests all supported filesystems on a test block
12*49cdfc7eSAndroid Build Coastguard Worker  * 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 <errno.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT	"mnt_point"
23*49cdfc7eSAndroid Build Coastguard Worker #define FNAME		MNTPOINT"/test"
24*49cdfc7eSAndroid Build Coastguard Worker #define FILE_SIZE_MB	32
25*49cdfc7eSAndroid Build Coastguard Worker #define FILE_SIZE	(FILE_SIZE_MB * TST_MB)
26*49cdfc7eSAndroid Build Coastguard Worker #define MODE		0644
27*49cdfc7eSAndroid Build Coastguard Worker 
verify_sync(void)28*49cdfc7eSAndroid Build Coastguard Worker static void verify_sync(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
31*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long written;
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(FNAME, O_RDWR|O_CREAT, MODE);
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	tst_dev_sync(fd);
36*49cdfc7eSAndroid Build Coastguard Worker 	tst_dev_bytes_written(tst_device->dev);
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	tst_fill_fd(fd, 0, TST_MB, FILE_SIZE_MB);
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	sync();
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	written = tst_dev_bytes_written(tst_device->dev);
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	if (written >= FILE_SIZE)
47*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Test file synced to device");
48*49cdfc7eSAndroid Build Coastguard Worker 	else
49*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Synced %li, expected %i", written, FILE_SIZE);
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
53*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
54*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
55*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
56*49cdfc7eSAndroid Build Coastguard Worker 	.skip_filesystems = (const char *[]) {
57*49cdfc7eSAndroid Build Coastguard Worker 		"tmpfs",
58*49cdfc7eSAndroid Build Coastguard Worker 		NULL
59*49cdfc7eSAndroid Build Coastguard Worker 	},
60*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
61*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_sync,
62*49cdfc7eSAndroid Build Coastguard Worker };
63