Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * IO verification helpers |
| 3 | */ |
| 4 | #include <unistd.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <string.h> |
Jens Axboe | 97af62c | 2007-05-22 11:12:13 +0200 | [diff] [blame] | 7 | #include <assert.h> |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 8 | |
| 9 | #include "fio.h" |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 10 | |
Jens Axboe | eef6eea | 2007-07-30 12:27:58 +0200 | [diff] [blame] | 11 | #include "crc/md5.h" |
| 12 | #include "crc/crc64.h" |
| 13 | #include "crc/crc32.h" |
| 14 | #include "crc/crc16.h" |
| 15 | #include "crc/crc7.h" |
| 16 | #include "crc/sha256.h" |
| 17 | #include "crc/sha512.h" |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 18 | |
Jens Axboe | 90059d6 | 2007-07-30 09:33:12 +0200 | [diff] [blame] | 19 | static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len) |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 20 | { |
| 21 | unsigned int todo; |
Jens Axboe | 4c5946c | 2007-07-26 11:55:10 +0200 | [diff] [blame] | 22 | int r; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 23 | |
| 24 | while (len) { |
Jens Axboe | 4c5946c | 2007-07-26 11:55:10 +0200 | [diff] [blame] | 25 | r = os_random_long(&td->verify_state); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * lrand48_r seems to be broken and only fill the bottom |
| 29 | * 32-bits, even on 64-bit archs with 64-bit longs |
| 30 | */ |
| 31 | todo = sizeof(r); |
| 32 | if (todo > len) |
| 33 | todo = len; |
| 34 | |
| 35 | memcpy(p, &r, todo); |
| 36 | |
| 37 | len -= todo; |
| 38 | p += todo; |
| 39 | } |
| 40 | } |
| 41 | |
Jens Axboe | 90059d6 | 2007-07-30 09:33:12 +0200 | [diff] [blame] | 42 | static void fill_pattern(struct thread_data *td, void *p, unsigned int len) |
| 43 | { |
| 44 | switch (td->o.verify_pattern_bytes) { |
| 45 | case 0: |
| 46 | fill_random_bytes(td, p, len); |
| 47 | break; |
| 48 | case 1: |
| 49 | memset(p, td->o.verify_pattern, len); |
| 50 | break; |
| 51 | case 2: |
| 52 | case 3: |
| 53 | case 4: { |
| 54 | unsigned int pattern = td->o.verify_pattern; |
| 55 | unsigned int i = 0; |
| 56 | unsigned char c1, c2, c3, c4; |
| 57 | unsigned char *b = p; |
| 58 | |
| 59 | c1 = pattern & 0xff; |
| 60 | pattern >>= 8; |
| 61 | c2 = pattern & 0xff; |
| 62 | pattern >>= 8; |
| 63 | c3 = pattern & 0xff; |
| 64 | pattern >>= 8; |
| 65 | c4 = pattern & 0xff; |
| 66 | |
| 67 | while (i < len) { |
| 68 | b[i++] = c1; |
| 69 | if (i == len) |
| 70 | break; |
| 71 | b[i++] = c2; |
| 72 | if (td->o.verify_pattern_bytes == 2 || i == len) |
| 73 | continue; |
| 74 | b[i++] = c3; |
| 75 | if (td->o.verify_pattern_bytes == 3 || i == len) |
| 76 | continue; |
| 77 | b[i++] = c4; |
| 78 | } |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static void memswp(void* buf1, void* buf2, unsigned int len) |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 85 | { |
| 86 | struct verify_header swap; |
Jens Axboe | 90059d6 | 2007-07-30 09:33:12 +0200 | [diff] [blame] | 87 | |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 88 | memcpy(&swap, buf1, len); |
| 89 | memcpy(buf1, buf2, len); |
| 90 | memcpy(buf2, &swap, len); |
| 91 | } |
| 92 | |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 93 | static void hexdump(void *buffer, int len) |
| 94 | { |
| 95 | unsigned char *p = buffer; |
| 96 | int i; |
| 97 | |
| 98 | for (i = 0; i < len; i++) |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 99 | log_info("%02x", p[i]); |
| 100 | log_info("\n"); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 103 | /* |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 104 | * Prepare for seperation of verify_header and checksum header |
| 105 | */ |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 106 | static inline unsigned int __hdr_size(int verify_type) |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 107 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 108 | unsigned int len; |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 109 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 110 | switch (verify_type) { |
| 111 | case VERIFY_NONE: |
| 112 | case VERIFY_NULL: |
| 113 | len = 0; |
| 114 | break; |
| 115 | case VERIFY_MD5: |
| 116 | len = sizeof(struct vhdr_md5); |
| 117 | break; |
| 118 | case VERIFY_CRC64: |
| 119 | len = sizeof(struct vhdr_crc64); |
| 120 | break; |
| 121 | case VERIFY_CRC32: |
| 122 | len = sizeof(struct vhdr_crc32); |
| 123 | break; |
| 124 | case VERIFY_CRC16: |
| 125 | len = sizeof(struct vhdr_crc16); |
| 126 | break; |
| 127 | case VERIFY_CRC7: |
| 128 | len = sizeof(struct vhdr_crc7); |
| 129 | break; |
| 130 | case VERIFY_SHA256: |
| 131 | len = sizeof(struct vhdr_sha256); |
| 132 | break; |
| 133 | case VERIFY_SHA512: |
| 134 | len = sizeof(struct vhdr_sha512); |
| 135 | break; |
Shawn Lewis | 7437ee8 | 2007-08-02 21:05:58 +0200 | [diff] [blame^] | 136 | case VERIFY_META: |
| 137 | len = sizeof(struct vhdr_meta); |
| 138 | break; |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 139 | default: |
| 140 | log_err("fio: unknown verify header!\n"); |
| 141 | assert(0); |
| 142 | } |
| 143 | |
| 144 | return len + sizeof(struct verify_header); |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static inline unsigned int hdr_size(struct verify_header *hdr) |
| 148 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 149 | return __hdr_size(hdr->verify_type); |
| 150 | } |
| 151 | |
| 152 | static void *hdr_priv(struct verify_header *hdr) |
| 153 | { |
| 154 | void *priv = hdr; |
| 155 | |
| 156 | return priv + sizeof(struct verify_header); |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 160 | * Return data area 'header_num' |
| 161 | */ |
| 162 | static inline void *io_u_verify_off(struct verify_header *hdr, |
| 163 | struct io_u *io_u, |
| 164 | unsigned char header_num) |
| 165 | { |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 166 | return io_u->buf + header_num * hdr->len + hdr_size(hdr); |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Shawn Lewis | 7437ee8 | 2007-08-02 21:05:58 +0200 | [diff] [blame^] | 169 | static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td, |
| 170 | struct io_u *io_u, unsigned int header_num) |
| 171 | { |
| 172 | struct vhdr_meta *vh = hdr_priv(hdr); |
| 173 | |
| 174 | if (vh->offset != io_u->offset + header_num * td->o.verify_interval) { |
| 175 | log_err("meta: verify failed at %llu/%u\n", |
| 176 | io_u->offset + header_num * hdr->len, |
| 177 | hdr->len); |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 184 | static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u, |
| 185 | unsigned int header_num) |
| 186 | { |
| 187 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 188 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 189 | uint8_t sha512[128]; |
| 190 | struct sha512_ctx sha512_ctx = { |
| 191 | .buf = sha512, |
| 192 | }; |
| 193 | |
| 194 | sha512_init(&sha512_ctx); |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 195 | sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr)); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 196 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 197 | if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) { |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 198 | log_err("sha512: verify failed at %llu/%u\n", |
| 199 | io_u->offset + header_num * hdr->len, |
| 200 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 201 | hexdump(vh->sha512, sizeof(vh->sha512)); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 202 | hexdump(sha512_ctx.buf, sizeof(sha512)); |
| 203 | return 1; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u, |
| 210 | unsigned int header_num) |
| 211 | { |
| 212 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 213 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 214 | uint8_t sha256[128]; |
| 215 | struct sha256_ctx sha256_ctx = { |
| 216 | .buf = sha256, |
| 217 | }; |
| 218 | |
| 219 | sha256_init(&sha256_ctx); |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 220 | sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr)); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 221 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 222 | if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) { |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 223 | log_err("sha256: verify failed at %llu/%u\n", |
| 224 | io_u->offset + header_num * hdr->len, |
| 225 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 226 | hexdump(vh->sha256, sizeof(vh->sha256)); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 227 | hexdump(sha256_ctx.buf, sizeof(sha256)); |
| 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 234 | static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u, |
| 235 | unsigned char header_num) |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 236 | { |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 237 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 238 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 239 | unsigned char c; |
| 240 | |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 241 | c = crc7(p, hdr->len - hdr_size(hdr)); |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 242 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 243 | if (c != vh->crc7) { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 244 | log_err("crc7: verify failed at %llu/%u\n", |
| 245 | io_u->offset + header_num * hdr->len, |
| 246 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 247 | log_err("crc7: wanted %x, got %x\n", vh->crc7, c); |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 254 | static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u, |
| 255 | unsigned int header_num) |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 256 | { |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 257 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 258 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 259 | unsigned short c; |
| 260 | |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 261 | c = crc16(p, hdr->len - hdr_size(hdr)); |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 262 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 263 | if (c != vh->crc16) { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 264 | log_err("crc16: verify failed at %llu/%u\n", |
| 265 | io_u->offset + header_num * hdr->len, |
| 266 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 267 | log_err("crc16: wanted %x, got %x\n", vh->crc16, c); |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 274 | static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u, |
| 275 | unsigned int header_num) |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 276 | { |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 277 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 278 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 279 | unsigned long long c; |
| 280 | |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 281 | c = crc64(p, hdr->len - hdr_size(hdr)); |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 282 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 283 | if (c != vh->crc64) { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 284 | log_err("crc64: verify failed at %llu/%u\n", |
| 285 | io_u->offset + header_num * hdr->len, |
| 286 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 287 | log_err("crc64: wanted %llx, got %llx\n", (unsigned long long) vh->crc64, c); |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 288 | return 1; |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 294 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u, |
| 295 | unsigned int header_num) |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 296 | { |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 297 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 298 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
| 299 | uint32_t c; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 300 | |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 301 | c = crc32(p, hdr->len - hdr_size(hdr)); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 302 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 303 | if (c != vh->crc32) { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 304 | log_err("crc32: verify failed at %llu/%u\n", |
| 305 | io_u->offset + header_num * hdr->len, |
| 306 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 307 | log_err("crc32: wanted %x, got %x\n", vh->crc32, c); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 308 | return 1; |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 314 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u, |
| 315 | unsigned int header_num) |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 316 | { |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 317 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 318 | struct vhdr_md5 *vh = hdr_priv(hdr); |
Jens Axboe | 8c43232 | 2007-07-27 13:16:24 +0200 | [diff] [blame] | 319 | uint32_t hash[MD5_HASH_WORDS]; |
| 320 | struct md5_ctx md5_ctx = { |
| 321 | .hash = hash, |
| 322 | }; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 323 | |
Jens Axboe | 61f821f | 2007-07-30 10:18:06 +0200 | [diff] [blame] | 324 | md5_init(&md5_ctx); |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 325 | md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr)); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 326 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 327 | if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 328 | log_err("md5: verify failed at %llu/%u\n", |
| 329 | io_u->offset + header_num * hdr->len, |
| 330 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 331 | hexdump(vh->md5_digest, sizeof(vh->md5_digest)); |
Jens Axboe | 8c43232 | 2007-07-27 13:16:24 +0200 | [diff] [blame] | 332 | hexdump(md5_ctx.hash, sizeof(hash)); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 333 | return 1; |
| 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
Jens Axboe | 36690c9 | 2007-03-26 10:23:34 +0200 | [diff] [blame] | 339 | int verify_io_u(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 340 | { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 341 | struct verify_header *hdr; |
| 342 | unsigned int hdr_inc, hdr_num = 0; |
Jens Axboe | 9564610 | 2007-07-28 21:17:50 +0200 | [diff] [blame] | 343 | void *p; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 344 | int ret; |
| 345 | |
Shawn Lewis | 1dcc049 | 2007-07-27 08:02:45 +0200 | [diff] [blame] | 346 | if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ) |
Jens Axboe | 36690c9 | 2007-03-26 10:23:34 +0200 | [diff] [blame] | 347 | return 0; |
| 348 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 349 | hdr_inc = io_u->buflen; |
Jens Axboe | a59e170 | 2007-07-30 08:53:27 +0200 | [diff] [blame] | 350 | if (td->o.verify_interval) |
| 351 | hdr_inc = td->o.verify_interval; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 352 | |
Jens Axboe | 9564610 | 2007-07-28 21:17:50 +0200 | [diff] [blame] | 353 | for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) { |
Jens Axboe | a59e170 | 2007-07-30 08:53:27 +0200 | [diff] [blame] | 354 | if (td->o.verify_offset) |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 355 | memswp(p, p + td->o.verify_offset, __hdr_size(td->o.verify)); |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 356 | |
Jens Axboe | 9564610 | 2007-07-28 21:17:50 +0200 | [diff] [blame] | 357 | hdr = p; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 358 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 359 | if (hdr->fio_magic != FIO_HDR_MAGIC) { |
| 360 | log_err("Bad verify header %x\n", hdr->fio_magic); |
| 361 | return EIO; |
| 362 | } |
| 363 | |
| 364 | switch (hdr->verify_type) { |
| 365 | case VERIFY_MD5: |
| 366 | ret = verify_io_u_md5(hdr, io_u, hdr_num); |
| 367 | break; |
| 368 | case VERIFY_CRC64: |
| 369 | ret = verify_io_u_crc64(hdr, io_u, hdr_num); |
| 370 | break; |
| 371 | case VERIFY_CRC32: |
| 372 | ret = verify_io_u_crc32(hdr, io_u, hdr_num); |
| 373 | break; |
| 374 | case VERIFY_CRC16: |
| 375 | ret = verify_io_u_crc16(hdr, io_u, hdr_num); |
| 376 | break; |
| 377 | case VERIFY_CRC7: |
| 378 | ret = verify_io_u_crc7(hdr, io_u, hdr_num); |
| 379 | break; |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 380 | case VERIFY_SHA256: |
| 381 | ret = verify_io_u_sha256(hdr, io_u, hdr_num); |
| 382 | break; |
| 383 | case VERIFY_SHA512: |
| 384 | ret = verify_io_u_sha512(hdr, io_u, hdr_num); |
| 385 | break; |
Shawn Lewis | 7437ee8 | 2007-08-02 21:05:58 +0200 | [diff] [blame^] | 386 | case VERIFY_META: |
| 387 | ret = verify_io_u_meta(hdr, td, io_u, hdr_num); |
| 388 | break; |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 389 | default: |
| 390 | log_err("Bad verify type %u\n", hdr->verify_type); |
| 391 | ret = 1; |
| 392 | } |
| 393 | hdr_num++; |
| 394 | } |
Jens Axboe | a7dfe86 | 2007-03-12 10:05:08 +0100 | [diff] [blame] | 395 | |
| 396 | return 0; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Shawn Lewis | 7437ee8 | 2007-08-02 21:05:58 +0200 | [diff] [blame^] | 399 | static void fill_meta(struct verify_header *hdr, struct thread_data *td, |
| 400 | struct io_u *io_u, unsigned int header_num) |
| 401 | { |
| 402 | struct vhdr_meta *vh = hdr_priv(hdr); |
| 403 | |
| 404 | vh->thread = td->thread_number; |
| 405 | |
| 406 | vh->time_sec = io_u->start_time.tv_sec; |
| 407 | vh->time_usec = io_u->start_time.tv_usec; |
| 408 | |
| 409 | vh->numberio = td->io_issues[DDIR_WRITE]; |
| 410 | |
| 411 | vh->offset = io_u->offset + header_num * td->o.verify_interval; |
| 412 | } |
| 413 | |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 414 | static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len) |
| 415 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 416 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 417 | struct sha512_ctx sha512_ctx = { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 418 | .buf = vh->sha512, |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | sha512_init(&sha512_ctx); |
| 422 | sha512_update(&sha512_ctx, p, len); |
| 423 | } |
| 424 | |
| 425 | static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len) |
| 426 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 427 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 428 | struct sha256_ctx sha256_ctx = { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 429 | .buf = vh->sha256, |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 430 | }; |
| 431 | |
| 432 | sha256_init(&sha256_ctx); |
| 433 | sha256_update(&sha256_ctx, p, len); |
| 434 | } |
| 435 | |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 436 | static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len) |
| 437 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 438 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
| 439 | |
| 440 | vh->crc7 = crc7(p, len); |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 443 | static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len) |
| 444 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 445 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
| 446 | |
| 447 | vh->crc16 = crc16(p, len); |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 450 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
| 451 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 452 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
| 453 | |
| 454 | vh->crc32 = crc32(p, len); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 457 | static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len) |
| 458 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 459 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
| 460 | |
| 461 | vh->crc64 = crc64(p, len); |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 462 | } |
| 463 | |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 464 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
| 465 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 466 | struct vhdr_md5 *vh = hdr_priv(hdr); |
Jens Axboe | 8c43232 | 2007-07-27 13:16:24 +0200 | [diff] [blame] | 467 | struct md5_ctx md5_ctx = { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame] | 468 | .hash = (uint32_t *) vh->md5_digest, |
Jens Axboe | 8c43232 | 2007-07-27 13:16:24 +0200 | [diff] [blame] | 469 | }; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 470 | |
Jens Axboe | 61f821f | 2007-07-30 10:18:06 +0200 | [diff] [blame] | 471 | md5_init(&md5_ctx); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 472 | md5_update(&md5_ctx, p, len); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* |
| 476 | * fill body of io_u->buf with random data and add a header with the |
| 477 | * crc32 or md5 sum of that data. |
| 478 | */ |
| 479 | void populate_verify_io_u(struct thread_data *td, struct io_u *io_u) |
| 480 | { |
Jens Axboe | baefa9b | 2007-07-27 12:57:25 +0200 | [diff] [blame] | 481 | struct verify_header *hdr; |
Jens Axboe | 9564610 | 2007-07-28 21:17:50 +0200 | [diff] [blame] | 482 | void *p = io_u->buf, *data; |
Shawn Lewis | 7437ee8 | 2007-08-02 21:05:58 +0200 | [diff] [blame^] | 483 | unsigned int hdr_inc, data_len, header_num = 0; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 484 | |
Jens Axboe | 9cc3d15 | 2007-03-26 10:32:30 +0200 | [diff] [blame] | 485 | if (td->o.verify == VERIFY_NULL) |
| 486 | return; |
| 487 | |
Jens Axboe | 90059d6 | 2007-07-30 09:33:12 +0200 | [diff] [blame] | 488 | fill_pattern(td, p, io_u->buflen); |
Jens Axboe | baefa9b | 2007-07-27 12:57:25 +0200 | [diff] [blame] | 489 | |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 490 | hdr_inc = io_u->buflen; |
Jens Axboe | a59e170 | 2007-07-30 08:53:27 +0200 | [diff] [blame] | 491 | if (td->o.verify_interval) |
| 492 | hdr_inc = td->o.verify_interval; |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 493 | |
Jens Axboe | 9564610 | 2007-07-28 21:17:50 +0200 | [diff] [blame] | 494 | for (;p < io_u->buf + io_u->buflen; p += hdr_inc) { |
| 495 | hdr = p; |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 496 | |
| 497 | hdr->fio_magic = FIO_HDR_MAGIC; |
| 498 | hdr->verify_type = td->o.verify; |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 499 | hdr->len = hdr_inc; |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 500 | data_len = hdr_inc - hdr_size(hdr); |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 501 | |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 502 | data = p + hdr_size(hdr); |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 503 | switch (td->o.verify) { |
| 504 | case VERIFY_MD5: |
| 505 | fill_md5(hdr, data, data_len); |
| 506 | break; |
| 507 | case VERIFY_CRC64: |
| 508 | fill_crc64(hdr, data, data_len); |
| 509 | break; |
| 510 | case VERIFY_CRC32: |
| 511 | fill_crc32(hdr, data, data_len); |
| 512 | break; |
| 513 | case VERIFY_CRC16: |
| 514 | fill_crc16(hdr, data, data_len); |
| 515 | break; |
| 516 | case VERIFY_CRC7: |
| 517 | fill_crc7(hdr, data, data_len); |
| 518 | break; |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 519 | case VERIFY_SHA256: |
| 520 | fill_sha256(hdr, data, data_len); |
| 521 | break; |
| 522 | case VERIFY_SHA512: |
| 523 | fill_sha512(hdr, data, data_len); |
| 524 | break; |
Shawn Lewis | 7437ee8 | 2007-08-02 21:05:58 +0200 | [diff] [blame^] | 525 | case VERIFY_META: |
| 526 | fill_meta(hdr, td, io_u, header_num); |
| 527 | break; |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 528 | default: |
| 529 | log_err("fio: bad verify type: %d\n", td->o.verify); |
| 530 | assert(0); |
| 531 | } |
Jens Axboe | a59e170 | 2007-07-30 08:53:27 +0200 | [diff] [blame] | 532 | if (td->o.verify_offset) |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 533 | memswp(p, p + td->o.verify_offset, hdr_size(hdr)); |
Shawn Lewis | 7437ee8 | 2007-08-02 21:05:58 +0200 | [diff] [blame^] | 534 | header_num++; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 535 | } |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | int get_next_verify(struct thread_data *td, struct io_u *io_u) |
| 539 | { |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 540 | struct io_piece *ipo = NULL; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 541 | |
Jens Axboe | d2d7fa5 | 2007-02-19 13:21:35 +0100 | [diff] [blame] | 542 | /* |
| 543 | * this io_u is from a requeue, we already filled the offsets |
| 544 | */ |
| 545 | if (io_u->file) |
| 546 | return 0; |
| 547 | |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 548 | if (!RB_EMPTY_ROOT(&td->io_hist_tree)) { |
| 549 | struct rb_node *n = rb_first(&td->io_hist_tree); |
| 550 | |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 551 | ipo = rb_entry(n, struct io_piece, rb_node); |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 552 | rb_erase(n, &td->io_hist_tree); |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 553 | } else if (!list_empty(&td->io_hist_list)) { |
| 554 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); |
| 555 | list_del(&ipo->list); |
| 556 | } |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 557 | |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 558 | if (ipo) { |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 559 | io_u->offset = ipo->offset; |
| 560 | io_u->buflen = ipo->len; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 561 | io_u->file = ipo->file; |
Jens Axboe | 97af62c | 2007-05-22 11:12:13 +0200 | [diff] [blame] | 562 | |
| 563 | if ((io_u->file->flags & FIO_FILE_OPEN) == 0) { |
| 564 | int r = td_io_open_file(td, io_u->file); |
| 565 | |
| 566 | if (r) |
| 567 | return 1; |
| 568 | } |
| 569 | |
| 570 | get_file(ipo->file); |
| 571 | assert(io_u->file->flags & FIO_FILE_OPEN); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 572 | io_u->ddir = DDIR_READ; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 573 | io_u->xfer_buf = io_u->buf; |
| 574 | io_u->xfer_buflen = io_u->buflen; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 575 | free(ipo); |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | return 1; |
| 580 | } |