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 | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 11 | #include "md5.h" |
| 12 | #include "crc64.h" |
| 13 | #include "crc32.h" |
| 14 | #include "crc16.h" |
| 15 | #include "crc7.h" |
| 16 | #include "sha256.h" |
| 17 | #include "sha512.h" |
| 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; |
| 136 | default: |
| 137 | log_err("fio: unknown verify header!\n"); |
| 138 | assert(0); |
| 139 | } |
| 140 | |
| 141 | return len + sizeof(struct verify_header); |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static inline unsigned int hdr_size(struct verify_header *hdr) |
| 145 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 146 | return __hdr_size(hdr->verify_type); |
| 147 | } |
| 148 | |
| 149 | static void *hdr_priv(struct verify_header *hdr) |
| 150 | { |
| 151 | void *priv = hdr; |
| 152 | |
| 153 | return priv + sizeof(struct verify_header); |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 157 | * Return data area 'header_num' |
| 158 | */ |
| 159 | static inline void *io_u_verify_off(struct verify_header *hdr, |
| 160 | struct io_u *io_u, |
| 161 | unsigned char header_num) |
| 162 | { |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 163 | return io_u->buf + header_num * hdr->len + hdr_size(hdr); |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 166 | static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u, |
| 167 | unsigned int header_num) |
| 168 | { |
| 169 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 170 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 171 | uint8_t sha512[128]; |
| 172 | struct sha512_ctx sha512_ctx = { |
| 173 | .buf = sha512, |
| 174 | }; |
| 175 | |
| 176 | sha512_init(&sha512_ctx); |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 177 | sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr)); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 178 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 179 | if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) { |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 180 | log_err("sha512: verify failed at %llu/%u\n", |
| 181 | io_u->offset + header_num * hdr->len, |
| 182 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 183 | hexdump(vh->sha512, sizeof(vh->sha512)); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 184 | hexdump(sha512_ctx.buf, sizeof(sha512)); |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u, |
| 192 | unsigned int header_num) |
| 193 | { |
| 194 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 195 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 196 | uint8_t sha256[128]; |
| 197 | struct sha256_ctx sha256_ctx = { |
| 198 | .buf = sha256, |
| 199 | }; |
| 200 | |
| 201 | sha256_init(&sha256_ctx); |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 202 | sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr)); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 203 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 204 | if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) { |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 205 | log_err("sha256: verify failed at %llu/%u\n", |
| 206 | io_u->offset + header_num * hdr->len, |
| 207 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 208 | hexdump(vh->sha256, sizeof(vh->sha256)); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 209 | hexdump(sha256_ctx.buf, sizeof(sha256)); |
| 210 | return 1; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 216 | static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u, |
| 217 | unsigned char header_num) |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 218 | { |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 219 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 220 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 221 | unsigned char c; |
| 222 | |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 223 | c = crc7(p, hdr->len - hdr_size(hdr)); |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 224 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 225 | if (c != vh->crc7) { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 226 | log_err("crc7: verify failed at %llu/%u\n", |
| 227 | io_u->offset + header_num * hdr->len, |
| 228 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 229 | log_err("crc7: wanted %x, got %x\n", vh->crc7, c); |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 236 | static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u, |
| 237 | unsigned int header_num) |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 238 | { |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 239 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 240 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 241 | unsigned short c; |
| 242 | |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 243 | c = crc16(p, hdr->len - hdr_size(hdr)); |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 244 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 245 | if (c != vh->crc16) { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 246 | log_err("crc16: verify failed at %llu/%u\n", |
| 247 | io_u->offset + header_num * hdr->len, |
| 248 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 249 | log_err("crc16: wanted %x, got %x\n", vh->crc16, c); |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 256 | static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u, |
| 257 | unsigned int header_num) |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 258 | { |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 259 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 260 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 261 | unsigned long long c; |
| 262 | |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 263 | c = crc64(p, hdr->len - hdr_size(hdr)); |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 264 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 265 | if (c != vh->crc64) { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 266 | log_err("crc64: verify failed at %llu/%u\n", |
| 267 | io_u->offset + header_num * hdr->len, |
| 268 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 269 | 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] | 270 | return 1; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 276 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u, |
| 277 | unsigned int header_num) |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 278 | { |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 279 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 280 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
| 281 | uint32_t c; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 282 | |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 283 | c = crc32(p, hdr->len - hdr_size(hdr)); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 284 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 285 | if (c != vh->crc32) { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 286 | log_err("crc32: verify failed at %llu/%u\n", |
| 287 | io_u->offset + header_num * hdr->len, |
| 288 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 289 | log_err("crc32: wanted %x, got %x\n", vh->crc32, c); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 290 | return 1; |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 296 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u, |
| 297 | unsigned int header_num) |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 298 | { |
Jens Axboe | d9f2caf | 2007-07-28 21:22:03 +0200 | [diff] [blame] | 299 | void *p = io_u_verify_off(hdr, io_u, header_num); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 300 | struct vhdr_md5 *vh = hdr_priv(hdr); |
Jens Axboe | 8c43232 | 2007-07-27 13:16:24 +0200 | [diff] [blame] | 301 | uint32_t hash[MD5_HASH_WORDS]; |
| 302 | struct md5_ctx md5_ctx = { |
| 303 | .hash = hash, |
| 304 | }; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 305 | |
Jens Axboe | 61f821f | 2007-07-30 10:18:06 +0200 | [diff] [blame] | 306 | md5_init(&md5_ctx); |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 307 | md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr)); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 308 | |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 309 | if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 310 | log_err("md5: verify failed at %llu/%u\n", |
| 311 | io_u->offset + header_num * hdr->len, |
| 312 | hdr->len); |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 313 | hexdump(vh->md5_digest, sizeof(vh->md5_digest)); |
Jens Axboe | 8c43232 | 2007-07-27 13:16:24 +0200 | [diff] [blame] | 314 | hexdump(md5_ctx.hash, sizeof(hash)); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 315 | return 1; |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
Jens Axboe | 36690c9 | 2007-03-26 10:23:34 +0200 | [diff] [blame] | 321 | 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] | 322 | { |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 323 | struct verify_header *hdr; |
| 324 | unsigned int hdr_inc, hdr_num = 0; |
Jens Axboe | 9564610 | 2007-07-28 21:17:50 +0200 | [diff] [blame] | 325 | void *p; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 326 | int ret; |
| 327 | |
Shawn Lewis | 1dcc049 | 2007-07-27 08:02:45 +0200 | [diff] [blame] | 328 | if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ) |
Jens Axboe | 36690c9 | 2007-03-26 10:23:34 +0200 | [diff] [blame] | 329 | return 0; |
| 330 | |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 331 | hdr_inc = io_u->buflen; |
Jens Axboe | a59e170 | 2007-07-30 08:53:27 +0200 | [diff] [blame] | 332 | if (td->o.verify_interval) |
| 333 | hdr_inc = td->o.verify_interval; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 334 | |
Jens Axboe | 9564610 | 2007-07-28 21:17:50 +0200 | [diff] [blame] | 335 | 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] | 336 | if (td->o.verify_offset) |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 337 | memswp(p, p + td->o.verify_offset, __hdr_size(td->o.verify)); |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 338 | |
Jens Axboe | 9564610 | 2007-07-28 21:17:50 +0200 | [diff] [blame] | 339 | hdr = p; |
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 | if (hdr->fio_magic != FIO_HDR_MAGIC) { |
| 342 | log_err("Bad verify header %x\n", hdr->fio_magic); |
| 343 | return EIO; |
| 344 | } |
| 345 | |
| 346 | switch (hdr->verify_type) { |
| 347 | case VERIFY_MD5: |
| 348 | ret = verify_io_u_md5(hdr, io_u, hdr_num); |
| 349 | break; |
| 350 | case VERIFY_CRC64: |
| 351 | ret = verify_io_u_crc64(hdr, io_u, hdr_num); |
| 352 | break; |
| 353 | case VERIFY_CRC32: |
| 354 | ret = verify_io_u_crc32(hdr, io_u, hdr_num); |
| 355 | break; |
| 356 | case VERIFY_CRC16: |
| 357 | ret = verify_io_u_crc16(hdr, io_u, hdr_num); |
| 358 | break; |
| 359 | case VERIFY_CRC7: |
| 360 | ret = verify_io_u_crc7(hdr, io_u, hdr_num); |
| 361 | break; |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 362 | case VERIFY_SHA256: |
| 363 | ret = verify_io_u_sha256(hdr, io_u, hdr_num); |
| 364 | break; |
| 365 | case VERIFY_SHA512: |
| 366 | ret = verify_io_u_sha512(hdr, io_u, hdr_num); |
| 367 | break; |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 368 | default: |
| 369 | log_err("Bad verify type %u\n", hdr->verify_type); |
| 370 | ret = 1; |
| 371 | } |
| 372 | hdr_num++; |
| 373 | } |
Jens Axboe | a7dfe86 | 2007-03-12 10:05:08 +0100 | [diff] [blame] | 374 | |
| 375 | return 0; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 378 | static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len) |
| 379 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 380 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 381 | struct sha512_ctx sha512_ctx = { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 382 | .buf = vh->sha512, |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | sha512_init(&sha512_ctx); |
| 386 | sha512_update(&sha512_ctx, p, len); |
| 387 | } |
| 388 | |
| 389 | static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len) |
| 390 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 391 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 392 | struct sha256_ctx sha256_ctx = { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 393 | .buf = vh->sha256, |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | sha256_init(&sha256_ctx); |
| 397 | sha256_update(&sha256_ctx, p, len); |
| 398 | } |
| 399 | |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 400 | static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len) |
| 401 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 402 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
| 403 | |
| 404 | vh->crc7 = crc7(p, len); |
Jens Axboe | 1e154bd | 2007-07-27 09:52:40 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 407 | static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len) |
| 408 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 409 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
| 410 | |
| 411 | vh->crc16 = crc16(p, len); |
Jens Axboe | 969f7ed | 2007-07-27 09:07:17 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 414 | static void fill_crc32(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_crc32 *vh = hdr_priv(hdr); |
| 417 | |
| 418 | vh->crc32 = crc32(p, len); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 421 | static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len) |
| 422 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 423 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
| 424 | |
| 425 | vh->crc64 = crc64(p, len); |
Jens Axboe | d77a7af | 2007-07-27 15:35:06 +0200 | [diff] [blame] | 426 | } |
| 427 | |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 428 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
| 429 | { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 430 | struct vhdr_md5 *vh = hdr_priv(hdr); |
Jens Axboe | 8c43232 | 2007-07-27 13:16:24 +0200 | [diff] [blame] | 431 | struct md5_ctx md5_ctx = { |
Jens Axboe | 546dfd9 | 2007-07-30 12:23:05 +0200 | [diff] [blame^] | 432 | .hash = (uint32_t *) vh->md5_digest, |
Jens Axboe | 8c43232 | 2007-07-27 13:16:24 +0200 | [diff] [blame] | 433 | }; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 434 | |
Jens Axboe | 61f821f | 2007-07-30 10:18:06 +0200 | [diff] [blame] | 435 | md5_init(&md5_ctx); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 436 | md5_update(&md5_ctx, p, len); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* |
| 440 | * fill body of io_u->buf with random data and add a header with the |
| 441 | * crc32 or md5 sum of that data. |
| 442 | */ |
| 443 | void populate_verify_io_u(struct thread_data *td, struct io_u *io_u) |
| 444 | { |
Jens Axboe | baefa9b | 2007-07-27 12:57:25 +0200 | [diff] [blame] | 445 | struct verify_header *hdr; |
Jens Axboe | 9564610 | 2007-07-28 21:17:50 +0200 | [diff] [blame] | 446 | void *p = io_u->buf, *data; |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 447 | unsigned int hdr_inc, data_len; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 448 | |
Jens Axboe | 9cc3d15 | 2007-03-26 10:32:30 +0200 | [diff] [blame] | 449 | if (td->o.verify == VERIFY_NULL) |
| 450 | return; |
| 451 | |
Jens Axboe | 90059d6 | 2007-07-30 09:33:12 +0200 | [diff] [blame] | 452 | fill_pattern(td, p, io_u->buflen); |
Jens Axboe | baefa9b | 2007-07-27 12:57:25 +0200 | [diff] [blame] | 453 | |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 454 | hdr_inc = io_u->buflen; |
Jens Axboe | a59e170 | 2007-07-30 08:53:27 +0200 | [diff] [blame] | 455 | if (td->o.verify_interval) |
| 456 | hdr_inc = td->o.verify_interval; |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 457 | |
Jens Axboe | 9564610 | 2007-07-28 21:17:50 +0200 | [diff] [blame] | 458 | for (;p < io_u->buf + io_u->buflen; p += hdr_inc) { |
| 459 | hdr = p; |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 460 | |
| 461 | hdr->fio_magic = FIO_HDR_MAGIC; |
| 462 | hdr->verify_type = td->o.verify; |
Shawn Lewis | 546a914 | 2007-07-28 21:11:37 +0200 | [diff] [blame] | 463 | hdr->len = hdr_inc; |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 464 | data_len = hdr_inc - hdr_size(hdr); |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 465 | |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 466 | data = p + hdr_size(hdr); |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 467 | switch (td->o.verify) { |
| 468 | case VERIFY_MD5: |
| 469 | fill_md5(hdr, data, data_len); |
| 470 | break; |
| 471 | case VERIFY_CRC64: |
| 472 | fill_crc64(hdr, data, data_len); |
| 473 | break; |
| 474 | case VERIFY_CRC32: |
| 475 | fill_crc32(hdr, data, data_len); |
| 476 | break; |
| 477 | case VERIFY_CRC16: |
| 478 | fill_crc16(hdr, data, data_len); |
| 479 | break; |
| 480 | case VERIFY_CRC7: |
| 481 | fill_crc7(hdr, data, data_len); |
| 482 | break; |
Jens Axboe | cd14cc1 | 2007-07-30 10:59:33 +0200 | [diff] [blame] | 483 | case VERIFY_SHA256: |
| 484 | fill_sha256(hdr, data, data_len); |
| 485 | break; |
| 486 | case VERIFY_SHA512: |
| 487 | fill_sha512(hdr, data, data_len); |
| 488 | break; |
Shawn Lewis | 3f9f4e2 | 2007-07-28 21:10:37 +0200 | [diff] [blame] | 489 | default: |
| 490 | log_err("fio: bad verify type: %d\n", td->o.verify); |
| 491 | assert(0); |
| 492 | } |
Jens Axboe | a59e170 | 2007-07-30 08:53:27 +0200 | [diff] [blame] | 493 | if (td->o.verify_offset) |
Jens Axboe | 8767783 | 2007-07-30 12:08:14 +0200 | [diff] [blame] | 494 | memswp(p, p + td->o.verify_offset, hdr_size(hdr)); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 495 | } |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | int get_next_verify(struct thread_data *td, struct io_u *io_u) |
| 499 | { |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 500 | struct io_piece *ipo = NULL; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 501 | |
Jens Axboe | d2d7fa5 | 2007-02-19 13:21:35 +0100 | [diff] [blame] | 502 | /* |
| 503 | * this io_u is from a requeue, we already filled the offsets |
| 504 | */ |
| 505 | if (io_u->file) |
| 506 | return 0; |
| 507 | |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 508 | if (!RB_EMPTY_ROOT(&td->io_hist_tree)) { |
| 509 | struct rb_node *n = rb_first(&td->io_hist_tree); |
| 510 | |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 511 | ipo = rb_entry(n, struct io_piece, rb_node); |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 512 | rb_erase(n, &td->io_hist_tree); |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 513 | } else if (!list_empty(&td->io_hist_list)) { |
| 514 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); |
| 515 | list_del(&ipo->list); |
| 516 | } |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 517 | |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 518 | if (ipo) { |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 519 | io_u->offset = ipo->offset; |
| 520 | io_u->buflen = ipo->len; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 521 | io_u->file = ipo->file; |
Jens Axboe | 97af62c | 2007-05-22 11:12:13 +0200 | [diff] [blame] | 522 | |
| 523 | if ((io_u->file->flags & FIO_FILE_OPEN) == 0) { |
| 524 | int r = td_io_open_file(td, io_u->file); |
| 525 | |
| 526 | if (r) |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | get_file(ipo->file); |
| 531 | assert(io_u->file->flags & FIO_FILE_OPEN); |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 532 | io_u->ddir = DDIR_READ; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 533 | io_u->xfer_buf = io_u->buf; |
| 534 | io_u->xfer_buflen = io_u->buflen; |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame] | 535 | free(ipo); |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | return 1; |
| 540 | } |