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