source: rtems/testsuites/fstests/fsjffs2gc01/init.c @ ade135d4

Last change on this file since ade135d4 was ade135d4, checked in by Sebastian Huber <sebastian.huber@…>, on Dec 19, 2016 at 9:19:44 AM

JFFS2: Add RTEMS_JFFS2_FORCE_GARBAGE_COLLECTION

Add IO control to force a garbage collection.

Update #2844.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include <tmacros.h>
20#include <fstest.h>
21
22#include <sys/stat.h>
23#include <string.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <errno.h>
27
28#include <rtems/jffs2.h>
29
30const char rtems_test_name[] = "FSJFFS2GC 1";
31
32static const rtems_jffs2_info info_initial = {
33  .flash_size = 131072,
34  .flash_blocks = 8,
35  .flash_block_size = 16384,
36  .used_size = 96,
37  .dirty_size = 0,
38  .wasted_size = 0,
39  .free_size = 130976,
40  .bad_size = 0,
41  .clean_blocks = 0,
42  .dirty_blocks = 0,
43  .erasable_blocks = 0,
44  .free_blocks = 8,
45  .bad_blocks = 0
46};
47
48static const rtems_jffs2_info info_big_created = {
49  .flash_size = 131072,
50  .flash_blocks = 8,
51  .flash_block_size = 16384,
52  .used_size = 22292,
53  .dirty_size = 0,
54  .wasted_size = 164,
55  .free_size = 108616,
56  .bad_size = 0,
57  .clean_blocks = 1,
58  .dirty_blocks = 0,
59  .erasable_blocks = 0,
60  .free_blocks = 7,
61  .bad_blocks = 0
62};
63
64static const rtems_jffs2_info info_big_removed = {
65  .flash_size = 131072,
66  .flash_blocks = 8,
67  .flash_block_size = 16384,
68  .used_size = 72,
69  .dirty_size = 16384,
70  .wasted_size = 6000,
71  .free_size = 108616,
72  .bad_size = 0,
73  .clean_blocks = 0,
74  .dirty_blocks = 0,
75  .erasable_blocks = 1,
76  .free_blocks = 7,
77  .bad_blocks = 0
78};
79
80static const rtems_jffs2_info info_more_files_created = {
81  .flash_size = 131072,
82  .flash_blocks = 8,
83  .flash_block_size = 16384,
84  .used_size = 54896,
85  .dirty_size = 23336,
86  .wasted_size = 36,
87  .free_size = 52804,
88  .bad_size = 0,
89  .clean_blocks = 2,
90  .dirty_blocks = 1,
91  .erasable_blocks = 1,
92  .free_blocks = 4,
93  .bad_blocks = 0
94};
95
96static const rtems_jffs2_info info_some_files_removed = {
97  .flash_size = 131072,
98  .flash_blocks = 8,
99  .flash_block_size = 16384,
100  .used_size = 23528,
101  .dirty_size = 47372,
102  .wasted_size = 7368,
103  .free_size = 52804,
104  .bad_size = 0,
105  .clean_blocks = 0,
106  .dirty_blocks = 3,
107  .erasable_blocks = 1,
108  .free_blocks = 4,
109  .bad_blocks = 0
110};
111
112static const rtems_jffs2_info info_after_first_gc = {
113  .flash_size = 131072,
114  .flash_blocks = 8,
115  .flash_block_size = 16384,
116  .used_size = 23540,
117  .dirty_size = 30988,
118  .wasted_size = 7368,
119  .free_size = 69176,
120  .bad_size = 0,
121  .clean_blocks = 0,
122  .dirty_blocks = 3,
123  .erasable_blocks = 0,
124  .free_blocks = 5,
125  .bad_blocks = 0
126};
127
128static const rtems_jffs2_info info_after_excessive_gc = {
129  .flash_size = 131072,
130  .flash_blocks = 8,
131  .flash_block_size = 16384,
132  .used_size = 7224,
133  .dirty_size = 0,
134  .wasted_size = 12,
135  .free_size = 123836,
136  .bad_size = 0,
137  .clean_blocks = 0,
138  .dirty_blocks = 0,
139  .erasable_blocks = 0,
140  .free_blocks = 8,
141  .bad_blocks = 0
142};
143
144static char big[] = "big";
145
146static const char * const more[] = {
147  "1",
148  "2",
149  "3",
150  "4",
151  "5",
152  "6",
153  "7"
154};
155
156#define ASSERT_INFO(a, b) do { \
157  rv = ioctl(fd, RTEMS_JFFS2_GET_INFO, &info); \
158  rtems_test_assert(rv == 0); \
159  rtems_test_assert(memcmp(a, b, sizeof(*a)) == 0); \
160} while (0)
161
162static const mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
163
164static char keg[523];
165
166static uint32_t simple_random(uint32_t v)
167{
168  v *= 1664525;
169  v += 1013904223;
170
171  return v;
172}
173
174static void init_keg(void)
175{
176  size_t i;
177  uint32_t v;
178
179  v = 123;
180  for (i = 0; i < sizeof(keg); ++i) {
181    v = simple_random(v);
182    keg[i] = (uint8_t) (v >> 23);
183  }
184}
185
186static void write_kegs(int fd, int kegs)
187{
188  int i;
189
190  for (i = 0; i < kegs; ++i) {
191    ssize_t n;
192
193    n = write(fd, &keg[0], sizeof(keg));
194    rtems_test_assert(n == (ssize_t) sizeof(keg));
195  }
196}
197
198static void create_big_file(void)
199{
200  int fd;
201  int rv;
202
203  fd = open(&big[0], O_WRONLY | O_TRUNC | O_CREAT, mode);
204  rtems_test_assert(fd >= 0);
205
206  write_kegs(fd, 37);
207
208  rv = close(fd);
209  rtems_test_assert(rv == 0);
210}
211
212static void remove_big_file(void)
213{
214  int rv;
215
216  rv = unlink(&big[0]);
217  rtems_test_assert(rv == 0);
218}
219
220static void create_more_files(void)
221{
222  int fds[RTEMS_ARRAY_SIZE(more)];
223  int rv;
224  size_t i;
225  int j;
226
227  for (i = 0; i < RTEMS_ARRAY_SIZE(fds); ++i) {
228    fds[i] = open(more[i], O_WRONLY | O_TRUNC | O_CREAT, mode);
229    rtems_test_assert(fds[i] >= 0);
230  }
231
232  for (j = 0; j < 13; ++j) {
233    for (i = 0; i < RTEMS_ARRAY_SIZE(fds); ++i) {
234      write_kegs(fds[i], 1);
235    }
236  }
237
238  for (i = 0; i < RTEMS_ARRAY_SIZE(fds); ++i) {
239    rv = close(fds[i]);
240    rtems_test_assert(rv == 0);
241  }
242}
243
244static void remove_some_files(void)
245{
246  size_t i;
247
248  for (i = 0; i < RTEMS_ARRAY_SIZE(more); i += 2) {
249    int rv;
250
251    rv = unlink(more[i]);
252    rtems_test_assert(rv == 0);
253  }
254}
255
256void test(void)
257{
258  int fd;
259  int rv;
260  int counter;
261  rtems_jffs2_info info;
262
263  init_keg();
264
265  fd = open("/", O_RDONLY);
266  rtems_test_assert(fd >= 0);
267
268  ASSERT_INFO(&info, &info_initial);
269
270  create_big_file();
271  ASSERT_INFO(&info, &info_big_created);
272
273  remove_big_file();
274  ASSERT_INFO(&info, &info_big_removed);
275
276  create_more_files();
277  ASSERT_INFO(&info, &info_more_files_created);
278
279  remove_some_files();
280  ASSERT_INFO(&info, &info_some_files_removed);
281
282  rv = ioctl(fd, RTEMS_JFFS2_FORCE_GARBAGE_COLLECTION);
283  rtems_test_assert(rv == 0);
284  ASSERT_INFO(&info, &info_after_first_gc);
285
286  counter = 0;
287
288  while (true) {
289    errno = ENXIO;
290    rv = ioctl(fd, RTEMS_JFFS2_FORCE_GARBAGE_COLLECTION);
291    if (rv == -1) {
292      rtems_test_assert(errno == EIO);
293      break;
294    }
295
296    ++counter;
297    rtems_test_assert(rv == 0);
298  }
299
300  rtems_test_assert(counter == 19);
301
302  rv = ioctl(fd, RTEMS_JFFS2_GET_INFO, &info);
303  rtems_test_assert(rv == 0);
304  ASSERT_INFO(&info, &info_after_excessive_gc);
305
306  rv = close(fd);
307  rtems_test_assert(rv == 0);
308}
Note: See TracBrowser for help on using the repository browser.