File: make-integrityhuge.c

package info (click to toggle)
nbd 1:3.20-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 5,960 kB
  • sloc: ansic: 7,494; sh: 6,496; makefile: 147
file content (94 lines) | stat: -rw-r--r-- 2,110 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * make-integrityhuge
 *
 * Make a file to test oversize writes
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdint.h>
#include <unistd.h>
#include "config.h"
#include "cliserv.h"
#include "nbd.h"

const uint64_t filesize=50*1000*1000;
const uint64_t transactions = 250;

static inline void dowrite(int f, void *buf, size_t len) {
        ssize_t res;

        while(len>0) {
                if((res=write(f, buf, len)) <=0) {
			perror ("Error writing transactions");
			exit(1);
                }
                len-=res;
                buf+=res;
        }
}

static inline uint64_t getrandomuint64() {
	uint64_t r=0;
        int i;
	/* RAND_MAX may be as low as 2^15 */
	for (i= 1 ; i<=5; i++)
		r ^= random() ^ (r << 15); 
	return r;
}

int main(int argc, char**argv) {
	struct nbd_request req;
	struct nbd_reply rep;
	uint64_t handle;
	int writefd = 1; /*stdout*/

	req.magic = htonl(NBD_REQUEST_MAGIC);
	rep.magic = htonl(NBD_REPLY_MAGIC);
	rep.error = 0;

	for (handle = 0; handle < transactions; handle++)
	{
		uint64_t offset;
		uint64_t length;
		uint64_t flags;
		uint32_t command;

		/* make the length between 0x400 and the length of the disk -08x800, with all
		 * the bottom bits clear */
		length = ((getrandomuint64() % (filesize-0x800)) & ~((uint64_t)0x3ff)) + 0x400;
		/* generate an offset that will fit the length */
		offset = (getrandomuint64() % (filesize-length)) & ~((uint64_t)0x3ff);
		flags = getrandomuint64();

		command = (flags & 0x01)?NBD_CMD_READ:NBD_CMD_WRITE;

		if (!(flags & 0x0f))
			command = NBD_CMD_FLAG_FUA | NBD_CMD_WRITE;

		if (!(flags & 0xf0)) {
			offset = 0;
			length = 0;
			command = NBD_CMD_FLUSH;
		}

		*(uint64_t *)(req.handle) = htonll(handle);
		*(uint64_t *)(rep.handle) = htonll(handle);
		req.type = htonl(command);
		req.from = htonll(offset);
		req.len = htonl(length);

		dowrite(writefd, &req, sizeof(req));
		dowrite(writefd, &rep, sizeof(rep));
	}

	req.type = htonl(NBD_CMD_DISC);
	req.from = 0;
	req.len = 0;
	dowrite(writefd, &req, sizeof(req));
	
	return 0;
}