Network Block Device  @PACKAGE_VERSION@
make-integrityhuge.c
Go to the documentation of this file.
1 /*
2  * make-integrityhuge
3  *
4  * Make a file to test oversize writes
5  */
6 
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"
17 
18 const uint64_t filesize=50*1000*1000;
19 const uint64_t transactions = 250;
20 
21 static inline void dowrite(int f, void *buf, size_t len) {
22  ssize_t res;
23 
24  while(len>0) {
25  if((res=write(f, buf, len)) <=0) {
26  perror ("Error writing transactions");
27  exit(1);
28  }
29  len-=res;
30  buf+=res;
31  }
32 }
33 
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;
41 }
42 
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*/
48 
49  req.magic = htonl(NBD_REQUEST_MAGIC);
50  rep.magic = htonl(NBD_REPLY_MAGIC);
51  rep.error = 0;
52 
53  for (handle = 0; handle < transactions; handle++)
54  {
55  uint64_t offset;
56  uint64_t length;
57  uint64_t flags;
58  uint32_t command;
59 
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();
66 
67  command = (flags & 0x01)?NBD_CMD_READ:NBD_CMD_WRITE;
68 
69  if (!(flags & 0x0f))
70  command = NBD_CMD_FLAG_FUA | NBD_CMD_WRITE;
71 
72  if (!(flags & 0xf0)) {
73  offset = 0;
74  length = 0;
75  command = NBD_CMD_FLUSH;
76  }
77 
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);
83 
84  dowrite(writefd, &req, sizeof(req));
85  dowrite(writefd, &rep, sizeof(rep));
86  }
87 
88  req.type = htonl(NBD_CMD_DISC);
89  req.from = 0;
90  req.len = 0;
91  dowrite(writefd, &req, sizeof(req));
92 
93  return 0;
94 }
__be32 type
Definition: nbd.h:67
Definition: nbd.h:77
char handle[8]
Definition: nbd.h:80
const uint64_t transactions
static void dowrite(int f, void *buf, size_t len)
__be32 error
Definition: nbd.h:79
#define NBD_CMD_FLAG_FUA
Definition: nbd.h:41
const uint64_t filesize
__be32 len
Definition: nbd.h:70
__be64 from
Definition: nbd.h:69
int main(int argc, char **argv)
#define htonll
Definition: cliserv.h:146
#define NBD_REPLY_MAGIC
Definition: nbd.h:58
__be32 magic
Definition: nbd.h:78
#define NBD_REQUEST_MAGIC
Definition: nbd.h:57
char handle[8]
Definition: nbd.h:39
char handle[8]
Definition: nbd.h:68
static uint64_t getrandomuint64()
__be32 len
Definition: nbd.h:41
__be32 magic
Definition: nbd.h:66