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