4 * Takes an nbd transaction log file on stdin and translates it into something
12 #include <sys/types.h>
16 /* We don't want to do syslog output in this program */
21 static inline void doread(int f
, void *buf
, size_t len
) {
25 if((res
=read(f
, buf
, len
)) <=0) {
28 perror ("Error reading transactions");
36 int main(int argc
, char**argv
) {
37 struct nbd_request req
;
46 int readfd
= 0; /* stdin */
50 if(strcmp(argv
[1], "--help") && strcmp(argv
[1], "-h")) {
51 printf("E: unknown option %s.\n", argv
[1]);
54 printf("This is nbd-trdump, part of nbd %s.\n", PACKAGE_VERSION
);
55 printf("Use: %s < transactionlog\n", argv
[0]);
60 /* Read a request or reply from the transaction file */
61 doread(readfd
, &magic
, sizeof(magic
));
64 case NBD_REQUEST_MAGIC
:
65 doread(readfd
, sizeof(magic
)+(char *)(&req
), sizeof(struct nbd_request
)-sizeof(magic
));
66 handle
= ntohll(*((long long int *)(req
.handle
)));
67 offset
= ntohll(req
.from
);
69 command
= ntohl(req
.type
);
71 switch (command
& NBD_CMD_MASK_COMMAND
) {
76 ctext
="NBD_CMD_WRITE";
82 ctext
="NBD_CMD_FLUSH";
88 printf("> H=%016llx C=0x%08x (%13s+%4s) O=%016llx L=%08x\n",
89 (long long unsigned int) handle
,
92 (command
& NBD_CMD_FLAG_FUA
)?"FUA":"NONE",
93 (long long unsigned int) offset
,
98 doread(readfd
, sizeof(magic
)+(char *)(&rep
), sizeof(struct nbd_reply
)-sizeof(magic
));
99 handle
= ntohll(*((long long int *)(rep
.handle
)));
100 error
= ntohl(rep
.error
);
102 printf("< H=%016llx E=0x%08x\n",
103 (long long unsigned int) handle
,
108 printf("? Unknown transaction type %08x\n",magic
);