4 * Make a file to test oversize writes
11 #include <sys/types.h>
18 const uint64_t filesize
=50*1000*1000;
19 const uint64_t transactions
= 250;
21 static inline void dowrite(int f
, void *buf
, size_t len
) {
25 if((res
=write(f
, buf
, len
)) <=0) {
26 perror ("Error writing transactions");
34 static inline uint64_t getrandomuint64() {
37 /* RAND_MAX may be as low as 2^15 */
38 for (i
= 1 ; i
<=5; i
++)
39 r
^= random() ^ (r
<< 15);
43 int main(int argc
, char**argv
) {
44 struct nbd_request req
;
47 int writefd
= 1; /*stdout*/
49 req
.magic
= htonl(NBD_REQUEST_MAGIC
);
50 rep
.magic
= htonl(NBD_REPLY_MAGIC
);
53 for (handle
= 0; handle
< transactions
; handle
++)
60 /* make the length between 0x400 and the length of the disk -08x800, with all
61 * the bottom bits clear */
62 length
= ((getrandomuint64() % (filesize
-0x800)) & ~((uint64_t)0x3ff)) + 0x400;
63 /* generate an offset that will fit the length */
64 offset
= (getrandomuint64() % (filesize
-length
)) & ~((uint64_t)0x3ff);
65 flags
= getrandomuint64();
67 command
= (flags
& 0x01)?NBD_CMD_READ
:NBD_CMD_WRITE
;
70 command
= NBD_CMD_FLAG_FUA
| NBD_CMD_WRITE
;
72 if (!(flags
& 0xf0)) {
75 command
= NBD_CMD_FLUSH
;
78 *(uint64_t *)(req
.handle
) = htonll(handle
);
79 *(uint64_t *)(rep
.handle
) = htonll(handle
);
80 req
.type
= htonl(command
);
81 req
.from
= htonll(offset
);
82 req
.len
= htonl(length
);
84 dowrite(writefd
, &req
, sizeof(req
));
85 dowrite(writefd
, &rep
, sizeof(rep
));
88 req
.type
= htonl(NBD_CMD_DISC
);
91 dowrite(writefd
, &req
, sizeof(req
));