1 #include <stdio.h> 2 #include <unistd.h> 3 #include <stdlib.h> 4 #include <fcntl.h> 5 #include <stropts.h> 6 #include <poll.h> 7 #include <string.h> 8 #include <sys/fs/zev.h> 9 #include <errno.h> 10 #include <sys/sysmacros.h> 11 #include <stdarg.h> 12 #include <sys/avl.h> 13 #include <sys/stat.h> 14 15 #define ZEV_DEVICE "/devices/pseudo/zev@0:ctrl" 16 17 #if !defined(offsetof) 18 #define offsetof(s, m) ((size_t)(&(((s *)0)->m))) 19 #endif 20 21 static char *zev_device = ZEV_DEVICE; 22 23 static char *zev_op_name[] = { 24 "ERROR", 25 "MARK", 26 "ZFS_MOUNT", 27 "ZFS_UMOUNT", 28 "ZVOL_WRITE", 29 "ZVOL_TRUNCATE", 30 "ZNODE_CLOSE_AFTER_UPDATE", 31 "ZNODE_CREATE", 32 "ZNODE_MKDIR", 33 "ZNODE_MAKE_XATTR_DIR", 34 "ZNODE_REMOVE", 35 "ZNODE_RMDIR", 36 "ZNODE_LINK", 37 "ZNODE_SYMLINK", 38 "ZNODE_RENAME", 39 "ZNODE_WRITE", 40 "ZNODE_TRUNCATE", 41 "ZNODE_SETATTR", 42 "ZNODE_ACL", 43 NULL 44 }; 45 46 #define MD_STATISTICS 1 47 #define MD_POLL_EVENTS 2 48 #define MD_CHECKSUMS 3 49 #define MD_DEBUG_INFO 4 50 #define MD_LIST_QUEUES 5 51 #define MD_SET_GLOBAL_MAX_QUEUE_LEN 6 52 #define MD_SET_MAX_QUEUE_LEN 7 53 #define MD_SET_POLL_WAKEUP_QUEUE_LEN 8 54 #define MD_MUTE_POOL 9 55 #define MD_UNMUTE_POOL 10 56 #define MD_MARK 11 57 #define MD_ADD_QUEUE 12 58 #define MD_ADD_BLOCKING_QUEUE 13 59 #define MD_REMOVE_QUEUE 14 60 #define MD_QUEUE_BLOCKING 15 61 #define MD_QUEUE_NONBLOCKING 16 62 #define MD_QUEUE_PROPERTIES 17 63 #define MD_ZEVSTAT 18 64 #define MD_ZEV_REPORT 19 65 #define MD_DUMP_SPOOL 20 66 #define MD_GET_ZEV_VERSION 21 67 68 static int verbose = 0; 69 static int grep_friendly = 0; 70 71 static void 72 zpf(char *fmt, ...) 73 { 74 va_list ap; 75 76 va_start(ap, fmt); 77 vprintf(fmt, ap); 78 va_end(ap); 79 if (grep_friendly) { 80 printf(" "); 81 } else { 82 printf("\n"); 83 } 84 } 85 86 static void 87 znl(void) 88 { 89 if (grep_friendly) 90 printf("\n"); 91 } 92 93 static void 94 sig2hex_direct(const uint8_t *sig, char *hex) 95 { 96 int i; 97 98 for (i = 0; i < SHA1_DIGEST_LENGTH; ++i) { 99 sprintf(hex + 2 * i, "%02x", sig[i]); 100 } 101 hex[SHA1_DIGEST_LENGTH * 2] = '\0'; 102 } 103 104 static int 105 zev_statistics(int fd) 106 { 107 zev_statistics_t zs; 108 if (ioctl(fd, ZEV_IOC_GET_GLOBAL_STATISTICS, &zs)) { 109 perror("getting statistics data failed"); 110 return (EXIT_FAILURE); 111 } 112 printf("ZEV module state:\n"); 113 114 printf(" queue length in bytes : %lu\n", zs.zev_queue_len); 115 printf(" queue length limit : %lu\n", zs.zev_max_queue_len); 116 printf(" bytes read from device : %lu\n", zs.zev_bytes_read); 117 printf(" module internal errors : %lu\n\n", zs.zev_cnt_errors); 118 119 printf(" discarded events : %lu\n", 120 zs.zev_cnt_discarded_events); 121 printf(" discarded bytes : %lu\n\n", zs.zev_bytes_discarded); 122 123 printf("ZFS event statistics:\n"); 124 125 printf(" total ZFS events : %lu\n", zs.zev_cnt_total_events); 126 printf(" ZFS mount : %lu\n", zs.zev_cnt_zfs_mount); 127 printf(" ZFS umount : %lu\n", zs.zev_cnt_zfs_umount); 128 printf(" ZVOL write : %lu\n", zs.zev_cnt_zvol_write); 129 printf(" ZVOL truncate : %lu\n", zs.zev_cnt_zvol_truncate); 130 printf(" ZNODE close after update: %lu\n", 131 zs.zev_cnt_znode_close_after_update); 132 printf(" ZNODE create : %lu\n", zs.zev_cnt_znode_create); 133 printf(" ZNODE remove : %lu\n", zs.zev_cnt_znode_remove); 134 printf(" ZNODE link : %lu\n", zs.zev_cnt_znode_link); 135 printf(" ZNODE symlink : %lu\n", zs.zev_cnt_znode_symlink); 136 printf(" ZNODE rename : %lu\n", zs.zev_cnt_znode_rename); 137 printf(" ZNODE write : %lu\n", zs.zev_cnt_znode_write); 138 printf(" ZNODE truncate : %lu\n", 139 zs.zev_cnt_znode_truncate); 140 printf(" ZNODE setattr : %lu\n", zs.zev_cnt_znode_setattr); 141 printf(" ZNODE acl : %lu\n", zs.zev_cnt_znode_acl); 142 return EXIT_SUCCESS; 143 } 144 145 static void 146 zev_print_inode_info(char *name, zev_inode_info_t *info) 147 { 148 zpf(" %s.inode: %llu", name, info->ino); 149 zpf(" %s.gen: %llu", name, info->gen); 150 zpf(" %s.mtime: %llu", name, info->mtime); 151 zpf(" %s.ctime: %llu", name, info->ctime); 152 zpf(" %s.size: %llu", name, info->size); 153 zpf(" %s.mode: %llo", name, info->mode); 154 zpf(" %s.links: %llu", name, info->links); 155 zpf(" %s.type: %lu", name, info->type); 156 zpf(" %s.flags: %lu", name, info->flags); 157 } 158 159 static void 160 zev_print_mark_payload(zev_mark_t *rec) 161 { 162 int i; 163 int j; 164 uint8_t *p; 165 char c; 166 167 zpf(" payload:"); 168 p = (uint8_t *)ZEV_PAYLOAD(rec); 169 for (i=0; i<rec->payload_len; i+=16) { 170 printf(" "); 171 for (j=i; j<rec->payload_len && j<i+16; j++) { 172 printf("%02x ", p[j]); 173 if (j == i + 7) 174 printf(" "); 175 } 176 if (grep_friendly) 177 continue; 178 for (; j<i+16; j++) { 179 printf(" "); 180 if (j == i + 7) 181 printf(" "); 182 } 183 printf(" "); 184 for (j=i; j<rec->payload_len && j<i+16; j++) { 185 c = '.'; 186 if (p[j] >= ' ' && p[j] <= '~') 187 c = p[j]; 188 printf("%c", c); 189 if (j == i + 7) 190 printf(" "); 191 } 192 printf("\n"); 193 } 194 } 195 196 static void 197 zev_print_error(char *buf) 198 { 199 zev_error_t *rec = (zev_error_t *)buf; 200 time_t op_time = rec->op_time; 201 char *ct = ctime(&op_time); ct[24] = '\0'; 202 203 if (verbose) { 204 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 205 zpf(" guid: %llu", rec->guid); 206 zpf(" failed.op: %s", 207 zev_op_name[rec->failed_op - ZEV_OP_MIN]); 208 zpf(" message: %s", ZEV_ERRSTR(rec)); 209 znl(); 210 } else { 211 printf("%s %s: failed_op=%s msg=%s\n", 212 ct, zev_op_name[rec->op - ZEV_OP_MIN], 213 zev_op_name[rec->failed_op - ZEV_OP_MIN], 214 ZEV_ERRSTR(rec)); 215 } 216 } 217 218 static void 219 zev_print_mark(char *buf) 220 { 221 zev_mark_t *rec = (zev_mark_t *)buf; 222 time_t op_time = rec->op_time; 223 char *ct = ctime(&op_time); ct[24] = '\0'; 224 225 if (verbose) { 226 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 227 zpf(" guid: %llu", rec->guid); 228 zpf(" mark.id: %llu", rec->mark_id); 229 zpf(" payload.len: %llu", rec->payload_len); 230 if (rec->payload_len) 231 zev_print_mark_payload(rec); 232 znl(); 233 } else { 234 printf("%s %s: guid=%llu mark_id=%lld payload_len=%ld " 235 "payload=\"%.*s\"\n", 236 ct, zev_op_name[rec->op - ZEV_OP_MIN], rec->guid, 237 rec->mark_id, rec->payload_len, 238 rec->payload_len, (char *)(rec + 1)); 239 } 240 } 241 242 static void 243 zev_print_zfs_mount(char *buf) 244 { 245 zev_zfs_mount_t *rec = (zev_zfs_mount_t *)buf; 246 time_t op_time = rec->op_time; 247 char *ct = ctime(&op_time); ct[24] = '\0'; 248 249 if (verbose) { 250 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 251 zpf(" guid: %llu", rec->guid); 252 zpf(" dataset: %s", ZEV_DATASET(rec)); 253 zpf(" mountpoint: %s", ZEV_MOUNTPOINT(rec)); 254 zpf(" remount: %s", rec->remount ? "true" : "false"); 255 zev_print_inode_info("root", &rec->root); 256 znl(); 257 } else { 258 printf("%s %s: guid=%llu remount=%s dataset='%s' " 259 "mountpoint='%s'\n", 260 ct, zev_op_name[rec->op - ZEV_OP_MIN], 261 rec->guid, 262 rec->remount ? "true" : "false", 263 ZEV_DATASET(rec), 264 ZEV_MOUNTPOINT(rec)); 265 } 266 } 267 268 static void 269 zev_print_zfs_umount(char *buf) 270 { 271 zev_zfs_umount_t *rec = (zev_zfs_umount_t *)buf; 272 time_t op_time = rec->op_time; 273 char *ct = ctime(&op_time); ct[24] = '\0'; 274 275 if (verbose) { 276 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 277 zpf(" guid: %llu", rec->guid); 278 zev_print_inode_info("covered", &rec->covered); 279 znl(); 280 } else { 281 printf("%s %s: guid=%llu\n", 282 ct, zev_op_name[rec->op - ZEV_OP_MIN], 283 rec->guid); 284 } 285 } 286 287 static void 288 zev_print_zvol_truncate(char *buf) 289 { 290 zev_zvol_truncate_t *rec = (zev_zvol_truncate_t *)buf; 291 time_t op_time = rec->op_time; 292 char *ct = ctime(&op_time); ct[24] = '\0'; 293 294 if (verbose) { 295 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 296 zpf(" guid: %llu", rec->guid); 297 zpf(" txg: %llu", rec->txg); 298 zpf(" offset: %llu", rec->offset); 299 zpf(" length: %llu", rec->length); 300 znl(); 301 } else { 302 printf("%s %s: guid=%llu offset=%llu length=%llu\n", 303 ct, zev_op_name[rec->op - ZEV_OP_MIN], 304 rec->guid, 305 rec->offset, 306 rec->length); 307 } 308 } 309 310 static void 311 zev_print_zvol_write(char *buf) 312 { 313 zev_print_zvol_truncate(buf); 314 } 315 316 static void 317 zev_print_znode_close_after_update(char *buf) 318 { 319 zev_znode_close_after_update_t *rec = 320 (zev_znode_close_after_update_t *)buf; 321 time_t op_time = rec->op_time; 322 char *ct = ctime(&op_time); ct[24] = '\0'; 323 324 if (verbose) { 325 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 326 zpf(" guid: %llu", rec->guid); 327 zev_print_inode_info("file", &rec->file); 328 znl(); 329 } else { 330 printf("%s %s: guid=%llu file=%llu.%llu\n", 331 ct, zev_op_name[rec->op - ZEV_OP_MIN], 332 rec->guid, 333 rec->file.ino, rec->file.gen); 334 } 335 } 336 337 static void 338 zev_print_znode_create(char *buf) 339 { 340 zev_znode_create_t *rec = (zev_znode_create_t *)buf; 341 time_t op_time = rec->op_time; 342 char *ct = ctime(&op_time); ct[24] = '\0'; 343 zev_sig_t *sig; 344 char sigval[(SHA1_DIGEST_LENGTH * 2) + 1]; 345 346 if (verbose) { 347 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 348 zpf(" guid: %llu", rec->guid); 349 zpf(" txg: %llu", rec->txg); 350 zpf(" name: '%s'", ZEV_NAME(rec)); 351 sig = &rec->signature; 352 sig2hex_direct(sig->value, sigval); 353 zpf(" sig: level %d, offset %llu, value %s", 354 sig->level, sig->block_offset, sigval); 355 zev_print_inode_info("file", &rec->file); 356 zev_print_inode_info("parent", &rec->parent); 357 znl(); 358 } else { 359 printf("%s %s: guid=%llu parent=%llu.%llu file=%llu.%llu " 360 "file.mtime=%llu, parent.mtime=%llu, name='%s'\n", 361 ct, zev_op_name[rec->op - ZEV_OP_MIN], 362 rec->guid, 363 rec->parent.ino, rec->parent.gen, 364 rec->file.ino, rec->file.gen, 365 rec->file.mtime, rec->parent.mtime, 366 ZEV_NAME(rec)); 367 } 368 } 369 370 static void 371 zev_print_znode_mkdir(char *buf) 372 { 373 zev_print_znode_create(buf); 374 } 375 376 static void 377 zev_print_znode_make_xattr_dir(char *buf) 378 { 379 zev_print_znode_create(buf); 380 } 381 382 static void 383 zev_print_znode_remove(char *buf) 384 { 385 zev_znode_remove_t *rec = (zev_znode_remove_t *)buf; 386 time_t op_time = rec->op_time; 387 char *ct = ctime(&op_time); ct[24] = '\0'; 388 389 if (verbose) { 390 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 391 zpf(" guid: %llu", rec->guid); 392 zpf(" txg: %llu", rec->txg); 393 zpf(" file.name: '%s'", ZEV_NAME(rec)); 394 zev_print_inode_info("file", &rec->file); 395 zev_print_inode_info("parent", &rec->parent); 396 znl(); 397 } else { 398 printf("%s %s: guid=%llu parent=%llu.%llu " 399 "file.mtime=%llu name='%s'\n", 400 ct, zev_op_name[rec->op - ZEV_OP_MIN], 401 rec->guid, 402 rec->parent.ino, rec->parent.gen, 403 rec->file.mtime, 404 ZEV_NAME(rec)); 405 } 406 } 407 408 static void 409 zev_print_znode_rmdir(char *buf) 410 { 411 zev_print_znode_remove(buf); 412 } 413 414 static void 415 zev_print_znode_link(char *buf) 416 { 417 zev_znode_link_t *rec = (zev_znode_link_t *)buf; 418 time_t op_time = rec->op_time; 419 char *ct = ctime(&op_time); ct[24] = '\0'; 420 421 if (verbose) { 422 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 423 zpf(" guid: %llu", rec->guid); 424 zpf(" txg: %llu", rec->txg); 425 zpf(" link.name: '%s'", ZEV_NAME(rec)); 426 zev_print_inode_info("file", &rec->file); 427 zev_print_inode_info("parent", &rec->parent); 428 znl(); 429 } else { 430 printf("%s %s: parent=%llu.%llu file=%llu.%llu " 431 "file.ctime=%llu parent.ctime=%llu name='%s'\n", 432 ct, zev_op_name[rec->op - ZEV_OP_MIN], 433 rec->parent.ino, rec->parent.gen, 434 rec->file.ino, rec->file.gen, 435 rec->file.ctime, rec->parent.ctime, 436 ZEV_NAME(rec)); 437 } 438 } 439 440 static void 441 zev_print_znode_symlink(char *buf) 442 { 443 zev_znode_symlink_t *rec = (zev_znode_symlink_t *)buf; 444 time_t op_time = rec->op_time; 445 char *ct = ctime(&op_time); ct[24] = '\0'; 446 zev_sig_t *sig; 447 char sigval[(SHA1_DIGEST_LENGTH * 2) + 1]; 448 449 if (verbose) { 450 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 451 zpf(" guid: %llu", rec->guid); 452 zpf(" txg: %llu", rec->txg); 453 zpf(" symlink.name: '%s'", ZEV_NAME(rec)); 454 zpf(" symlink.link: '%s'", ZEV_LINK(rec)); 455 sig = &rec->signature; 456 sig2hex_direct(sig->value, sigval); 457 zpf(" sig: level %d, offset %llu, value %s", 458 sig->level, sig->block_offset, sigval); 459 zev_print_inode_info("file", &rec->file); 460 zev_print_inode_info("parent", &rec->parent); 461 znl(); 462 } else { 463 printf("%s %s: parent=%llu.%llu file=%llu.%llu " 464 "name='%s' link='%s'\n", 465 ct, zev_op_name[rec->op - ZEV_OP_MIN], 466 rec->parent.ino, rec->parent.gen, 467 rec->file.ino, rec->file.gen, 468 ZEV_NAME(rec), 469 ZEV_LINK(rec)); 470 } 471 } 472 473 static void 474 zev_print_znode_rename(char *buf) 475 { 476 zev_znode_rename_t *rec = (zev_znode_rename_t *)buf; 477 time_t op_time = rec->op_time; 478 char *ct = ctime(&op_time); ct[24] = '\0'; 479 480 if (verbose) { 481 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 482 zpf(" guid: %llu", rec->guid); 483 zpf(" txg: %llu", rec->txg); 484 zpf(" file.srcname: '%s'", ZEV_SRCNAME(rec)); 485 zpf(" file.dstname: '%s'", ZEV_DSTNAME(rec)); 486 zev_print_inode_info("file", &rec->file); 487 if (rec->clobbered_file.ino) 488 zev_print_inode_info("clobbered_file", 489 &rec->clobbered_file); 490 zev_print_inode_info("srcdir", &rec->srcdir); 491 zev_print_inode_info("dstdir", &rec->dstdir); 492 znl(); 493 } else { 494 printf("%s %s: srcdir=%llu.%llu dstdir=%llu.%llu " 495 "file=%llu.%llu file.mtime=%llu, file.ctime=%llu, " 496 "srcdir.mtime=%llu, srcdir.ctime=%llu, " 497 "dstdir.mtime=%llu, dstdir.ctime=%llu, " 498 "srcname='%s' dstname='%s'\n", 499 ct, zev_op_name[rec->op - ZEV_OP_MIN], 500 rec->srcdir.ino, rec->srcdir.gen, 501 rec->dstdir.ino, rec->dstdir.gen, 502 rec->file.ino, rec->file.gen, 503 rec->file.mtime, rec->file.ctime, 504 rec->srcdir.mtime, rec->srcdir.ctime, 505 rec->dstdir.mtime, rec->dstdir.ctime, 506 ZEV_SRCNAME(rec), 507 ZEV_DSTNAME(rec)); 508 } 509 } 510 511 static void 512 zev_print_znode_write(char *buf) 513 { 514 zev_znode_write_t *rec = (zev_znode_write_t *)buf; 515 time_t op_time = rec->op_time; 516 char *ct = ctime(&op_time); ct[24] = '\0'; 517 zev_sig_t *sig; 518 char sigval[(SHA1_DIGEST_LENGTH * 2) + 1]; 519 int i; 520 521 if (verbose) { 522 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 523 zpf(" guid: %llu", rec->guid); 524 zpf(" txg: %llu", rec->txg); 525 zpf(" offset: %llu", rec->offset); 526 zpf(" length: %llu", rec->length); 527 zev_print_inode_info("file", &rec->file); 528 znl(); 529 for (i=0; i<rec->signature_cnt; i++) { 530 sig = (zev_sig_t *)ZEV_SIGNATURES(rec); 531 sig += i; 532 sig2hex_direct(sig->value, sigval); 533 zpf(" sig: level %d, offset %llu, value %s", 534 sig->level, sig->block_offset, sigval); 535 } 536 } else { 537 printf("%s %s: file=%llu.%llu offset=%llu length=%llu\n", 538 ct, zev_op_name[rec->op - ZEV_OP_MIN], 539 rec->file.ino, rec->file.gen, 540 rec->offset, rec->length); 541 } 542 } 543 544 static void 545 zev_print_znode_truncate(char *buf) 546 { 547 zev_print_znode_write(buf); 548 } 549 550 static void 551 zev_print_znode_setattr(char *buf) 552 { 553 zev_znode_setattr_t *rec = (zev_znode_setattr_t *)buf; 554 time_t op_time = rec->op_time; 555 char *ct = ctime(&op_time); ct[24] = '\0'; 556 557 if (verbose) { 558 zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]); 559 zpf(" guid: %llu", rec->guid); 560 zpf(" txg: %llu", rec->txg); 561 zev_print_inode_info("file", &rec->file); 562 znl(); 563 } else { 564 printf("%s %s: file=%llu.%llu mtime=%llu\n", 565 ct, zev_op_name[rec->op - ZEV_OP_MIN], 566 rec->file.ino, rec->file.gen, rec->file.mtime); 567 } 568 } 569 570 static void 571 zev_print_znode_acl(char *buf) 572 { 573 zev_print_znode_setattr(buf); 574 } 575 576 static void 577 zev_print_event(char *buf, int len) 578 { 579 int record_len; 580 int op; 581 582 record_len = *(uint32_t *)buf; 583 if (record_len != len) { 584 fprintf(stderr, "record length mismatch: got %d, expected %d\n", 585 record_len, len); 586 exit(1); 587 } 588 op = *((uint32_t *)buf + 1); 589 if (op < ZEV_OP_MIN || op > ZEV_OP_MAX) { 590 fprintf(stderr, "unknown op code: %d\n", op); 591 exit(1); 592 } 593 switch (op) { 594 case ZEV_OP_ERROR: 595 zev_print_error(buf); 596 break; 597 case ZEV_OP_MARK: 598 zev_print_mark(buf); 599 break; 600 case ZEV_OP_ZFS_MOUNT: 601 zev_print_zfs_mount(buf); 602 break; 603 case ZEV_OP_ZFS_UMOUNT: 604 zev_print_zfs_umount(buf); 605 break; 606 case ZEV_OP_ZVOL_TRUNCATE: 607 zev_print_zvol_truncate(buf); 608 break; 609 case ZEV_OP_ZVOL_WRITE: 610 zev_print_zvol_write(buf); 611 break; 612 case ZEV_OP_ZNODE_CLOSE_AFTER_UPDATE: 613 zev_print_znode_close_after_update(buf); 614 break; 615 case ZEV_OP_ZNODE_CREATE: 616 zev_print_znode_create(buf); 617 break; 618 case ZEV_OP_ZNODE_MKDIR: 619 zev_print_znode_mkdir(buf); 620 break; 621 case ZEV_OP_ZNODE_MAKE_XATTR_DIR: 622 zev_print_znode_make_xattr_dir(buf); 623 break; 624 case ZEV_OP_ZNODE_REMOVE: 625 zev_print_znode_remove(buf); 626 break; 627 case ZEV_OP_ZNODE_RMDIR: 628 zev_print_znode_rmdir(buf); 629 break; 630 case ZEV_OP_ZNODE_LINK: 631 zev_print_znode_link(buf); 632 break; 633 case ZEV_OP_ZNODE_SYMLINK: 634 zev_print_znode_symlink(buf); 635 break; 636 case ZEV_OP_ZNODE_RENAME: 637 zev_print_znode_rename(buf); 638 break; 639 case ZEV_OP_ZNODE_WRITE: 640 zev_print_znode_write(buf); 641 break; 642 case ZEV_OP_ZNODE_TRUNCATE: 643 zev_print_znode_truncate(buf); 644 break; 645 case ZEV_OP_ZNODE_SETATTR: 646 zev_print_znode_setattr(buf); 647 break; 648 case ZEV_OP_ZNODE_ACL: 649 zev_print_znode_acl(buf); 650 break; 651 default: 652 fprintf(stderr, "unhandled op code: %d\n", op); 653 exit(1); 654 } 655 } 656 657 static int 658 zev_poll_events(int fd, int create_tmp_queue) 659 { 660 struct pollfd pfd[1]; 661 int ret; 662 char buf[4096]; 663 zev_event_t *ev; 664 int off = 0; 665 int q_fd; 666 667 if (create_tmp_queue) { 668 snprintf(buf, sizeof(buf), 669 "/devices/pseudo/zev@0:%s", ZEV_TMPQUEUE_DEVICE_NAME); 670 q_fd = open(buf, O_RDONLY); 671 if (q_fd < 0) { 672 perror("opening queue device failed"); 673 return (EXIT_FAILURE); 674 } 675 } else { 676 q_fd = fd; 677 } 678 679 while (1) { 680 pfd[0].fd = q_fd; 681 pfd[0].events = POLLIN; 682 ret = poll(pfd, 1, 1000); 683 if (ret < 0) { 684 perror("poll failed"); 685 close(q_fd); 686 return(EXIT_FAILURE); 687 } 688 if (!(pfd[0].revents & POLLIN)) 689 continue; 690 /* data available */ 691 ret = read(q_fd, buf, sizeof(buf)); 692 if (ret < 0) { 693 perror("read failed"); 694 close(q_fd); 695 return(EXIT_FAILURE); 696 } 697 if (ret == 0) 698 continue; 699 while (ret > off) { 700 ev = (zev_event_t *)(buf + off); 701 zev_print_event(buf + off, ev->header.record_len); 702 off += ev->header.record_len; 703 } 704 off = 0; 705 } 706 if (create_tmp_queue) 707 close(q_fd); 708 return EXIT_SUCCESS; 709 } 710 711 static int 712 zev_dump_spool(int fd) 713 { 714 int len; 715 char buf[4096]; 716 int off = 0; 717 718 while (1) { 719 len = read(fd, buf + off, sizeof(buf) - off); 720 if (len == -1) { 721 fprintf(stderr, "reading from spool failed: %s\n", 722 strerror(errno)); 723 return EXIT_FAILURE; 724 } 725 if (len == 0) 726 break; 727 728 len += off; 729 off = 0; 730 while (len > off + sizeof(uint32_t)) { 731 uint32_t evlen; 732 char *mp; 733 zev_event_t *ev; 734 735 ev = (zev_event_t *)(buf + off); 736 evlen = ev->header.record_len; 737 if (len < off + evlen + 1) 738 break; 739 mp = buf + off + evlen; 740 if (!memchr(mp, 0, len - off - evlen)) 741 break; 742 zev_print_event(buf + off, ev->header.record_len); 743 off += ev->header.record_len + strlen(mp) + 1; 744 } 745 746 memmove(buf, buf + off, len - off); 747 off = len - off; 748 } 749 750 return EXIT_SUCCESS; 751 } 752 753 static void 754 usage(char *progname) 755 { 756 fprintf(stderr, "usage: %s [-d <dev>] [options]\n", progname); 757 fprintf(stderr, "\n"); 758 fprintf(stderr, " Status information:\n"); 759 fprintf(stderr, " -s show zev statistics\n"); 760 fprintf(stderr, " -p poll for ZFS events\n"); 761 fprintf(stderr, " -f <name> dump events from spool\n"); 762 fprintf(stderr, " -D print zev module debug " 763 "information\n"); 764 fprintf(stderr, " -T <interval> <cnt> zevstat mode\n"); 765 fprintf(stderr, " -R <base filename> zevreport mode\n"); 766 fprintf(stderr, "\n"); 767 fprintf(stderr, " Tune zev module settings:\n"); 768 fprintf(stderr, " -Q <bytes> set maximum event queue " 769 "length\n"); 770 fprintf(stderr, " -m <pool> mute pool, no events for " 771 "this pool\n"); 772 fprintf(stderr, " -M <pool> unmute pool\n"); 773 fprintf(stderr, "\n"); 774 fprintf(stderr, " Queue management:\n"); 775 fprintf(stderr, " -l list queues\n"); 776 fprintf(stderr, " -a <name> add non-blocking queue\n"); 777 fprintf(stderr, " -A <name> add blocking queue\n"); 778 fprintf(stderr, " -r <name> remove queue\n"); 779 fprintf(stderr, " -b <name> make queue non-blocking " 780 "(default)\n"); 781 fprintf(stderr, " -B <name> make queue block when full\n"); 782 fprintf(stderr, " -P <name> display queue properties\n"); 783 fprintf(stderr, " -L <name> <bytes> set maximum event queue " 784 "length\n"); 785 fprintf(stderr, " -t <name> <bytes> set queue length poll " 786 "throttle\n"); 787 fprintf(stderr, "\n"); 788 fprintf(stderr, " Other options:\n"); 789 fprintf(stderr, " -d <dev> non-default device file. " 790 "('%s')\n", ZEV_DEVICE); 791 fprintf(stderr, " -q <name> use device file for this " 792 "queue name\n"); 793 fprintf(stderr, " -k <guid>:<payload> queue mark event\n"); 794 fprintf(stderr, " -c <filename> list file's content " 795 "checksums\n"); 796 fprintf(stderr, " -v verbose: additional output " 797 "for some operations\n"); 798 fprintf(stderr, " -g grep-friendly event output, " 799 "one event per line\n"); 800 fprintf(stderr, " -V query zev module version\n"); 801 exit (EXIT_FAILURE); 802 } 803 804 static void 805 zevstat_usage(char *progname) 806 { 807 fprintf(stderr, "usage: %s [-v] <interval> [count]\n", progname); 808 fprintf(stderr, " -v verbose, show counters for all event types\n"); 809 exit (EXIT_FAILURE); 810 } 811 812 static void 813 zevreport_usage(char *progname) 814 { 815 fprintf(stderr, "usage: %s <output base filename>\n", progname); 816 exit (EXIT_FAILURE); 817 } 818 819 static int 820 zev_add_queue(int fd, char *arg, int blocking) 821 { 822 zev_ioctl_add_queue_t aq; 823 int namelen; 824 825 namelen = strlen(arg); 826 if (namelen > ZEV_MAX_QUEUE_NAME_LEN) { 827 fprintf(stderr, "queue name too long: %s\n", arg); 828 return (EXIT_FAILURE); 829 } 830 831 aq.zev_namelen = namelen; 832 strcpy(aq.zev_name, arg); 833 aq.zev_flags = ZEV_FL_PERSISTENT | ZEV_FL_INITIALLY_EMPTY; 834 if (blocking) { 835 aq.zev_flags |= ZEV_FL_BLOCK_WHILE_QUEUE_FULL; 836 aq.zev_max_queue_len = ZEV_MAX_QUEUE_LEN; 837 } else { 838 aq.zev_max_queue_len = (1024 * 1024); 839 } 840 841 if (ioctl(fd, ZEV_IOC_ADD_QUEUE, &aq)) { 842 perror("adding queue failed"); 843 return (EXIT_FAILURE); 844 } 845 return (0); 846 } 847 848 static int 849 zev_remove_queue(int fd, char *arg) 850 { 851 zev_ioctl_remove_queue_t aq; 852 int namelen; 853 854 namelen = strlen(arg); 855 if (namelen > ZEV_MAX_QUEUE_NAME_LEN) { 856 fprintf(stderr, "queue name too long: %s\n", arg); 857 return (EXIT_FAILURE); 858 } 859 860 aq.zev_queue_name.zev_namelen = namelen; 861 strcpy(aq.zev_queue_name.zev_name, arg); 862 863 if (ioctl(fd, ZEV_IOC_REMOVE_QUEUE, &aq)) { 864 perror("removing queue failed"); 865 return (EXIT_FAILURE); 866 } 867 return (0); 868 } 869 870 static int 871 zev_set_global_max_queue_len(int fd, char *arg) 872 { 873 uint64_t maxqueuelen; 874 875 if (!arg) { 876 fprintf(stderr, "missing queue length parameter\n"); 877 return (EXIT_FAILURE); 878 } 879 880 errno = 0; 881 maxqueuelen = strtol(arg, (char **)NULL, 10); 882 if (errno) { 883 fprintf(stderr, "invalid queue length parameter: %s\n", arg); 884 return (EXIT_FAILURE); 885 } 886 if (ioctl(fd, ZEV_IOC_SET_MAX_QUEUE_LEN, &maxqueuelen)) { 887 perror("setting max queue length failed"); 888 return (EXIT_FAILURE); 889 } 890 return (0); 891 } 892 893 static int 894 zev_mute_unmute_impl(int fd, char *poolname, int mute) 895 { 896 zev_ioctl_poolarg_t pa; 897 int len; 898 int op = mute ? ZEV_IOC_MUTE_POOL : ZEV_IOC_UNMUTE_POOL; 899 len = strlen(poolname); 900 if (len <= 0 || len >= sizeof(pa.zev_poolname)) { 901 fprintf(stderr, "invalid poolname: %s\n", poolname); 902 return (EXIT_FAILURE); 903 } 904 strcpy(pa.zev_poolname, poolname); 905 pa.zev_poolname_len = len; 906 if (ioctl(fd, op, &pa)) { 907 perror("muting pool data failed"); 908 return (EXIT_FAILURE); 909 } 910 return (0); 911 } 912 913 int 914 zev_mute_pool(int fd, char *poolname) 915 { 916 return zev_mute_unmute_impl(fd, poolname, 1); 917 } 918 919 int 920 zev_unmute_pool(int fd, char *poolname) 921 { 922 return zev_mute_unmute_impl(fd, poolname, 0); 923 } 924 925 static int 926 zev_debug_info(int fd) 927 { 928 zev_ioctl_debug_info_t di; 929 930 if (ioctl(fd, ZEV_IOC_GET_DEBUG_INFO, &di)) { 931 perror("getting zev debug info failed"); 932 return (EXIT_FAILURE); 933 } 934 935 printf("memory allocated: %llu bytes\n", di.zev_memory_allocated); 936 printf("checksum cache size: %llu\n", di.zev_chksum_cache_size); 937 printf("checksum cache hits: %llu\n", di.zev_chksum_cache_hits); 938 printf("checksum cache misses: %llu\n", di.zev_chksum_cache_misses); 939 return 0; 940 } 941 942 static int 943 zev_mark(int fd, char *arg) 944 { 945 zev_ioctl_mark_t *mark; 946 uint64_t guid; 947 int len; 948 char *p; 949 950 p = strchr(arg, ':'); 951 if (!p) { 952 fprintf(stderr, "expected value is <guid>:<payload>, " 953 "e.g. '123:hello'\n"); 954 exit (EXIT_FAILURE); 955 } 956 *p = '\n'; 957 p++; 958 959 errno = 0; 960 guid = strtoll(arg, (char **)NULL, 10); 961 if (errno) { 962 fprintf(stderr, "guid must be a number.\n"); 963 exit (EXIT_FAILURE); 964 } 965 966 len = strlen(p); 967 968 mark = malloc(sizeof(*mark) + len + 1); 969 if (!mark) { 970 fprintf(stderr, "can't allocate mark structure: %s\n", 971 strerror(errno)); 972 exit (EXIT_FAILURE); 973 } 974 mark->zev_guid = guid; 975 mark->zev_mark_id = 0; 976 mark->zev_payload_len = len; 977 strcpy(ZEV_PAYLOAD(mark), p); 978 979 if (ioctl(fd, ZEV_IOC_MARK, mark)) { 980 perror("queueing mark failed"); 981 return (EXIT_FAILURE); 982 } 983 984 printf("mark id: %lu\n", mark->zev_mark_id); 985 return (0); 986 } 987 988 static int 989 zev_queue_blocking(int fd, char *arg, int block) 990 { 991 zev_ioctl_get_queue_properties_t gqp; 992 993 gqp.zev_queue_name.zev_namelen = strlen(arg); 994 if (gqp.zev_queue_name.zev_namelen > ZEV_MAX_QUEUE_NAME_LEN) { 995 fprintf(stderr, "queue name too long.\n"); 996 return EXIT_FAILURE; 997 } 998 strcpy(gqp.zev_queue_name.zev_name, arg); 999 1000 if (ioctl(fd, ZEV_IOC_GET_QUEUE_PROPERTIES, &gqp)) { 1001 perror("getting queue properties failed"); 1002 return (EXIT_FAILURE); 1003 } 1004 if (block) { 1005 gqp.zev_flags |= ZEV_FL_BLOCK_WHILE_QUEUE_FULL; 1006 } else { 1007 gqp.zev_flags &= ~ZEV_FL_BLOCK_WHILE_QUEUE_FULL; 1008 } 1009 if (ioctl(fd, ZEV_IOC_SET_QUEUE_PROPERTIES, &gqp)) { 1010 perror("setting queue properties failed"); 1011 return (EXIT_FAILURE); 1012 } 1013 return (0); 1014 } 1015 1016 static int 1017 zev_set_max_queue_len(int fd, char *arg, char *len) 1018 { 1019 zev_ioctl_get_queue_properties_t gqp; 1020 1021 if (!len) { 1022 fprintf(stderr, "queue size parameter missing.\n"); 1023 return EXIT_FAILURE; 1024 } 1025 1026 gqp.zev_queue_name.zev_namelen = strlen(arg); 1027 if (gqp.zev_queue_name.zev_namelen > ZEV_MAX_QUEUE_NAME_LEN) { 1028 fprintf(stderr, "queue name too long.\n"); 1029 return EXIT_FAILURE; 1030 } 1031 strcpy(gqp.zev_queue_name.zev_name, arg); 1032 1033 if (ioctl(fd, ZEV_IOC_GET_QUEUE_PROPERTIES, &gqp)) { 1034 perror("getting queue properties failed"); 1035 return (EXIT_FAILURE); 1036 } 1037 gqp.zev_max_queue_len = atol(len); 1038 if (gqp.zev_max_queue_len == 0 && strcmp("0", len)) { 1039 fprintf(stderr, "queue size parameter garbled.\n"); 1040 return (EXIT_FAILURE); 1041 } 1042 if (gqp.zev_max_queue_len > ZEV_MAX_QUEUE_LEN) { 1043 fprintf(stderr, "queue size parameter out of bounds.\n"); 1044 return (EXIT_FAILURE); 1045 } 1046 1047 if (ioctl(fd, ZEV_IOC_SET_QUEUE_PROPERTIES, &gqp)) { 1048 perror("setting queue properties failed"); 1049 return (EXIT_FAILURE); 1050 } 1051 return (0); 1052 } 1053 1054 static int 1055 zev_set_poll_wakeup_queue_len(int fd, char *arg, char *len) 1056 { 1057 zev_ioctl_get_queue_properties_t gqp; 1058 1059 if (!len) { 1060 fprintf(stderr, "poll throttle parameter missing.\n"); 1061 return EXIT_FAILURE; 1062 } 1063 1064 gqp.zev_queue_name.zev_namelen = strlen(arg); 1065 if (gqp.zev_queue_name.zev_namelen > ZEV_MAX_QUEUE_NAME_LEN) { 1066 fprintf(stderr, "queue name too long.\n"); 1067 return EXIT_FAILURE; 1068 } 1069 strcpy(gqp.zev_queue_name.zev_name, arg); 1070 1071 if (ioctl(fd, ZEV_IOC_GET_QUEUE_PROPERTIES, &gqp)) { 1072 perror("getting queue properties failed"); 1073 return (EXIT_FAILURE); 1074 } 1075 gqp.zev_poll_wakeup_threshold = atol(len); 1076 if (gqp.zev_poll_wakeup_threshold == 0 && strcmp("0", len)) { 1077 fprintf(stderr, "poll throttle parameter garbled.\n"); 1078 return (EXIT_FAILURE); 1079 } 1080 if (gqp.zev_poll_wakeup_threshold > ZEV_MAX_POLL_WAKEUP_QUEUE_LEN) { 1081 fprintf(stderr, "poll throttle parameter out of bounds.\n"); 1082 return (EXIT_FAILURE); 1083 } 1084 1085 if (ioctl(fd, ZEV_IOC_SET_QUEUE_PROPERTIES, &gqp)) { 1086 perror("setting queue properties failed"); 1087 return (EXIT_FAILURE); 1088 } 1089 return (0); 1090 } 1091 1092 static int 1093 zev_queue_properties(int fd, char *arg) 1094 { 1095 zev_ioctl_get_queue_properties_t gqp; 1096 1097 gqp.zev_queue_name.zev_namelen = strlen(arg); 1098 if (gqp.zev_queue_name.zev_namelen > ZEV_MAX_QUEUE_NAME_LEN) { 1099 fprintf(stderr, "queue name too long.\n"); 1100 return EXIT_FAILURE; 1101 } 1102 strcpy(gqp.zev_queue_name.zev_name, arg); 1103 1104 if (ioctl(fd, ZEV_IOC_GET_QUEUE_PROPERTIES, &gqp)) { 1105 perror("getting queue properties failed"); 1106 return (EXIT_FAILURE); 1107 } 1108 1109 printf("queue : %s\n", arg); 1110 printf("max size : %" PRIu64 "\n", gqp.zev_max_queue_len); 1111 printf("poll throttle: %" PRIu64 "\n", gqp.zev_poll_wakeup_threshold); 1112 printf("persistent : %s\n", 1113 gqp.zev_flags & ZEV_FL_PERSISTENT ? "yes" : "no"); 1114 printf("blocking : %s\n", 1115 gqp.zev_flags & ZEV_FL_BLOCK_WHILE_QUEUE_FULL ? "yes" : "no"); 1116 1117 return (0); 1118 } 1119 1120 static int 1121 zev_list_queues(int fd) 1122 { 1123 zev_ioctl_get_queue_properties_t gqp; 1124 zev_ioctl_get_queue_list_t gql; 1125 zev_ioctl_get_queue_statistics_t gs; 1126 uint64_t i; 1127 char name[ZEV_MAX_QUEUE_NAME_LEN+1]; 1128 zev_statistics_t zs; 1129 1130 if (ioctl(fd, ZEV_IOC_GET_GLOBAL_STATISTICS, &zs)) { 1131 perror("getting statistics data failed"); 1132 return (EXIT_FAILURE); 1133 } 1134 1135 if (ioctl(fd, ZEV_IOC_GET_QUEUE_LIST, &gql)) { 1136 perror("getting queue list failed"); 1137 return (EXIT_FAILURE); 1138 } 1139 1140 printf("Name Size " 1141 "Size%% Max Size Per Block\n"); 1142 1143 for (i=0; i<gql.zev_n_queues; i++) { 1144 strncpy(name, gql.zev_queue_name[i].zev_name, 1145 ZEV_MAX_QUEUE_NAME_LEN); 1146 name[gql.zev_queue_name[i].zev_namelen] = '\0'; 1147 1148 memcpy(gqp.zev_queue_name.zev_name, 1149 gql.zev_queue_name[i].zev_name, ZEV_MAX_QUEUE_NAME_LEN); 1150 gqp.zev_queue_name.zev_namelen = 1151 gql.zev_queue_name[i].zev_namelen; 1152 1153 if (ioctl(fd, ZEV_IOC_GET_QUEUE_PROPERTIES, &gqp)) { 1154 if (errno == ENOENT) 1155 continue; 1156 perror("getting queue properties failed"); 1157 return (EXIT_FAILURE); 1158 } 1159 1160 memcpy(gs.zev_queue_name.zev_name, 1161 gql.zev_queue_name[i].zev_name, ZEV_MAX_QUEUE_NAME_LEN); 1162 gs.zev_queue_name.zev_namelen = 1163 gql.zev_queue_name[i].zev_namelen; 1164 1165 if (ioctl(fd, ZEV_IOC_GET_QUEUE_STATISTICS, &gs)) { 1166 if (errno == ENOENT) 1167 continue; 1168 perror("getting statistics data failed"); 1169 return (EXIT_FAILURE); 1170 } 1171 1172 if (gqp.zev_max_queue_len == 0) { 1173 gqp.zev_max_queue_len = zs.zev_max_queue_len; 1174 } 1175 printf("%-40s %-10" PRIu64 " %5.1f %-10" PRIu64 1176 " %-3s %-3s\n", 1177 name, 1178 gs.zev_statistics.zev_queue_len * 100.0 / 1179 gqp.zev_max_queue_len, 1180 gs.zev_statistics.zev_queue_len, 1181 gqp.zev_max_queue_len, 1182 gqp.zev_flags & ZEV_FL_PERSISTENT ? "yes" : "no", 1183 gqp.zev_flags & ZEV_FL_BLOCK_WHILE_QUEUE_FULL ? 1184 "yes" : "no"); 1185 } 1186 1187 return (0); 1188 } 1189 1190 static int 1191 zev_checksum(int dev_fd, char *filename) 1192 { 1193 int fd; 1194 offset_t off; 1195 offset_t data; 1196 zev_sig_t *sig; 1197 char *buf; 1198 zev_ioctl_get_signatures_t *gs; 1199 int i; 1200 char sigval[(SHA1_DIGEST_LENGTH * 2) + 1]; 1201 int buf_size; 1202 1203 /* control struct, one lv1 signature and up to 256 lv0 signatures */ 1204 buf_size = (1 + 256) * sizeof(zev_sig_t); 1205 buf = malloc(sizeof(zev_ioctl_get_signatures_t) + buf_size); 1206 if (!buf) { 1207 perror("can't allocate checksum buffer"); 1208 return (EXIT_FAILURE); 1209 } 1210 1211 fd = open(filename, O_RDONLY); 1212 if (fd < 0) { 1213 perror("can't open file"); 1214 return (EXIT_FAILURE); 1215 } 1216 1217 gs = (zev_ioctl_get_signatures_t *)buf; 1218 gs->zev_fd = fd; 1219 gs->zev_bufsize = buf_size; 1220 1221 off = 0; 1222 data = 0; 1223 while (1) { 1224 errno = 0; 1225 data = llseek(fd, off, SEEK_DATA); 1226 if (data < 0) { 1227 if (errno == ENXIO) /* no more data */ 1228 break; 1229 perror("llseek failed"); 1230 goto err; 1231 } 1232 data = P2ALIGN(data, ZEV_L1_SIZE); 1233 off = data + ZEV_L1_SIZE; 1234 1235 gs->zev_offset = data; 1236 gs->zev_len = ZEV_L1_SIZE; 1237 1238 if (ioctl(dev_fd, ZEV_IOC_GET_FILE_SIGNATURES, gs)) { 1239 perror("ioctl to get signatures failed"); 1240 goto err; 1241 } 1242 1243 for (i=0; i<gs->zev_signature_cnt; i++) { 1244 sig = (zev_sig_t *)ZEV_SIGNATURES(gs); 1245 sig += i; 1246 sig2hex_direct(sig->value, sigval); 1247 printf("level %d, offset %llu, value %s\n", 1248 sig->level, sig->block_offset, sigval); 1249 } 1250 } 1251 1252 free(buf); 1253 close(fd); 1254 return 0; 1255 err: 1256 free(buf); 1257 close(fd); 1258 return (EXIT_FAILURE); 1259 } 1260 1261 typedef struct zevstat { 1262 uint64_t ns_start; 1263 uint64_t events[ZEV_OP_MIN + ZEV_OP_MAX]; 1264 uint64_t guids; 1265 uint64_t total_events; 1266 uint64_t total_guids; 1267 avl_tree_t guids_interval; 1268 avl_tree_t guids_runtime; 1269 } zevstat_t; 1270 1271 typedef struct zev_guidtrack_t { 1272 uint64_t guid; 1273 avl_node_t avl_interval; 1274 avl_node_t avl_runtime; 1275 } zev_guidtrack_t; 1276 1277 zevstat_t zevstat; 1278 1279 static void 1280 zev_eventstat(char *buf, int len) 1281 { 1282 zev_header_t *rec = (zev_header_t *)buf; 1283 zev_guidtrack_t *gt; 1284 zev_guidtrack_t *gt_int; 1285 zev_guidtrack_t to_find; 1286 avl_index_t where; 1287 1288 zevstat.total_events++; 1289 zevstat.events[rec->op]++; 1290 1291 to_find.guid = rec->guid; 1292 gt = avl_find(&zevstat.guids_runtime, &to_find, &where); 1293 if (!gt) { 1294 gt = malloc(sizeof(*gt)); 1295 if (!gt) { 1296 perror("can't get guid tracking record"); 1297 exit (EXIT_FAILURE); 1298 } 1299 gt->guid = rec->guid; 1300 avl_insert(&zevstat.guids_runtime, gt, where); 1301 } 1302 gt_int = avl_find(&zevstat.guids_interval, &to_find, &where); 1303 if (!gt_int) 1304 avl_insert(&zevstat.guids_interval, gt, where); 1305 } 1306 1307 static void 1308 zev_eventstat_interval(FILE *out) 1309 { 1310 uint64_t events; 1311 int i; 1312 zev_guidtrack_t *gt; 1313 1314 events = 0; 1315 for (i = ZEV_OP_MIN; i <= ZEV_OP_MAX; i++) { 1316 events += zevstat.events[i]; 1317 } 1318 1319 if (verbose) { 1320 fprintf(out, "%u %6llu %6llu %6llu %6llu ", 1321 time(NULL), 1322 events, 1323 zevstat.total_events, 1324 avl_numnodes(&zevstat.guids_interval), 1325 avl_numnodes(&zevstat.guids_runtime)); 1326 for (i = ZEV_OP_MIN; i <= ZEV_OP_MAX; i++) 1327 fprintf(out, "%6llu ", zevstat.events[i]); 1328 fprintf(out, "\n"); 1329 } else { 1330 fprintf(out, "%u %6llu %6llu %6llu %6llu\n", 1331 time(NULL), 1332 events, 1333 zevstat.total_events, 1334 avl_numnodes(&zevstat.guids_interval), 1335 avl_numnodes(&zevstat.guids_runtime)); 1336 } 1337 memset(&zevstat.events, 0, sizeof(zevstat.events)); 1338 zevstat.guids = 0; 1339 while (gt = avl_first(&zevstat.guids_interval)) 1340 avl_remove(&zevstat.guids_interval, gt); 1341 fflush(out); 1342 } 1343 1344 static int 1345 zev_evcompar(const void *a, const void *b) 1346 { 1347 const zev_guidtrack_t *ga = a; 1348 const zev_guidtrack_t *gb = b; 1349 1350 if (ga->guid > gb->guid) 1351 return 1; 1352 if (ga->guid < gb->guid) 1353 return -1; 1354 return 0; 1355 } 1356 1357 static int 1358 zev_zevstat(int fd, char *s_interval, char *s_count, char *outfile) 1359 { 1360 uint64_t interval = 1000; 1361 uint64_t ms; 1362 uint64_t t_until; 1363 uint64_t t_now; 1364 int cnt = -1; 1365 struct pollfd pfd[1]; 1366 int ret; 1367 char buf[4096]; 1368 zev_event_t *ev; 1369 int off = 0; 1370 zev_ioctl_add_queue_t aq; 1371 int q_fd; 1372 zev_guidtrack_t *gt; 1373 FILE *out = stdout; 1374 struct stat st; 1375 char filename[MAXPATHLEN]; 1376 int retry; 1377 1378 if (outfile) { 1379 retry = 0; 1380 strncpy(filename, outfile, sizeof(filename)); 1381 while (stat(filename, &st) == 0) { 1382 /* file exists */ 1383 snprintf(filename, sizeof(filename), 1384 "%s.%d", outfile, retry); 1385 retry++; 1386 } 1387 out = fopen(filename, "wb+"); 1388 if (!out) { 1389 perror("opening output file failed"); 1390 return (EXIT_FAILURE); 1391 } 1392 } 1393 1394 memset(&zevstat, 0, sizeof(zevstat)); 1395 avl_create(&zevstat.guids_runtime, zev_evcompar, 1396 sizeof(zev_guidtrack_t), 1397 offsetof(zev_guidtrack_t, avl_runtime)); 1398 avl_create(&zevstat.guids_interval, zev_evcompar, 1399 sizeof(zev_guidtrack_t), 1400 offsetof(zev_guidtrack_t, avl_interval)); 1401 1402 if (s_interval) { 1403 interval = atol(s_interval); 1404 if (interval == 0) { 1405 fprintf(stderr, "invalid interval.\n"); 1406 return (EXIT_FAILURE); 1407 } 1408 interval *= 1000; 1409 } 1410 if (s_count) { 1411 cnt = atol(s_count); 1412 if (interval == 0) { 1413 fprintf(stderr, "invalid count.\n"); 1414 return (EXIT_FAILURE); 1415 } 1416 } 1417 1418 aq.zev_max_queue_len = 1024 * 1024; 1419 aq.zev_flags = ZEV_FL_INITIALLY_EMPTY; 1420 snprintf(aq.zev_name, ZEV_MAX_QUEUE_NAME_LEN, 1421 "zevstat.%ld.%ld", time(NULL), getpid()); 1422 aq.zev_namelen = strlen(aq.zev_name); 1423 1424 if (ioctl(fd, ZEV_IOC_ADD_QUEUE, &aq)) { 1425 perror("adding temporary queue failed"); 1426 return (EXIT_FAILURE); 1427 } 1428 1429 snprintf(buf, sizeof(buf), 1430 "/devices/pseudo/zev@0:%s", aq.zev_name); 1431 q_fd = open(buf, O_RDONLY); 1432 if (q_fd < 0) { 1433 perror("opening queue device failed"); 1434 return (EXIT_FAILURE); 1435 } 1436 1437 pfd[0].fd = q_fd; 1438 pfd[0].events = POLLIN; 1439 1440 /* drain queue */ 1441 while ((ret = poll(pfd, 1, 0)) > 0) { 1442 if (read(q_fd, buf, sizeof(buf)) < 0) { 1443 perror("read failed"); 1444 close(q_fd); 1445 return(EXIT_FAILURE); 1446 } 1447 } 1448 if (ret < 0) { 1449 perror("poll failed"); 1450 close(q_fd); 1451 return(EXIT_FAILURE); 1452 } 1453 1454 fprintf(out, "timestamp events tevents guids tguids"); 1455 if (verbose) { 1456 fprintf(out, " error mark mount umount zvol_w "); 1457 fprintf(out, "zvol_t close create mkdir mxattr "); 1458 fprintf(out, "remove rmdir link symlnk rename "); 1459 fprintf(out, "write trunc setatt acl"); 1460 } 1461 fprintf(out, "\n"); 1462 while (cnt) { 1463 t_until = gethrtime() + (interval * 1000000); 1464 ms = interval; 1465 do { 1466 ret = poll(pfd, 1, ms); 1467 t_now = gethrtime(); 1468 if (t_now < t_until) { 1469 ms = t_until - t_now; 1470 ms /= 1000000ull; 1471 } 1472 if (ret < 0) { 1473 perror("poll failed"); 1474 close(q_fd); 1475 return(EXIT_FAILURE); 1476 } 1477 if (!(pfd[0].revents & POLLIN)) 1478 continue; 1479 /* data available */ 1480 ret = read(q_fd, buf, sizeof(buf)); 1481 if (ret < 0) { 1482 perror("read failed"); 1483 close(q_fd); 1484 return(EXIT_FAILURE); 1485 } 1486 if (ret == 0) 1487 continue; 1488 while (ret > off) { 1489 ev = (zev_event_t *)(buf + off); 1490 zev_eventstat(buf + off, ev->header.record_len); 1491 off += ev->header.record_len; 1492 } 1493 off = 0; 1494 } while ((t_now) < t_until && (ms > 0)); 1495 zev_eventstat_interval(out); 1496 if (cnt > 0) 1497 cnt--; 1498 } 1499 close(q_fd); 1500 if (outfile) 1501 fclose(out); 1502 while (gt = avl_first(&zevstat.guids_interval)) 1503 avl_remove(&zevstat.guids_interval, gt); 1504 while (gt = avl_first(&zevstat.guids_runtime)) { 1505 avl_remove(&zevstat.guids_runtime, gt); 1506 free(gt); 1507 } 1508 return EXIT_SUCCESS; 1509 } 1510 1511 static int 1512 zev_report(int fd, char *basename) 1513 { 1514 char filename[MAXPATHLEN]; 1515 char count[10]; 1516 time_t now; 1517 time_t midnight; 1518 struct tm tm; 1519 int minutes; 1520 int ret; 1521 1522 verbose++; 1523 while (1) { 1524 now = time(NULL); 1525 localtime_r(&now, &tm); 1526 snprintf(filename, sizeof(filename), "%s.%04d-%02d-%02d", 1527 basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); 1528 tm.tm_sec = 0; 1529 tm.tm_min = 0; 1530 tm.tm_hour = 0; 1531 tm.tm_mday++; /* works for Jan 32nd, Feb 30th, etc. */ 1532 midnight = mktime(&tm); 1533 if (now % 60) 1534 sleep(60 - (now % 60)); 1535 minutes = (midnight - time(NULL)) / 60; 1536 snprintf(count, sizeof(count), "%d", minutes); 1537 ret = zev_zevstat(fd, "60", count, filename); 1538 if (ret) 1539 return EXIT_FAILURE; 1540 } 1541 return EXIT_SUCCESS; /* never reached */ 1542 } 1543 1544 static int 1545 zev_get_zev_version(int fd) 1546 { 1547 zev_ioctl_get_zev_version vi; 1548 1549 if (ioctl(fd, ZEV_IOC_GET_ZEV_VERSION, &vi)) { 1550 perror("getting zev cersion info failed"); 1551 return (EXIT_FAILURE); 1552 } 1553 1554 printf("zev major version: %llu\n", vi.zev_major_version); 1555 printf("zev minor version: %llu\n", vi.zev_minor_version); 1556 return 0; 1557 } 1558 1559 static void 1560 zev_sigint(int sig) 1561 { 1562 fflush(stdout); 1563 } 1564 1565 int 1566 main(int argc, char **argv) 1567 { 1568 int fd; 1569 int c; 1570 extern char *optarg; 1571 int create_tmp_queue = 1; 1572 char buf[MAXPATHLEN]; 1573 int mode = 0; 1574 char *arg = NULL; 1575 char *arg2 = NULL; 1576 char *p; 1577 1578 sigset(SIGINT, zev_sigint); 1579 1580 /* open device */ 1581 fd = open(zev_device, O_RDONLY); 1582 if (fd < 0) { 1583 perror("opening zev device failed"); 1584 return EXIT_FAILURE; 1585 } 1586 1587 p = strrchr(argv[0], '/'); 1588 if (!p) { 1589 p = argv[0]; 1590 } else { 1591 p++; 1592 } 1593 if (!strcmp(p, "zevstat")) { 1594 mode = MD_ZEVSTAT; 1595 if (argc < 2) 1596 zevstat_usage(argv[0]); 1597 if (!strcmp(argv[1], "-v")) { 1598 if (argc < 3) 1599 zevstat_usage(argv[0]); 1600 verbose++; 1601 arg = argv[2]; 1602 arg2 = argv[3]; 1603 } else { 1604 arg = argv[1]; 1605 arg2 = argv[2]; 1606 } 1607 return zev_zevstat(fd, arg, arg2, NULL); 1608 } else if(!strcmp(p, "zevreport")) { 1609 mode = MD_ZEV_REPORT; 1610 if (argc != 2) 1611 zevreport_usage(argv[0]); 1612 return zev_report(fd, argv[1]); 1613 } 1614 1615 while ((c = getopt(argc, argv, 1616 "a:A:b:B:c:d:Df:ghk:lL:m:M:pP:q:Q:r:R:st:T:vV?")) != -1) { 1617 switch(c) { 1618 case 'g': 1619 grep_friendly++; 1620 verbose++; 1621 break; 1622 case 'v': 1623 verbose++; 1624 break; 1625 case 's': 1626 mode = MD_STATISTICS; 1627 break; 1628 case 'p': 1629 mode = MD_POLL_EVENTS; 1630 break; 1631 case 'c': 1632 mode = MD_CHECKSUMS; 1633 arg = optarg; 1634 break; 1635 case 'D': 1636 mode = MD_DEBUG_INFO; 1637 break; 1638 case 'd': 1639 close(fd); 1640 zev_device = optarg; 1641 fd = open(zev_device, O_RDONLY); 1642 if (fd < 0) { 1643 perror("opening zev device failed"); 1644 return EXIT_FAILURE; 1645 } 1646 create_tmp_queue = 0; 1647 break; 1648 case 'q': 1649 snprintf(buf, sizeof(buf), 1650 "/devices/pseudo/zev@0:%s", optarg); 1651 close(fd); 1652 zev_device = buf; 1653 fd = open(zev_device, O_RDONLY); 1654 if (fd < 0) { 1655 perror("opening zev device failed"); 1656 return EXIT_FAILURE; 1657 } 1658 create_tmp_queue = 0; 1659 break; 1660 case 'f': 1661 fd = open(optarg, O_RDONLY); 1662 if (fd < 0) { 1663 perror("opening spool file failed"); 1664 return EXIT_FAILURE; 1665 } 1666 mode = MD_DUMP_SPOOL; 1667 break; 1668 case 'l': 1669 mode = MD_LIST_QUEUES; 1670 break; 1671 case 'Q': 1672 mode = MD_SET_GLOBAL_MAX_QUEUE_LEN; 1673 arg = optarg; 1674 break; 1675 case 'L': 1676 mode = MD_SET_MAX_QUEUE_LEN; 1677 arg = optarg; 1678 arg2 = argv[optind]; 1679 break; 1680 case 'T': 1681 mode = MD_ZEVSTAT; 1682 arg = optarg; 1683 arg2 = argv[optind]; 1684 break; 1685 case 'R': 1686 mode = MD_ZEV_REPORT; 1687 arg = optarg; 1688 break; 1689 case 't': 1690 mode = MD_SET_POLL_WAKEUP_QUEUE_LEN; 1691 arg = optarg; 1692 arg2 = argv[optind]; 1693 break; 1694 case 'm': 1695 mode = MD_MUTE_POOL; 1696 arg = optarg; 1697 break; 1698 case 'M': 1699 mode = MD_UNMUTE_POOL; 1700 arg = optarg; 1701 break; 1702 case 'k': 1703 mode = MD_MARK; 1704 arg = optarg; 1705 break; 1706 case 'a': 1707 mode = MD_ADD_QUEUE; 1708 arg = optarg; 1709 break; 1710 case 'A': 1711 mode = MD_ADD_BLOCKING_QUEUE; 1712 arg = optarg; 1713 break; 1714 case 'r': 1715 mode = MD_REMOVE_QUEUE; 1716 arg = optarg; 1717 break; 1718 case 'b': 1719 mode = MD_QUEUE_BLOCKING; 1720 arg = optarg; 1721 break; 1722 case 'B': 1723 mode = MD_QUEUE_NONBLOCKING; 1724 arg = optarg; 1725 break; 1726 case 'P': 1727 mode = MD_QUEUE_PROPERTIES; 1728 arg = optarg; 1729 break; 1730 case 'V': 1731 mode = MD_GET_ZEV_VERSION; 1732 break; 1733 case 'h': 1734 case '?': 1735 default: 1736 usage(argv[0]); 1737 } 1738 } 1739 1740 switch (mode) { 1741 case MD_STATISTICS: 1742 return zev_statistics(fd); 1743 case MD_POLL_EVENTS: 1744 return zev_poll_events(fd, create_tmp_queue); 1745 case MD_DUMP_SPOOL: 1746 return zev_dump_spool(fd); 1747 case MD_CHECKSUMS: 1748 return zev_checksum(fd, arg); 1749 case MD_DEBUG_INFO: 1750 return zev_debug_info(fd); 1751 case MD_LIST_QUEUES: 1752 return zev_list_queues(fd); 1753 case MD_SET_GLOBAL_MAX_QUEUE_LEN: 1754 return zev_set_global_max_queue_len(fd, arg); 1755 case MD_SET_MAX_QUEUE_LEN: 1756 return zev_set_max_queue_len(fd, arg, arg2); 1757 case MD_SET_POLL_WAKEUP_QUEUE_LEN: 1758 return zev_set_poll_wakeup_queue_len(fd, arg, arg2); 1759 case MD_ZEVSTAT: 1760 return zev_zevstat(fd, arg, arg2, NULL); 1761 case MD_ZEV_REPORT: 1762 return zev_report(fd, arg); 1763 case MD_MUTE_POOL: 1764 return zev_mute_pool(fd, arg); 1765 case MD_UNMUTE_POOL: 1766 return zev_unmute_pool(fd, arg); 1767 case MD_MARK: 1768 return zev_mark(fd, arg); 1769 case MD_ADD_QUEUE: 1770 return zev_add_queue(fd, arg, 0); 1771 case MD_ADD_BLOCKING_QUEUE: 1772 return zev_add_queue(fd, arg, 1); 1773 case MD_REMOVE_QUEUE: 1774 return zev_remove_queue(fd, arg); 1775 case MD_QUEUE_BLOCKING: 1776 return zev_queue_blocking(fd, arg, 0); 1777 case MD_QUEUE_NONBLOCKING: 1778 return zev_queue_blocking(fd, arg, 1); 1779 case MD_QUEUE_PROPERTIES: 1780 return zev_queue_properties(fd, arg); 1781 case MD_GET_ZEV_VERSION: 1782 return zev_get_zev_version(fd); 1783 default: 1784 close(fd); 1785 usage(argv[0]); 1786 return EXIT_FAILURE; 1787 }; 1788 } 1789 1790