1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
4 * Copyright (c) 2019 SUSE LLC <mdoucha@suse.cz>
5 */
6
7 /*
8 * Tests that writing to fallocated file works when filesystem is full.
9 * Test scenario:
10 * - fallocate() some empty blocks
11 * - fill the filesystem
12 * - write() into the preallocated space
13 * - try to fallocate() more blocks until we get ENOSPC
14 * - write() into the extra allocated space
15 * - deallocate part of the file
16 * - write() to the end of file to check that some blocks were freed
17 */
18
19 #define _GNU_SOURCE
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <fcntl.h>
25 #include "tst_test.h"
26 #include "lapi/fallocate.h"
27
28 #define MNTPOINT "mntpoint"
29 #define FALLOCATE_BLOCKS 16
30 #define DEALLOCATE_BLOCKS 4
31 #define TESTED_FLAGS "fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)"
32
33 static int fd;
34 static char *buf;
35 static blksize_t blocksize;
36 static long bufsize;
37
setup(void)38 static void setup(void)
39 {
40 struct stat statbuf;
41
42 fd = SAFE_OPEN(MNTPOINT "/test_file", O_WRONLY | O_CREAT);
43
44 /*
45 * Use real FS block size, otherwise fallocate() call will test
46 * different things on different platforms
47 */
48 SAFE_FSTAT(fd, &statbuf);
49 blocksize = statbuf.st_blksize;
50 bufsize = FALLOCATE_BLOCKS * blocksize;
51 buf = SAFE_MALLOC(bufsize);
52 }
53
run(void)54 static void run(void)
55 {
56 long extsize, tmp;
57
58 TEST(fallocate(fd, 0, 0, bufsize));
59
60 if (TST_RET) {
61 if (TST_ERR == ENOTSUP)
62 tst_brk(TCONF | TTERRNO, "fallocate() not supported");
63
64 tst_brk(TBROK | TTERRNO, "fallocate(fd, 0, 0, %ld)", bufsize);
65 }
66
67 tst_fill_fs(MNTPOINT, 1);
68
69 TEST(write(fd, buf, bufsize));
70
71 if (TST_RET < 0)
72 tst_res(TFAIL | TTERRNO, "write() failed unexpectedly");
73 else if (TST_RET != bufsize)
74 tst_res(TFAIL, "Short write(): %ld bytes (expected %zu)",
75 TST_RET, bufsize);
76 else
77 tst_res(TPASS, "write() wrote %ld bytes", TST_RET);
78
79 /*
80 * Some file systems may still have a few extra blocks that can be
81 * allocated.
82 */
83 for (TST_RET = 0, extsize = 0; !TST_RET; extsize += blocksize)
84 TEST(fallocate(fd, 0, bufsize + extsize, blocksize));
85
86 if (TST_RET != -1) {
87 tst_res(TFAIL, "Invalid fallocate() return value %ld", TST_RET);
88 return;
89 }
90
91 if (TST_ERR != ENOSPC) {
92 tst_res(TFAIL | TTERRNO, "fallocate() should fail with ENOSPC");
93 return;
94 }
95
96 /* The loop above always counts 1 more block than it should. */
97 extsize -= blocksize;
98 tst_res(TINFO, "fallocate()d %ld extra blocks on full FS",
99 extsize / blocksize);
100
101 for (tmp = extsize; tmp > 0; tmp -= TST_RET) {
102 TEST(write(fd, buf, MIN(bufsize, tmp)));
103
104 if (TST_RET <= 0) {
105 tst_res(TFAIL | TTERRNO, "write() failed unexpectedly");
106 return;
107 }
108 }
109
110 tst_res(TPASS, "fallocate() on full FS");
111
112 /* Btrfs deallocates only complete extents, not individual blocks */
113 if (!strcmp(tst_device->fs_type, "btrfs"))
114 tmp = bufsize + extsize;
115 else
116 tmp = DEALLOCATE_BLOCKS * blocksize;
117
118 TEST(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, tmp));
119
120 if (TST_RET == -1) {
121 if (TST_ERR == ENOTSUP)
122 tst_brk(TCONF, TESTED_FLAGS);
123
124 tst_brk(TBROK | TTERRNO, TESTED_FLAGS);
125 }
126 tst_res(TPASS, TESTED_FLAGS);
127
128 TEST(write(fd, buf, 10));
129 if (TST_RET == -1)
130 tst_res(TFAIL | TTERRNO, "write()");
131 else
132 tst_res(TPASS, "write()");
133
134 /* TODO: wipe the test device here to allow looping with -i/-I */
135 }
136
cleanup(void)137 static void cleanup(void)
138 {
139 if (fd > 0)
140 SAFE_CLOSE(fd);
141
142 free(buf);
143 }
144
145 static struct tst_test test = {
146 .needs_root = 1,
147 .mount_device = 1,
148 .dev_min_size = 512,
149 .mntpoint = MNTPOINT,
150 .all_filesystems = 1,
151 .setup = setup,
152 .cleanup = cleanup,
153 .test_all = run,
154 };
155