1 /* Simple block pattern reader/writer for testing FBD */
9 #define BLOCK_SIZE 4096 /* set to match root FS to prevent partial I/O */
11 static int flush_buf(int fd
, char *buf
, size_t size
, size_t write_size
)
15 while (write_size
<= size
) {
16 if ((r
= write(fd
, buf
, write_size
)) != write_size
) {
20 fprintf(stderr
, "short write (%d < %d)\n",
35 static int write_pattern(int fd
, char *pattern
, int write_size
)
39 int r
, count
, nblocks
;
41 /* Only write sizes that are a multiple or a
42 * divisor of the block size, are supported.
44 nblocks
= write_size
/ BLOCK_SIZE
;
45 if (!nblocks
) nblocks
= 1;
46 size
= nblocks
* BLOCK_SIZE
;
48 if ((buf
= malloc(size
)) == NULL
) {
57 ptr
= &buf
[count
* BLOCK_SIZE
];
65 memset(ptr
, *pattern
, BLOCK_SIZE
);
69 memset(ptr
, 0, BLOCK_SIZE
);
73 memset(ptr
, 0, BLOCK_SIZE
);
79 if (++count
== nblocks
) {
80 if ((r
= flush_buf(fd
, buf
, size
, write_size
)) !=
92 r
= flush_buf(fd
, buf
, count
* BLOCK_SIZE
, write_size
);
101 static int read_pattern(int fd
)
103 char buf
[BLOCK_SIZE
];
108 memset(buf
, '?', sizeof(buf
));
110 if ((r
= read(fd
, buf
, sizeof(buf
))) != sizeof(buf
)) {
113 if (!r
) break; /* stop at hard EOF */
115 lseek(fd
, sizeof(buf
), SEEK_CUR
);
120 if (buf
[0] == 'E' && buf
[1] == 'O' && buf
[2] == 'F') {
121 for (i
= 3; i
< sizeof(buf
); i
++)
122 if (buf
[i
] != 0) break;
124 if (i
== sizeof(buf
)) break;
127 for (i
= 1; i
< sizeof(buf
); i
++)
128 if (buf
[i
] != buf
[0]) break;
130 if (i
== sizeof(buf
)) {
138 printf("%c", buf
[0]);
152 for (i
= val
= 0; i
< sizeof(buf
); i
++)
155 printf("%c", 'a' + val
% 26);
163 int main(int argc
, char **argv
)
168 fprintf(stderr
, "usage: %s <device> [pattern [writesz]]\n",
174 fd
= open(argv
[1], (argc
> 2) ? O_WRONLY
: O_RDONLY
);
182 r
= write_pattern(fd
, argv
[2],
183 argv
[3] ? atoi(argv
[3]) : BLOCK_SIZE
);
185 r
= read_pattern(fd
);