xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fdatasync/fdatasync03.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  * fdatasync03
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * It basically tests fdatasync() to sync test file data having large dirty
11*49cdfc7eSAndroid Build Coastguard Worker  * file pages to block device. Also, it tests all supported filesystems on a
12*49cdfc7eSAndroid Build Coastguard Worker  * test 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 <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_fdatasync(void)28*49cdfc7eSAndroid Build Coastguard Worker static void verify_fdatasync(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 	TEST(fdatasync(fd));
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET)
43*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TTERRNO, "fdatasync(fd) failed");
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	written = tst_dev_bytes_written(tst_device->dev);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	if (written >= FILE_SIZE)
50*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Test file data synced to device");
51*49cdfc7eSAndroid Build Coastguard Worker 	else
52*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Synced %li, expected %i", written, FILE_SIZE);
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
56*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
57*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
58*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
59*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
60*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_fdatasync,
61*49cdfc7eSAndroid Build Coastguard Worker 	.skip_filesystems = (const char *[]) {
62*49cdfc7eSAndroid Build Coastguard Worker 		"tmpfs",
63*49cdfc7eSAndroid Build Coastguard Worker 		NULL
64*49cdfc7eSAndroid Build Coastguard Worker 	}
65*49cdfc7eSAndroid Build Coastguard Worker };
66