1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 2016 embedded brains GmbH & Co. KG |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #ifdef HAVE_CONFIG_H |
---|
29 | #include "config.h" |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <tmacros.h> |
---|
33 | #include <fstest.h> |
---|
34 | |
---|
35 | #include <sys/stat.h> |
---|
36 | #include <string.h> |
---|
37 | #include <fcntl.h> |
---|
38 | #include <unistd.h> |
---|
39 | #include <errno.h> |
---|
40 | |
---|
41 | #include <rtems/jffs2.h> |
---|
42 | |
---|
43 | const char rtems_test_name[] = "FSJFFS2GC 1"; |
---|
44 | |
---|
45 | static const rtems_jffs2_info info_initial = { |
---|
46 | .flash_size = 131072, |
---|
47 | .flash_blocks = 8, |
---|
48 | .flash_block_size = 16384, |
---|
49 | .used_size = 96, |
---|
50 | .dirty_size = 0, |
---|
51 | .wasted_size = 0, |
---|
52 | .free_size = 130976, |
---|
53 | .bad_size = 0, |
---|
54 | .clean_blocks = 0, |
---|
55 | .dirty_blocks = 0, |
---|
56 | .erasable_blocks = 0, |
---|
57 | .free_blocks = 8, |
---|
58 | .bad_blocks = 0 |
---|
59 | }; |
---|
60 | |
---|
61 | static const rtems_jffs2_info info_big_created = { |
---|
62 | .flash_size = 131072, |
---|
63 | .flash_blocks = 8, |
---|
64 | .flash_block_size = 16384, |
---|
65 | .used_size = 22292, |
---|
66 | .dirty_size = 0, |
---|
67 | .wasted_size = 164, |
---|
68 | .free_size = 108616, |
---|
69 | .bad_size = 0, |
---|
70 | .clean_blocks = 1, |
---|
71 | .dirty_blocks = 0, |
---|
72 | .erasable_blocks = 0, |
---|
73 | .free_blocks = 7, |
---|
74 | .bad_blocks = 0 |
---|
75 | }; |
---|
76 | |
---|
77 | static const rtems_jffs2_info info_big_removed = { |
---|
78 | .flash_size = 131072, |
---|
79 | .flash_blocks = 8, |
---|
80 | .flash_block_size = 16384, |
---|
81 | .used_size = 72, |
---|
82 | .dirty_size = 16384, |
---|
83 | .wasted_size = 6000, |
---|
84 | .free_size = 108616, |
---|
85 | .bad_size = 0, |
---|
86 | .clean_blocks = 0, |
---|
87 | .dirty_blocks = 0, |
---|
88 | .erasable_blocks = 1, |
---|
89 | .free_blocks = 7, |
---|
90 | .bad_blocks = 0 |
---|
91 | }; |
---|
92 | |
---|
93 | static const rtems_jffs2_info info_more_files_created = { |
---|
94 | .flash_size = 131072, |
---|
95 | .flash_blocks = 8, |
---|
96 | .flash_block_size = 16384, |
---|
97 | .used_size = 54896, |
---|
98 | .dirty_size = 23336, |
---|
99 | .wasted_size = 36, |
---|
100 | .free_size = 52804, |
---|
101 | .bad_size = 0, |
---|
102 | .clean_blocks = 2, |
---|
103 | .dirty_blocks = 1, |
---|
104 | .erasable_blocks = 1, |
---|
105 | .free_blocks = 4, |
---|
106 | .bad_blocks = 0 |
---|
107 | }; |
---|
108 | |
---|
109 | static const rtems_jffs2_info info_some_files_removed = { |
---|
110 | .flash_size = 131072, |
---|
111 | .flash_blocks = 8, |
---|
112 | .flash_block_size = 16384, |
---|
113 | .used_size = 23528, |
---|
114 | .dirty_size = 47372, |
---|
115 | .wasted_size = 7368, |
---|
116 | .free_size = 52804, |
---|
117 | .bad_size = 0, |
---|
118 | .clean_blocks = 0, |
---|
119 | .dirty_blocks = 3, |
---|
120 | .erasable_blocks = 1, |
---|
121 | .free_blocks = 4, |
---|
122 | .bad_blocks = 0 |
---|
123 | }; |
---|
124 | |
---|
125 | static const rtems_jffs2_info info_after_first_gc = { |
---|
126 | .flash_size = 131072, |
---|
127 | .flash_blocks = 8, |
---|
128 | .flash_block_size = 16384, |
---|
129 | .used_size = 23540, |
---|
130 | .dirty_size = 30988, |
---|
131 | .wasted_size = 7368, |
---|
132 | .free_size = 69176, |
---|
133 | .bad_size = 0, |
---|
134 | .clean_blocks = 0, |
---|
135 | .dirty_blocks = 3, |
---|
136 | .erasable_blocks = 0, |
---|
137 | .free_blocks = 5, |
---|
138 | .bad_blocks = 0 |
---|
139 | }; |
---|
140 | |
---|
141 | static const rtems_jffs2_info info_after_excessive_gc = { |
---|
142 | .flash_size = 131072, |
---|
143 | .flash_blocks = 8, |
---|
144 | .flash_block_size = 16384, |
---|
145 | .used_size = 7224, |
---|
146 | .dirty_size = 0, |
---|
147 | .wasted_size = 12, |
---|
148 | .free_size = 123836, |
---|
149 | .bad_size = 0, |
---|
150 | .clean_blocks = 0, |
---|
151 | .dirty_blocks = 0, |
---|
152 | .erasable_blocks = 0, |
---|
153 | .free_blocks = 8, |
---|
154 | .bad_blocks = 0 |
---|
155 | }; |
---|
156 | |
---|
157 | static char big[] = "big"; |
---|
158 | |
---|
159 | static const char * const more[] = { |
---|
160 | "1", |
---|
161 | "2", |
---|
162 | "3", |
---|
163 | "4", |
---|
164 | "5", |
---|
165 | "6", |
---|
166 | "7" |
---|
167 | }; |
---|
168 | |
---|
169 | #define ASSERT_INFO(a, b) do { \ |
---|
170 | rv = ioctl(fd, RTEMS_JFFS2_GET_INFO, &info); \ |
---|
171 | rtems_test_assert(rv == 0); \ |
---|
172 | rtems_test_assert(memcmp(a, b, sizeof(*a)) == 0); \ |
---|
173 | } while (0) |
---|
174 | |
---|
175 | static const mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; |
---|
176 | |
---|
177 | static char keg[523]; |
---|
178 | |
---|
179 | static uint32_t simple_random(uint32_t v) |
---|
180 | { |
---|
181 | v *= 1664525; |
---|
182 | v += 1013904223; |
---|
183 | |
---|
184 | return v; |
---|
185 | } |
---|
186 | |
---|
187 | static void init_keg(void) |
---|
188 | { |
---|
189 | size_t i; |
---|
190 | uint32_t v; |
---|
191 | |
---|
192 | v = 123; |
---|
193 | for (i = 0; i < sizeof(keg); ++i) { |
---|
194 | v = simple_random(v); |
---|
195 | keg[i] = (uint8_t) (v >> 23); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | static void write_kegs(int fd, int kegs) |
---|
200 | { |
---|
201 | int i; |
---|
202 | |
---|
203 | for (i = 0; i < kegs; ++i) { |
---|
204 | ssize_t n; |
---|
205 | |
---|
206 | n = write(fd, &keg[0], sizeof(keg)); |
---|
207 | rtems_test_assert(n == (ssize_t) sizeof(keg)); |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | static void create_big_file(void) |
---|
212 | { |
---|
213 | int fd; |
---|
214 | int rv; |
---|
215 | |
---|
216 | fd = open(&big[0], O_WRONLY | O_TRUNC | O_CREAT, mode); |
---|
217 | rtems_test_assert(fd >= 0); |
---|
218 | |
---|
219 | write_kegs(fd, 37); |
---|
220 | |
---|
221 | rv = close(fd); |
---|
222 | rtems_test_assert(rv == 0); |
---|
223 | } |
---|
224 | |
---|
225 | static void remove_big_file(void) |
---|
226 | { |
---|
227 | int rv; |
---|
228 | |
---|
229 | rv = unlink(&big[0]); |
---|
230 | rtems_test_assert(rv == 0); |
---|
231 | } |
---|
232 | |
---|
233 | static void create_more_files(void) |
---|
234 | { |
---|
235 | int fds[RTEMS_ARRAY_SIZE(more)]; |
---|
236 | int rv; |
---|
237 | size_t i; |
---|
238 | int j; |
---|
239 | |
---|
240 | for (i = 0; i < RTEMS_ARRAY_SIZE(fds); ++i) { |
---|
241 | fds[i] = open(more[i], O_WRONLY | O_TRUNC | O_CREAT, mode); |
---|
242 | rtems_test_assert(fds[i] >= 0); |
---|
243 | } |
---|
244 | |
---|
245 | for (j = 0; j < 13; ++j) { |
---|
246 | for (i = 0; i < RTEMS_ARRAY_SIZE(fds); ++i) { |
---|
247 | write_kegs(fds[i], 1); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | for (i = 0; i < RTEMS_ARRAY_SIZE(fds); ++i) { |
---|
252 | rv = close(fds[i]); |
---|
253 | rtems_test_assert(rv == 0); |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | static void remove_some_files(void) |
---|
258 | { |
---|
259 | size_t i; |
---|
260 | |
---|
261 | for (i = 0; i < RTEMS_ARRAY_SIZE(more); i += 2) { |
---|
262 | int rv; |
---|
263 | |
---|
264 | rv = unlink(more[i]); |
---|
265 | rtems_test_assert(rv == 0); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | void test(void) |
---|
270 | { |
---|
271 | int fd; |
---|
272 | int rv; |
---|
273 | int counter; |
---|
274 | rtems_jffs2_info info; |
---|
275 | |
---|
276 | init_keg(); |
---|
277 | |
---|
278 | /* |
---|
279 | * Ensure that jiffies != 0, to use most likely path in |
---|
280 | * jffs2_mark_node_obsolete(). |
---|
281 | */ |
---|
282 | while (rtems_clock_get_ticks_since_boot() == 0) { |
---|
283 | /* Wait */ |
---|
284 | } |
---|
285 | |
---|
286 | fd = open("/", O_RDONLY); |
---|
287 | rtems_test_assert(fd >= 0); |
---|
288 | |
---|
289 | ASSERT_INFO(&info, &info_initial); |
---|
290 | |
---|
291 | create_big_file(); |
---|
292 | ASSERT_INFO(&info, &info_big_created); |
---|
293 | |
---|
294 | remove_big_file(); |
---|
295 | ASSERT_INFO(&info, &info_big_removed); |
---|
296 | |
---|
297 | create_more_files(); |
---|
298 | ASSERT_INFO(&info, &info_more_files_created); |
---|
299 | |
---|
300 | remove_some_files(); |
---|
301 | ASSERT_INFO(&info, &info_some_files_removed); |
---|
302 | |
---|
303 | rv = ioctl(fd, RTEMS_JFFS2_ON_DEMAND_GARBAGE_COLLECTION); |
---|
304 | rtems_test_assert(rv == 0); |
---|
305 | ASSERT_INFO(&info, &info_after_first_gc); |
---|
306 | |
---|
307 | rv = ioctl(fd, RTEMS_JFFS2_ON_DEMAND_GARBAGE_COLLECTION); |
---|
308 | rtems_test_assert(rv == 0); |
---|
309 | ASSERT_INFO(&info, &info_after_first_gc); |
---|
310 | |
---|
311 | counter = 0; |
---|
312 | |
---|
313 | while (true) { |
---|
314 | errno = ENXIO; |
---|
315 | rv = ioctl(fd, RTEMS_JFFS2_FORCE_GARBAGE_COLLECTION); |
---|
316 | if (rv == -1) { |
---|
317 | rtems_test_assert(errno == EIO); |
---|
318 | break; |
---|
319 | } |
---|
320 | |
---|
321 | ++counter; |
---|
322 | rtems_test_assert(rv == 0); |
---|
323 | } |
---|
324 | |
---|
325 | rtems_test_assert(counter == 19); |
---|
326 | |
---|
327 | rv = ioctl(fd, RTEMS_JFFS2_GET_INFO, &info); |
---|
328 | rtems_test_assert(rv == 0); |
---|
329 | ASSERT_INFO(&info, &info_after_excessive_gc); |
---|
330 | |
---|
331 | rv = close(fd); |
---|
332 | rtems_test_assert(rv == 0); |
---|
333 | } |
---|