summaryrefslogtreecommitdiffstats
path: root/testsuites/fstests/fsdosfswrite01/init.c
blob: b787e3c37f10f0508979998ad171eb3a79d98b30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Obere Lagerstr. 30
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
  #include "config.h"
#endif

#define TEST_INIT

#include "tmacros.h"
#include <fcntl.h>
#include <rtems/dosfs.h>
#include <rtems/sparse-disk.h>
#include <rtems/blkdev.h>
#include <bsp.h>

const char rtems_test_name[] = "FSDOSFSWRITE 1";

#define MAX_PATH_LENGTH 100 /* Maximum number of characters per path */
#define SECTOR_SIZE 512 /* sector size (bytes) */
#define FAT16_MAX_CLN 65525 /* maximum + 1 number of clusters for FAT16 */
#define FAT16_DEFAULT_SECTORS_PER_CLUSTER 32 /* Default number of sectors per cluster for FAT16 */
#define SECTORS_PER_CLUSTER 2

static void format_and_mount( const char *dev_name, const char *mount_dir )
{
  static const msdos_format_request_param_t rqdata = {
    .sectors_per_cluster = SECTORS_PER_CLUSTER,
    .quick_format        = true
  };

  int                                       rv;


  rv = msdos_format( dev_name, &rqdata );
  rtems_test_assert( rv == 0 );

  rv = mount( dev_name,
              mount_dir,
              RTEMS_FILESYSTEM_TYPE_DOSFS,
              RTEMS_FILESYSTEM_READ_WRITE,
              NULL );
  rtems_test_assert( rv == 0 );
}

static void do_fsync( const char *file )
{
  int rv;
  int fd;


  fd = open( file, O_RDONLY );
  rtems_test_assert( fd >= 0 );

  rv = fsync( fd );
  rtems_test_assert( rv == 0 );

  rv = close( fd );
  rtems_test_assert( rv == 0 );
}

static void check_block_stats( const char *dev_name,
  const char                              *mount_dir,
  const rtems_blkdev_stats                *expected_stats )
{
  int                fd;
  int                rv;
  rtems_blkdev_stats actual_stats;


  do_fsync( mount_dir );

  fd = open( dev_name, O_RDONLY );
  rtems_test_assert( fd >= 0 );

  rv = ioctl( fd, RTEMS_BLKIO_GETDEVSTATS, &actual_stats );
  rtems_test_assert( rv == 0 );
  rtems_test_assert( memcmp( &actual_stats, expected_stats,
                             sizeof( actual_stats ) ) == 0 );

  rv = close( fd );
  rtems_test_assert( rv == 0 );
}

static void reset_block_stats( const char *dev_name, const char *mount_dir )
{
  int fd;
  int rv;


  do_fsync( mount_dir );

  fd = open( dev_name, O_RDONLY );
  rtems_test_assert( fd >= 0 );

  rv = ioctl( fd, RTEMS_BLKIO_PURGEDEV );
  rtems_test_assert( rv == 0 );

  rv = ioctl( fd, RTEMS_BLKIO_RESETDEVSTATS );
  rtems_test_assert( rv == 0 );

  rv = close( fd );
  rtems_test_assert( rv == 0 );
}

static int create_file( const char *file_name )
{
  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;


  return creat( file_name, mode );
}

static void test_normal_file_write(
  const char *dev_name,
  const char *mount_dir,
  const char *file_name )
{
  static const rtems_blkdev_stats complete_existing_block_stats = {
    .read_hits            = 0,
    .read_misses          = 0,
    .read_ahead_transfers = 0,
    .read_blocks          = 0,
    .read_errors          = 0,
    .write_transfers      = 1,
    .write_blocks         = 1,
    .write_errors         = 0
  };
  static const rtems_blkdev_stats complete_new_block_stats = {
    .read_hits            = 3,
    .read_misses          = 2,
    .read_ahead_transfers = 0,
    .read_blocks          = 2,
    .read_errors          = 0,
    .write_transfers      = 1,
    .write_blocks         = 3,
    .write_errors         = 0
  };
  static const rtems_blkdev_stats partial_new_block_stats = {
    .read_hits            = 3,
    .read_misses          = 3,
    .read_ahead_transfers = 0,
    .read_blocks          = 3,
    .read_errors          = 0,
    .write_transfers      = 1,
    .write_blocks         = 3,
    .write_errors         = 0
  };

  int                             rv;
  int                             fd;
  ssize_t                         num_bytes;
  uint8_t                         cluster_buf[SECTOR_SIZE
                                              * SECTORS_PER_CLUSTER];
  uint32_t                        cluster_size = sizeof( cluster_buf );
  off_t                           off;


  memset( cluster_buf, 0xFE, cluster_size );

  format_and_mount( dev_name, mount_dir );

  fd = create_file( file_name );
  rtems_test_assert( fd >= 0 );

  num_bytes = write( fd, cluster_buf, cluster_size );
  rtems_test_assert( (ssize_t) cluster_size == num_bytes );

  off = lseek( fd, 0, SEEK_SET );
  rtems_test_assert( off == 0 );

  reset_block_stats( dev_name, mount_dir );

  /* Write a complete cluster into an existing file space */
  num_bytes = write( fd, cluster_buf, cluster_size );
  rtems_test_assert( (ssize_t) cluster_size == num_bytes );

  check_block_stats( dev_name, mount_dir, &complete_existing_block_stats );
  reset_block_stats( dev_name, mount_dir );

  /* Write a complete cluster into a new file space */
  num_bytes = write( fd, cluster_buf, cluster_size );
  rtems_test_assert( (ssize_t) cluster_size == num_bytes );

  check_block_stats( dev_name, mount_dir, &complete_new_block_stats );
  reset_block_stats( dev_name, mount_dir );

  /* Write a new partial cluster into a new file space */
  num_bytes = write( fd, cluster_buf, 1 );
  rtems_test_assert( num_bytes == 1 );

  check_block_stats( dev_name, mount_dir, &partial_new_block_stats );

  rv = close( fd );
  rtems_test_assert( 0 == rv );

  rv = unmount( mount_dir );
  rtems_test_assert( 0 == rv );
}

static void test_fat12_root_directory_write( const char *dev_name,
  const char                                            *mount_dir,
  const char                                            *file_name )
{
  static const rtems_blkdev_stats fat12_root_dir_stats = {
    .read_hits            = 11,
    .read_misses          = 2,
    .read_ahead_transfers = 0,
    .read_blocks          = 2,
    .read_errors          = 0,
    .write_transfers      = 1,
    .write_blocks         = 1,
    .write_errors         = 0
  };

  int                             fd;
  int                             rv;


  format_and_mount( dev_name, mount_dir );

  reset_block_stats( dev_name, mount_dir );

  fd = create_file( file_name );
  rtems_test_assert( fd >= 0 );

  rv = close( fd );
  rtems_test_assert( rv == 0 );

  check_block_stats( dev_name, mount_dir, &fat12_root_dir_stats );

  rv = unmount( mount_dir );
  rtems_test_assert( rv == 0 );
}

static void test( void )
{
  static const char dev_name[]  = "/dev/sda";
  static const char mount_dir[] = "/mnt";
  static const char file_name[] = "/mnt/file.txt";

  rtems_status_code sc;
  int               rv;


  sc = rtems_disk_io_initialize();
  rtems_test_assert( sc == RTEMS_SUCCESSFUL );

  rv = mkdir( mount_dir, S_IRWXU | S_IRWXG | S_IRWXO );
  rtems_test_assert( 0 == rv );

  /* A 1.44 MB disk */
  sc = rtems_sparse_disk_create_and_register(
    dev_name,
    SECTOR_SIZE,
    64,
    2880,
    0
    );
  rtems_test_assert( RTEMS_SUCCESSFUL == sc );

  test_fat12_root_directory_write( dev_name, mount_dir, file_name );

  test_normal_file_write( dev_name, mount_dir, file_name );

  rv = unlink( dev_name );
  rtems_test_assert( rv == 0 );
}

static void Init( rtems_task_argument arg )
{
  TEST_BEGIN();

  test();

  TEST_END();
  rtems_test_exit( 0 );
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK

#define CONFIGURE_FILESYSTEM_DOSFS

/* 1 device file for blkstats + 1 file for writing + 1 mount_dir + stdin + stdout + stderr + device file when mounted */
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 8

#define CONFIGURE_UNLIMITED_OBJECTS
#define CONFIGURE_UNIFIED_WORK_AREAS

#define CONFIGURE_INIT_TASK_STACK_SIZE ( 32 * 1024 )

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT

#define CONFIGURE_BDBUF_BUFFER_MAX_SIZE ( 32 * 1024 )

#define CONFIGURE_INIT

#include <rtems/confdefs.h>