Add missing break
[nbd.git] / nbd-trdump.c
1 /*
2  * nbd-trdump.c
3  *
4  * Takes an nbd transaction log file on stdin and translates it into something
5  * comprehensible
6  */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <stdint.h>
14 #include <unistd.h>
15 #include "config.h"
16 /* We don't want to do syslog output in this program */
17 #undef ISSERVER
18 #include "cliserv.h"
19 #include "nbd.h"
20
21 static inline void doread(int f, void *buf, size_t len) {
22         ssize_t res;
23
24         while(len>0) {
25                 if((res=read(f, buf, len)) <=0) {
26                         if (!res)
27                                 exit(0);
28                         perror ("Error reading transactions");
29                         exit(1);
30                 }
31                 len-=res;
32                 buf+=res;
33         }
34 }
35
36 int main(int argc, char**argv) {
37         struct nbd_request req;
38         struct nbd_reply rep;
39         uint32_t magic;
40         uint64_t handle;
41         uint32_t error;
42         uint32_t command;
43         uint32_t len;
44         uint64_t offset;
45         char * ctext;
46         int readfd = 0; /* stdin */
47
48         if(argc > 1) {
49                 int retval=0;
50                 if(strcmp(argv[1], "--help") && strcmp(argv[1], "-h")) {
51                         printf("E: unknown option %s.\n", argv[1]);
52                         retval=1;
53                 }
54                 printf("This is nbd-trdump, part of nbd %s.\n", PACKAGE_VERSION);
55                 printf("Use: %s < transactionlog\n", argv[0]);
56                 return retval;
57         }
58
59         while (1) {
60                 /* Read a request or reply from the transaction file */
61                 doread(readfd, &magic, sizeof(magic));
62                 magic = ntohl(magic);
63                 switch (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);
68                         len = ntohl(req.len);
69                         command = ntohl(req.type);
70                         
71                         switch (command & NBD_CMD_MASK_COMMAND) {
72                         case NBD_CMD_READ:
73                                 ctext="NBD_CMD_READ";
74                                 break;
75                         case NBD_CMD_WRITE:
76                                 ctext="NBD_CMD_WRITE";
77                                 break;
78                         case NBD_CMD_DISC:
79                                 ctext="NBD_CMD_DISC";
80                                 break;
81                         case NBD_CMD_FLUSH:
82                                 ctext="NBD_CMD_FLUSH";
83                                 break;
84                         default:
85                                 ctext="UNKNOWN";
86                                 break;
87                         }
88                         printf("> H=%016llx C=0x%08x (%13s+%4s) O=%016llx L=%08x\n",
89                                (long long unsigned int) handle,
90                                command,
91                                ctext,
92                                (command & NBD_CMD_FLAG_FUA)?"FUA":"NONE",
93                                (long long unsigned int) offset,
94                                len);
95                         
96                         break;
97                 case NBD_REPLY_MAGIC:
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);
101                         
102                         printf("< H=%016llx E=0x%08x\n",
103                                (long long unsigned int) handle,
104                                error);
105                         break;
106                         
107                 default:
108                         printf("? Unknown transaction type %08x\n",magic);
109                         break;
110                 }
111                 
112         }
113         /* never reached */
114         return 0;
115 }