source: rtems/testsuites/fstests/fsdosfswrite01/init.c @ 42a22f08

4.11
Last change on this file since 42a22f08 was 42a22f08, checked in by Ralf Kirchner <ralf.kirchner@…>, on Dec 5, 2012 at 12:43:34 PM

dosfs: Cluster write optimization

Separate cluster write from sector write for quick file write.

New test fstests/fsdosfswrite01.

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