xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fallocate/fallocate03.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 /*
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2007
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Sharyathi Nagesh <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2008-2017
7*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2024 SUSE LLC Andrea Manzini <[email protected]>
8*49cdfc7eSAndroid Build Coastguard Worker  */
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker /*\
11*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
12*49cdfc7eSAndroid Build Coastguard Worker  * Test fallocate() on sparse file for different offsets, with a total of 8 test cases
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define BLOCKS_WRITTEN 12
21*49cdfc7eSAndroid Build Coastguard Worker #define HOLE_SIZE_IN_BLOCKS 12
22*49cdfc7eSAndroid Build Coastguard Worker #define DEFAULT_MODE 0
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static int fd;
25*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
26*49cdfc7eSAndroid Build Coastguard Worker 	int mode;
27*49cdfc7eSAndroid Build Coastguard Worker 	loff_t offset;
28*49cdfc7eSAndroid Build Coastguard Worker } test_cases[] = {
29*49cdfc7eSAndroid Build Coastguard Worker 	{DEFAULT_MODE, 2},
30*49cdfc7eSAndroid Build Coastguard Worker 	{DEFAULT_MODE, BLOCKS_WRITTEN},
31*49cdfc7eSAndroid Build Coastguard Worker 	{DEFAULT_MODE, BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS / 2 - 1},
32*49cdfc7eSAndroid Build Coastguard Worker 	{DEFAULT_MODE, BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS + 1},
33*49cdfc7eSAndroid Build Coastguard Worker 	{FALLOC_FL_KEEP_SIZE, 2},
34*49cdfc7eSAndroid Build Coastguard Worker 	{FALLOC_FL_KEEP_SIZE, BLOCKS_WRITTEN},
35*49cdfc7eSAndroid Build Coastguard Worker 	{FALLOC_FL_KEEP_SIZE, BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS / 2 + 1},
36*49cdfc7eSAndroid Build Coastguard Worker 	{FALLOC_FL_KEEP_SIZE, BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS + 2}
37*49cdfc7eSAndroid Build Coastguard Worker };
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static int block_size;
40*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)41*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker 	if (fd)
44*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
write_data_to_file(int buf_size)47*49cdfc7eSAndroid Build Coastguard Worker static void write_data_to_file(int buf_size)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	char buf[buf_size + 1];
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	for (int blocks = 0; blocks < BLOCKS_WRITTEN; blocks++) {
52*49cdfc7eSAndroid Build Coastguard Worker 		for (int i = 0; i < buf_size; i++)
53*49cdfc7eSAndroid Build Coastguard Worker 			buf[i] = 'A' + (i % 26);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 		buf[buf_size] = '\0';
56*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, buf_size);
57*49cdfc7eSAndroid Build Coastguard Worker 	}
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)60*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker 	char fname[NAME_MAX];
63*49cdfc7eSAndroid Build Coastguard Worker 	struct stat file_stat;
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(fname, "tfile_sparse_%d", getpid());
66*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FSTAT(fd, &file_stat);
69*49cdfc7eSAndroid Build Coastguard Worker 	block_size = (int)file_stat.st_blksize;
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	write_data_to_file(block_size);
72*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(fd, block_size * (BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS), SEEK_SET);
73*49cdfc7eSAndroid Build Coastguard Worker 	write_data_to_file(block_size);
74*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(fd, 0, SEEK_SET);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 
verify_fallocate(unsigned int nr)78*49cdfc7eSAndroid Build Coastguard Worker static void verify_fallocate(unsigned int nr)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tc = &test_cases[nr];
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(
83*49cdfc7eSAndroid Build Coastguard Worker 		fallocate(fd, tc->mode, tc->offset * block_size, block_size),
84*49cdfc7eSAndroid Build Coastguard Worker 		"fallocate(fd, %s, %ld, %d)",
85*49cdfc7eSAndroid Build Coastguard Worker 		tc->mode ? "FALLOC_FL_KEEP_SIZE" : "DEFAULT_MODE",
86*49cdfc7eSAndroid Build Coastguard Worker 		tc->offset * block_size, block_size);
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
90*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
91*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
92*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_fallocate,
93*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
94*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(test_cases)
95*49cdfc7eSAndroid Build Coastguard Worker };
96