1 | /* |
---|
2 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Obere Lagerstr. 30 |
---|
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 <fcntl.h> |
---|
21 | #include <rtems/dosfs.h> |
---|
22 | #include <rtems/sparse-disk.h> |
---|
23 | #include <rtems/blkdev.h> |
---|
24 | #include <bsp.h> |
---|
25 | |
---|
26 | const char rtems_test_name[] = "FSDOSFSWRITE 1"; |
---|
27 | |
---|
28 | #define MAX_PATH_LENGTH 100 /* Maximum number of characters per path */ |
---|
29 | #define SECTOR_SIZE 512 /* sector size (bytes) */ |
---|
30 | #define FAT16_MAX_CLN 65525 /* maximum + 1 number of clusters for FAT16 */ |
---|
31 | #define FAT16_DEFAULT_SECTORS_PER_CLUSTER 32 /* Default number of sectors per cluster for FAT16 */ |
---|
32 | #define SECTORS_PER_CLUSTER 2 |
---|
33 | |
---|
34 | static void format_and_mount( const char *dev_name, const char *mount_dir ) |
---|
35 | { |
---|
36 | static const msdos_format_request_param_t rqdata = { |
---|
37 | .sectors_per_cluster = SECTORS_PER_CLUSTER, |
---|
38 | .quick_format = true |
---|
39 | }; |
---|
40 | |
---|
41 | int rv; |
---|
42 | |
---|
43 | |
---|
44 | rv = msdos_format( dev_name, &rqdata ); |
---|
45 | rtems_test_assert( rv == 0 ); |
---|
46 | |
---|
47 | rv = mount( dev_name, |
---|
48 | mount_dir, |
---|
49 | RTEMS_FILESYSTEM_TYPE_DOSFS, |
---|
50 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
51 | NULL ); |
---|
52 | rtems_test_assert( rv == 0 ); |
---|
53 | } |
---|
54 | |
---|
55 | static void do_fsync( const char *file ) |
---|
56 | { |
---|
57 | int rv; |
---|
58 | int fd; |
---|
59 | |
---|
60 | |
---|
61 | fd = open( file, O_RDONLY ); |
---|
62 | rtems_test_assert( fd >= 0 ); |
---|
63 | |
---|
64 | rv = fsync( fd ); |
---|
65 | rtems_test_assert( rv == 0 ); |
---|
66 | |
---|
67 | rv = close( fd ); |
---|
68 | rtems_test_assert( rv == 0 ); |
---|
69 | } |
---|
70 | |
---|
71 | static void check_block_stats( const char *dev_name, |
---|
72 | const char *mount_dir, |
---|
73 | const rtems_blkdev_stats *expected_stats ) |
---|
74 | { |
---|
75 | int fd; |
---|
76 | int rv; |
---|
77 | rtems_blkdev_stats actual_stats; |
---|
78 | |
---|
79 | |
---|
80 | do_fsync( mount_dir ); |
---|
81 | |
---|
82 | fd = open( dev_name, O_RDONLY ); |
---|
83 | rtems_test_assert( fd >= 0 ); |
---|
84 | |
---|
85 | rv = ioctl( fd, RTEMS_BLKIO_GETDEVSTATS, &actual_stats ); |
---|
86 | rtems_test_assert( rv == 0 ); |
---|
87 | rtems_test_assert( memcmp( &actual_stats, expected_stats, |
---|
88 | sizeof( actual_stats ) ) == 0 ); |
---|
89 | |
---|
90 | rv = close( fd ); |
---|
91 | rtems_test_assert( rv == 0 ); |
---|
92 | } |
---|
93 | |
---|
94 | static void reset_block_stats( const char *dev_name, const char *mount_dir ) |
---|
95 | { |
---|
96 | int fd; |
---|
97 | int rv; |
---|
98 | |
---|
99 | |
---|
100 | do_fsync( mount_dir ); |
---|
101 | |
---|
102 | fd = open( dev_name, O_RDONLY ); |
---|
103 | rtems_test_assert( fd >= 0 ); |
---|
104 | |
---|
105 | rv = ioctl( fd, RTEMS_BLKIO_PURGEDEV ); |
---|
106 | rtems_test_assert( rv == 0 ); |
---|
107 | |
---|
108 | rv = ioctl( fd, RTEMS_BLKIO_RESETDEVSTATS ); |
---|
109 | rtems_test_assert( rv == 0 ); |
---|
110 | |
---|
111 | rv = close( fd ); |
---|
112 | rtems_test_assert( rv == 0 ); |
---|
113 | } |
---|
114 | |
---|
115 | static int create_file( const char *file_name ) |
---|
116 | { |
---|
117 | mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; |
---|
118 | |
---|
119 | |
---|
120 | return creat( file_name, mode ); |
---|
121 | } |
---|
122 | |
---|
123 | static void test_normal_file_write( |
---|
124 | const char *dev_name, |
---|
125 | const char *mount_dir, |
---|
126 | const char *file_name ) |
---|
127 | { |
---|
128 | static const rtems_blkdev_stats complete_existing_block_stats = { |
---|
129 | .read_hits = 0, |
---|
130 | .read_misses = 0, |
---|
131 | .read_ahead_transfers = 0, |
---|
132 | .read_blocks = 0, |
---|
133 | .read_errors = 0, |
---|
134 | .write_transfers = 1, |
---|
135 | .write_blocks = 1, |
---|
136 | .write_errors = 0 |
---|
137 | }; |
---|
138 | static const rtems_blkdev_stats complete_new_block_stats = { |
---|
139 | .read_hits = 3, |
---|
140 | .read_misses = 2, |
---|
141 | .read_ahead_transfers = 0, |
---|
142 | .read_blocks = 2, |
---|
143 | .read_errors = 0, |
---|
144 | .write_transfers = 1, |
---|
145 | .write_blocks = 3, |
---|
146 | .write_errors = 0 |
---|
147 | }; |
---|
148 | static const rtems_blkdev_stats partial_new_block_stats = { |
---|
149 | .read_hits = 3, |
---|
150 | .read_misses = 3, |
---|
151 | .read_ahead_transfers = 0, |
---|
152 | .read_blocks = 3, |
---|
153 | .read_errors = 0, |
---|
154 | .write_transfers = 1, |
---|
155 | .write_blocks = 3, |
---|
156 | .write_errors = 0 |
---|
157 | }; |
---|
158 | |
---|
159 | int rv; |
---|
160 | int fd; |
---|
161 | ssize_t num_bytes; |
---|
162 | uint8_t cluster_buf[SECTOR_SIZE |
---|
163 | * SECTORS_PER_CLUSTER]; |
---|
164 | uint32_t cluster_size = sizeof( cluster_buf ); |
---|
165 | off_t off; |
---|
166 | |
---|
167 | |
---|
168 | memset( cluster_buf, 0xFE, cluster_size ); |
---|
169 | |
---|
170 | format_and_mount( dev_name, mount_dir ); |
---|
171 | |
---|
172 | fd = create_file( file_name ); |
---|
173 | rtems_test_assert( fd >= 0 ); |
---|
174 | |
---|
175 | num_bytes = write( fd, cluster_buf, cluster_size ); |
---|
176 | rtems_test_assert( (ssize_t) cluster_size == num_bytes ); |
---|
177 | |
---|
178 | off = lseek( fd, 0, SEEK_SET ); |
---|
179 | rtems_test_assert( off == 0 ); |
---|
180 | |
---|
181 | reset_block_stats( dev_name, mount_dir ); |
---|
182 | |
---|
183 | /* Write a complete cluster into an existing file space */ |
---|
184 | num_bytes = write( fd, cluster_buf, cluster_size ); |
---|
185 | rtems_test_assert( (ssize_t) cluster_size == num_bytes ); |
---|
186 | |
---|
187 | check_block_stats( dev_name, mount_dir, &complete_existing_block_stats ); |
---|
188 | reset_block_stats( dev_name, mount_dir ); |
---|
189 | |
---|
190 | /* Write a complete cluster into a new file space */ |
---|
191 | num_bytes = write( fd, cluster_buf, cluster_size ); |
---|
192 | rtems_test_assert( (ssize_t) cluster_size == num_bytes ); |
---|
193 | |
---|
194 | check_block_stats( dev_name, mount_dir, &complete_new_block_stats ); |
---|
195 | reset_block_stats( dev_name, mount_dir ); |
---|
196 | |
---|
197 | /* Write a new partial cluster into a new file space */ |
---|
198 | num_bytes = write( fd, cluster_buf, 1 ); |
---|
199 | rtems_test_assert( num_bytes == 1 ); |
---|
200 | |
---|
201 | check_block_stats( dev_name, mount_dir, &partial_new_block_stats ); |
---|
202 | |
---|
203 | rv = close( fd ); |
---|
204 | rtems_test_assert( 0 == rv ); |
---|
205 | |
---|
206 | rv = unmount( mount_dir ); |
---|
207 | rtems_test_assert( 0 == rv ); |
---|
208 | } |
---|
209 | |
---|
210 | static void test_fat12_root_directory_write( const char *dev_name, |
---|
211 | const char *mount_dir, |
---|
212 | const char *file_name ) |
---|
213 | { |
---|
214 | static const rtems_blkdev_stats fat12_root_dir_stats = { |
---|
215 | .read_hits = 11, |
---|
216 | .read_misses = 2, |
---|
217 | .read_ahead_transfers = 0, |
---|
218 | .read_blocks = 2, |
---|
219 | .read_errors = 0, |
---|
220 | .write_transfers = 1, |
---|
221 | .write_blocks = 1, |
---|
222 | .write_errors = 0 |
---|
223 | }; |
---|
224 | |
---|
225 | int fd; |
---|
226 | int rv; |
---|
227 | |
---|
228 | |
---|
229 | format_and_mount( dev_name, mount_dir ); |
---|
230 | |
---|
231 | reset_block_stats( dev_name, mount_dir ); |
---|
232 | |
---|
233 | fd = create_file( file_name ); |
---|
234 | rtems_test_assert( fd >= 0 ); |
---|
235 | |
---|
236 | rv = close( fd ); |
---|
237 | rtems_test_assert( rv == 0 ); |
---|
238 | |
---|
239 | check_block_stats( dev_name, mount_dir, &fat12_root_dir_stats ); |
---|
240 | |
---|
241 | rv = unmount( mount_dir ); |
---|
242 | rtems_test_assert( rv == 0 ); |
---|
243 | } |
---|
244 | |
---|
245 | static void test( void ) |
---|
246 | { |
---|
247 | static const char dev_name[] = "/dev/sda"; |
---|
248 | static const char mount_dir[] = "/mnt"; |
---|
249 | static const char file_name[] = "/mnt/file.txt"; |
---|
250 | |
---|
251 | rtems_status_code sc; |
---|
252 | int rv; |
---|
253 | |
---|
254 | rv = mkdir( mount_dir, S_IRWXU | S_IRWXG | S_IRWXO ); |
---|
255 | rtems_test_assert( 0 == rv ); |
---|
256 | |
---|
257 | /* A 1.44 MB disk */ |
---|
258 | sc = rtems_sparse_disk_create_and_register( |
---|
259 | dev_name, |
---|
260 | SECTOR_SIZE, |
---|
261 | 64, |
---|
262 | 2880, |
---|
263 | 0 |
---|
264 | ); |
---|
265 | rtems_test_assert( RTEMS_SUCCESSFUL == sc ); |
---|
266 | |
---|
267 | test_fat12_root_directory_write( dev_name, mount_dir, file_name ); |
---|
268 | |
---|
269 | test_normal_file_write( dev_name, mount_dir, file_name ); |
---|
270 | |
---|
271 | rv = unlink( dev_name ); |
---|
272 | rtems_test_assert( rv == 0 ); |
---|
273 | } |
---|
274 | |
---|
275 | static void Init( rtems_task_argument arg ) |
---|
276 | { |
---|
277 | TEST_BEGIN(); |
---|
278 | |
---|
279 | test(); |
---|
280 | |
---|
281 | TEST_END(); |
---|
282 | rtems_test_exit( 0 ); |
---|
283 | } |
---|
284 | |
---|
285 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
286 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
287 | #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK |
---|
288 | |
---|
289 | #define CONFIGURE_FILESYSTEM_DOSFS |
---|
290 | |
---|
291 | /* 1 device file for blkstats + 1 file for writing + 1 mount_dir + stdin + stdout + stderr + device file when mounted */ |
---|
292 | #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 8 |
---|
293 | |
---|
294 | #define CONFIGURE_UNLIMITED_OBJECTS |
---|
295 | #define CONFIGURE_UNIFIED_WORK_AREAS |
---|
296 | |
---|
297 | #define CONFIGURE_INIT_TASK_STACK_SIZE ( 32 * 1024 ) |
---|
298 | |
---|
299 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
300 | |
---|
301 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
302 | |
---|
303 | #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT |
---|
304 | |
---|
305 | #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE ( 32 * 1024 ) |
---|
306 | |
---|
307 | #define CONFIGURE_INIT |
---|
308 | |
---|
309 | #include <rtems/confdefs.h> |
---|