Document all options supported by nbdtab
[nbd.git] / make-integrityhuge.c
blobab602230f89ed3bfe7140ffd7f2fbe4bb9822361
1 /*
2 * make-integrityhuge
4 * Make a file to test oversize writes
5 */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <stdint.h>
13 #include <unistd.h>
14 #include "config.h"
15 #include "cliserv.h"
16 #include "nbd.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) {
22 ssize_t res;
24 while(len>0) {
25 if((res=write(f, buf, len)) <=0) {
26 perror ("Error writing transactions");
27 exit(1);
29 len-=res;
30 buf+=res;
34 static inline uint64_t getrandomuint64() {
35 uint64_t r=0;
36 int i;
37 /* RAND_MAX may be as low as 2^15 */
38 for (i= 1 ; i<=5; i++)
39 r ^= random() ^ (r << 15);
40 return r;
43 int main(int argc, char**argv) {
44 struct nbd_request req;
45 struct nbd_reply rep;
46 uint64_t handle;
47 int writefd = 1; /*stdout*/
49 req.magic = htonl(NBD_REQUEST_MAGIC);
50 rep.magic = htonl(NBD_REPLY_MAGIC);
51 rep.error = 0;
53 for (handle = 0; handle < transactions; handle++)
55 uint64_t offset;
56 uint64_t length;
57 uint64_t flags;
58 uint32_t command;
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;
69 if (!(flags & 0x0f))
70 command = NBD_CMD_FLAG_FUA | NBD_CMD_WRITE;
72 if (!(flags & 0xf0)) {
73 offset = 0;
74 length = 0;
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);
89 req.from = 0;
90 req.len = 0;
91 dowrite(writefd, &req, sizeof(req));
93 return 0;