source: rtems/testsuites/fstests/fsjffs2gc01/init.c @ 07c833f

Last change on this file since 07c833f was 07c833f, checked in by Sebastian Huber <sebastian.huber@…>, on Dec 19, 2016 at 8:54:16 AM

JFFS2: Add RTEMS_JFFS2_GET_INFO

Add IO control RTEMS_JFFS2_GET_INFO to get some JFFS2 filesystem
instance information.

Update #2844.

  • Property mode set to 100644
File size: 4.5 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 char big[] = "big";
113
114static const char * const more[] = {
115  "1",
116  "2",
117  "3",
118  "4",
119  "5",
120  "6",
121  "7"
122};
123
124#define ASSERT_INFO(a, b) do { \
125  rv = ioctl(fd, RTEMS_JFFS2_GET_INFO, &info); \
126  rtems_test_assert(rv == 0); \
127  rtems_test_assert(memcmp(a, b, sizeof(*a)) == 0); \
128} while (0)
129
130static const mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
131
132static char keg[523];
133
134static uint32_t simple_random(uint32_t v)
135{
136  v *= 1664525;
137  v += 1013904223;
138
139  return v;
140}
141
142static void init_keg(void)
143{
144  size_t i;
145  uint32_t v;
146
147  v = 123;
148  for (i = 0; i < sizeof(keg); ++i) {
149    v = simple_random(v);
150    keg[i] = (uint8_t) (v >> 23);
151  }
152}
153
154static void write_kegs(int fd, int kegs)
155{
156  int i;
157
158  for (i = 0; i < kegs; ++i) {
159    ssize_t n;
160
161    n = write(fd, &keg[0], sizeof(keg));
162    rtems_test_assert(n == (ssize_t) sizeof(keg));
163  }
164}
165
166static void create_big_file(void)
167{
168  int fd;
169  int rv;
170
171  fd = open(&big[0], O_WRONLY | O_TRUNC | O_CREAT, mode);
172  rtems_test_assert(fd >= 0);
173
174  write_kegs(fd, 37);
175
176  rv = close(fd);
177  rtems_test_assert(rv == 0);
178}
179
180static void remove_big_file(void)
181{
182  int rv;
183
184  rv = unlink(&big[0]);
185  rtems_test_assert(rv == 0);
186}
187
188static void create_more_files(void)
189{
190  int fds[RTEMS_ARRAY_SIZE(more)];
191  int rv;
192  size_t i;
193  int j;
194
195  for (i = 0; i < RTEMS_ARRAY_SIZE(fds); ++i) {
196    fds[i] = open(more[i], O_WRONLY | O_TRUNC | O_CREAT, mode);
197    rtems_test_assert(fds[i] >= 0);
198  }
199
200  for (j = 0; j < 13; ++j) {
201    for (i = 0; i < RTEMS_ARRAY_SIZE(fds); ++i) {
202      write_kegs(fds[i], 1);
203    }
204  }
205
206  for (i = 0; i < RTEMS_ARRAY_SIZE(fds); ++i) {
207    rv = close(fds[i]);
208    rtems_test_assert(rv == 0);
209  }
210}
211
212static void remove_some_files(void)
213{
214  size_t i;
215
216  for (i = 0; i < RTEMS_ARRAY_SIZE(more); i += 2) {
217    int rv;
218
219    rv = unlink(more[i]);
220    rtems_test_assert(rv == 0);
221  }
222}
223
224void test(void)
225{
226  int fd;
227  int rv;
228  rtems_jffs2_info info;
229
230  init_keg();
231
232  fd = open("/", O_RDONLY);
233  rtems_test_assert(fd >= 0);
234
235  ASSERT_INFO(&info, &info_initial);
236
237  create_big_file();
238  ASSERT_INFO(&info, &info_big_created);
239
240  remove_big_file();
241  ASSERT_INFO(&info, &info_big_removed);
242
243  create_more_files();
244  ASSERT_INFO(&info, &info_more_files_created);
245
246  remove_some_files();
247  ASSERT_INFO(&info, &info_some_files_removed);
248
249  rv = close(fd);
250  rtems_test_assert(rv == 0);
251}
Note: See TracBrowser for help on using the repository browser.