[42a22f08] | 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 |
---|
[c499856] | 12 | * http://www.rtems.org/license/LICENSE. |
---|
[42a22f08] | 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 | |
---|
[bc75887] | 26 | const char rtems_test_name[] = "FSDOSFSWRITE 1"; |
---|
| 27 | |
---|
[42a22f08] | 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_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 new_block_stats = { |
---|
| 139 | .read_hits = 8, |
---|
| 140 | .read_misses = 2, |
---|
| 141 | .read_ahead_transfers = 0, |
---|
| 142 | .read_blocks = 2, |
---|
| 143 | .read_errors = 0, |
---|
| 144 | .write_transfers = 1, |
---|
| 145 | .write_blocks = 4, |
---|
| 146 | .write_errors = 0 |
---|
| 147 | }; |
---|
| 148 | |
---|
| 149 | int rv; |
---|
| 150 | int fd; |
---|
| 151 | ssize_t num_bytes; |
---|
| 152 | uint8_t cluster_buf[SECTOR_SIZE |
---|
| 153 | * SECTORS_PER_CLUSTER]; |
---|
| 154 | uint32_t cluster_size = sizeof( cluster_buf ); |
---|
| 155 | off_t off; |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | memset( cluster_buf, 0xFE, cluster_size ); |
---|
| 159 | |
---|
| 160 | format_and_mount( dev_name, mount_dir ); |
---|
| 161 | |
---|
| 162 | fd = create_file( file_name ); |
---|
| 163 | rtems_test_assert( fd >= 0 ); |
---|
| 164 | |
---|
| 165 | num_bytes = write( fd, cluster_buf, cluster_size ); |
---|
| 166 | rtems_test_assert( (ssize_t) cluster_size == num_bytes ); |
---|
| 167 | |
---|
| 168 | off = lseek( fd, 0, SEEK_SET ); |
---|
| 169 | rtems_test_assert( off == 0 ); |
---|
| 170 | |
---|
| 171 | reset_block_stats( dev_name, mount_dir ); |
---|
| 172 | |
---|
| 173 | /* Write a complete cluster into an existing file space */ |
---|
| 174 | num_bytes = write( fd, cluster_buf, cluster_size ); |
---|
| 175 | rtems_test_assert( (ssize_t) cluster_size == num_bytes ); |
---|
| 176 | |
---|
| 177 | check_block_stats( dev_name, mount_dir, &complete_block_stats ); |
---|
| 178 | reset_block_stats( dev_name, mount_dir ); |
---|
| 179 | |
---|
| 180 | num_bytes = write( fd, cluster_buf, cluster_size ); |
---|
| 181 | rtems_test_assert( (ssize_t) cluster_size == num_bytes ); |
---|
| 182 | |
---|
| 183 | /* Write a new partial cluster into a new file space */ |
---|
| 184 | num_bytes = write( fd, cluster_buf, 1 ); |
---|
| 185 | rtems_test_assert( num_bytes == 1 ); |
---|
| 186 | |
---|
| 187 | check_block_stats( dev_name, mount_dir, &new_block_stats ); |
---|
| 188 | |
---|
| 189 | rv = close( fd ); |
---|
| 190 | rtems_test_assert( 0 == rv ); |
---|
| 191 | |
---|
| 192 | rv = unmount( mount_dir ); |
---|
| 193 | rtems_test_assert( 0 == rv ); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | static void test_fat12_root_directory_write( const char *dev_name, |
---|
| 197 | const char *mount_dir, |
---|
| 198 | const char *file_name ) |
---|
| 199 | { |
---|
| 200 | static const rtems_blkdev_stats fat12_root_dir_stats = { |
---|
| 201 | .read_hits = 11, |
---|
| 202 | .read_misses = 2, |
---|
| 203 | .read_ahead_transfers = 0, |
---|
| 204 | .read_blocks = 2, |
---|
| 205 | .read_errors = 0, |
---|
| 206 | .write_transfers = 1, |
---|
| 207 | .write_blocks = 1, |
---|
| 208 | .write_errors = 0 |
---|
| 209 | }; |
---|
| 210 | |
---|
| 211 | int fd; |
---|
| 212 | int rv; |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | format_and_mount( dev_name, mount_dir ); |
---|
| 216 | |
---|
| 217 | reset_block_stats( dev_name, mount_dir ); |
---|
| 218 | |
---|
| 219 | fd = create_file( file_name ); |
---|
| 220 | rtems_test_assert( fd >= 0 ); |
---|
| 221 | |
---|
| 222 | rv = close( fd ); |
---|
| 223 | rtems_test_assert( rv == 0 ); |
---|
| 224 | |
---|
| 225 | check_block_stats( dev_name, mount_dir, &fat12_root_dir_stats ); |
---|
| 226 | |
---|
| 227 | rv = unmount( mount_dir ); |
---|
| 228 | rtems_test_assert( rv == 0 ); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | static void test( void ) |
---|
| 232 | { |
---|
| 233 | static const char dev_name[] = "/dev/sda"; |
---|
| 234 | static const char mount_dir[] = "/mnt"; |
---|
| 235 | static const char file_name[] = "/mnt/file.txt"; |
---|
| 236 | |
---|
| 237 | rtems_status_code sc; |
---|
| 238 | int rv; |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | sc = rtems_disk_io_initialize(); |
---|
| 242 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
| 243 | |
---|
| 244 | rv = mkdir( mount_dir, S_IRWXU | S_IRWXG | S_IRWXO ); |
---|
| 245 | rtems_test_assert( 0 == rv ); |
---|
| 246 | |
---|
| 247 | /* A 1.44 MB disk */ |
---|
| 248 | sc = rtems_sparse_disk_create_and_register( |
---|
| 249 | dev_name, |
---|
| 250 | SECTOR_SIZE, |
---|
| 251 | 64, |
---|
| 252 | 2880, |
---|
| 253 | 0 |
---|
| 254 | ); |
---|
| 255 | rtems_test_assert( RTEMS_SUCCESSFUL == sc ); |
---|
| 256 | |
---|
| 257 | test_fat12_root_directory_write( dev_name, mount_dir, file_name ); |
---|
| 258 | |
---|
| 259 | test_normal_file_write( dev_name, mount_dir, file_name ); |
---|
| 260 | |
---|
| 261 | rv = unlink( dev_name ); |
---|
| 262 | rtems_test_assert( rv == 0 ); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | static void Init( rtems_task_argument arg ) |
---|
| 266 | { |
---|
[bc75887] | 267 | TEST_BEGIN(); |
---|
[42a22f08] | 268 | |
---|
| 269 | test(); |
---|
| 270 | |
---|
[bc75887] | 271 | TEST_END(); |
---|
[42a22f08] | 272 | rtems_test_exit( 0 ); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
| 276 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
| 277 | #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK |
---|
| 278 | |
---|
| 279 | #define CONFIGURE_FILESYSTEM_DOSFS |
---|
| 280 | |
---|
| 281 | /* 1 device file for blkstats + 1 file for writing + 1 mount_dir + stdin + stdout + stderr + device file when mounted */ |
---|
| 282 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 8 |
---|
| 283 | |
---|
| 284 | #define CONFIGURE_UNLIMITED_OBJECTS |
---|
| 285 | #define CONFIGURE_UNIFIED_WORK_AREAS |
---|
| 286 | |
---|
| 287 | #define CONFIGURE_INIT_TASK_STACK_SIZE ( 32 * 1024 ) |
---|
| 288 | |
---|
[bc75887] | 289 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
| 290 | |
---|
[42a22f08] | 291 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
| 292 | |
---|
| 293 | #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE ( 32 * 1024 ) |
---|
| 294 | |
---|
| 295 | #define CONFIGURE_INIT |
---|
| 296 | |
---|
[bc75887] | 297 | #include <rtems/confdefs.h> |
---|