[PATCH] blktrace: fix get_subbuf() leak
[blktrace.git] / blkrawverify.c
index 88d91a5c9d85057c68a5b3f29d8049a928e17927..ca827072c8b854699d4da99d53c79dcd3fb8836d 100644 (file)
@@ -33,6 +33,8 @@ struct trace_info {
        char *string;
 };
 
+int data_is_native = -1;
+
 #define TRACE_TO_STRING(f)     {.bit_field = f, .string = #f}
 static struct trace_info traces[] = {
        TRACE_TO_STRING( BLK_TC_READ ),
@@ -101,8 +103,8 @@ static void dump_trace(FILE *ofp, char *prefix, struct blk_io_trace *bit)
        fprintf(ofp, "    Dump %s\n", prefix);
        fprintf(ofp, "        %8s: %08x\n", "magic", bit->magic);
        fprintf(ofp, "        %8s: %u\n", "sequence", bit->sequence);
-       fprintf(ofp, "        %8s: %llu\n", "time", bit->time);
-       fprintf(ofp, "        %8s: %llu\n", "sector", bit->sector);
+       fprintf(ofp, "        %8s: %llu\n", "time", (unsigned long long) bit->time);
+       fprintf(ofp, "        %8s: %llu\n", "sector", (unsigned long long) bit->sector);
        fprintf(ofp, "        %8s: %u\n", "bytes", bit->bytes);
        fprintf(ofp, "        %8s: %s\n", "action", act_to_str(bit->action));
        fprintf(ofp, "        %8s: %u\n", "bytes", bit->bytes);
@@ -113,7 +115,7 @@ static void dump_trace(FILE *ofp, char *prefix, struct blk_io_trace *bit)
                                                           MINOR(bit->device));
 }
 
-static int process(FILE *ofp, char *file, unsigned int cpu)
+static int process(FILE **fp, char *devname, char *file, unsigned int cpu)
 {
 #      define SWAP_BITS() do {                                         \
                if (bit_save) {                                         \
@@ -136,7 +138,7 @@ static int process(FILE *ofp, char *file, unsigned int cpu)
        } while (0)
 
        size_t n;
-       FILE *ifp;
+       FILE *ifp, *ofp;
        __u32 save_device = 0, save_sequence = 0;
        __u64 save_time = 0;
        struct blk_io_trace *bit_save = NULL;
@@ -145,15 +147,43 @@ static int process(FILE *ofp, char *file, unsigned int cpu)
        unsigned int nbad = 0;
        unsigned int nbad_trace = 0, nbad_pdu = 0, nbad_cpu = 0;
        unsigned int nbad_seq = 0, nbad_dev = 0, nbad_time = 0;
+       char ofname[1024];
 
        ifp = fopen(file, "r");
+       if (!ifp)
+               return 0;
+
+       sprintf(ofname, "%s.verify.out", devname);
+
+       if (!*fp) {
+               *fp = fopen(ofname, "w");
+               if (*fp == NULL) {
+                       fprintf(stderr,"Failed to open %s (%s), skipping\n",
+                               ofname, strerror(errno));
+                       fclose(ifp);
+                       return 0;
+               }
+               fprintf(*fp, "\n---------------\n" );
+               fprintf(*fp, "Verifying %s\n", devname);
+       }
+
+       ofp = *fp;
        while ((n = fread(bit, sizeof(struct blk_io_trace), 1, ifp)) == 1) {
+               if (data_is_native == -1)
+                       check_data_endianness(bit->magic);
+
                trace_to_cpu(bit);
-               if (verify_trace(bit)) {
+
+               if (!CHECK_MAGIC(bit)) {
                        INC_BAD("bad trace");
                        continue;
                }
 
+               if ((bit->magic & 0xff) != SUPPORTED_VERSION) {
+                       fprintf(stderr, "unsupported trace version\n");
+                       break;
+               }
+
                if (bit->pdu_len) {
                        char *pdu_buf;
 
@@ -173,6 +203,12 @@ static int process(FILE *ofp, char *file, unsigned int cpu)
                        continue;
                }
 
+               /*
+                * skip notify traces, they don't have valid sequences
+                */
+               if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY))
+                       continue;
+
                if (ngood) {
                        if (bit->sequence <= save_sequence) {
                                INC_BAD("bad seq");
@@ -199,7 +235,7 @@ static int process(FILE *ofp, char *file, unsigned int cpu)
                SWAP_BITS();
        }
 
-       if (n == 0)
+       if (n == 0 && !feof(ifp))
                fprintf(stderr,"%s: fread failed %d/%s\n",
                        file, errno, strerror(errno));
        fclose(ifp);
@@ -247,27 +283,23 @@ int main(int argc, char *argv[])
        for (i = 1; i < argc; i++) {
                devname = argv[i];
                sprintf(ofname, "%s.verify.out", devname);
-               ofp = fopen(ofname, "w");
-               if (ofp == NULL) {
-                       fprintf(stderr,"Failed to open %s (%s), skipping %s\n",
-                               ofname, strerror(errno), devname);
-                       continue;
-               }
+               ofp = NULL;
 
-               fprintf(ofp, "\n---------------\n" );
-               fprintf(ofp, "Verifying %s\n", devname);
                printf("Verifying %s\n", devname); fflush(stdout);
                for (cpu = 0; ; cpu++) {
                        sprintf(fname, "%s.blktrace.%d", devname, cpu);
                        if (stat(fname, &st) < 0)
                                break;
                        printf("    CPU %d ", cpu); fflush(stdout);
-                       nbad = process(ofp, fname, cpu);
+                       nbad = process(&ofp, devname, fname, cpu);
                        if (nbad)
                                printf("-- %d bad", nbad);
                        printf("\n");
                }
-               fclose(ofp);
+               if (ofp) {
+                       fclose(ofp);
+                       fprintf(stdout, "Wrote output to %s\n", ofname);
+               }
        }
 
        return 0;