xref: /titanic_41/usr/src/cmd/zevadm/zevadm.c (revision 936260673937830abacea5e1c31988a86d2d9f0a)
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 
67 static int verbose = 0;
68 static int grep_friendly = 0;
69 
70 static void
71 zpf(char *fmt, ...)
72 {
73 	va_list	ap;
74 
75 	va_start(ap, fmt);
76 	vprintf(fmt, ap);
77 	va_end(ap);
78 	if (grep_friendly) {
79 		printf(" ");
80 	} else {
81 		printf("\n");
82 	}
83 }
84 
85 static void
86 znl(void)
87 {
88 	if (grep_friendly)
89 		printf("\n");
90 }
91 
92 static void
93 sig2hex_direct(const uint8_t *sig, char *hex)
94 {
95 	int     i;
96 
97 	for (i = 0; i < SHA1_DIGEST_LENGTH; ++i) {
98 		sprintf(hex + 2 * i, "%02x", sig[i]);
99 	}
100 	hex[SHA1_DIGEST_LENGTH * 2] = '\0';
101 }
102 
103 static int
104 zev_statistics(int fd)
105 {
106 	zev_statistics_t zs;
107 	if (ioctl(fd, ZEV_IOC_GET_GLOBAL_STATISTICS, &zs)) {
108 		perror("getting statistics data failed");
109 		return (EXIT_FAILURE);
110 	}
111 	printf("ZEV module state:\n");
112 
113 	printf("    queue length in bytes   : %lu\n", zs.zev_queue_len);
114 	printf("    queue length limit      : %lu\n", zs.zev_max_queue_len);
115 	printf("    bytes read from device  : %lu\n", zs.zev_bytes_read);
116 	printf("    module internal errors  : %lu\n\n", zs.zev_cnt_errors);
117 
118 	printf("    discarded events        : %lu\n",
119 	    zs.zev_cnt_discarded_events);
120 	printf("    discarded bytes         : %lu\n\n", zs.zev_bytes_discarded);
121 
122 	printf("ZFS event statistics:\n");
123 
124 	printf("    total ZFS events        : %lu\n", zs.zev_cnt_total_events);
125 	printf("    ZFS mount               : %lu\n", zs.zev_cnt_zfs_mount);
126 	printf("    ZFS umount              : %lu\n", zs.zev_cnt_zfs_umount);
127 	printf("    ZVOL write              : %lu\n", zs.zev_cnt_zvol_write);
128 	printf("    ZVOL truncate           : %lu\n", zs.zev_cnt_zvol_truncate);
129 	printf("    ZNODE close after update: %lu\n",
130 	    zs.zev_cnt_znode_close_after_update);
131 	printf("    ZNODE create            : %lu\n", zs.zev_cnt_znode_create);
132 	printf("    ZNODE remove            : %lu\n", zs.zev_cnt_znode_remove);
133 	printf("    ZNODE link              : %lu\n", zs.zev_cnt_znode_link);
134 	printf("    ZNODE symlink           : %lu\n", zs.zev_cnt_znode_symlink);
135 	printf("    ZNODE rename            : %lu\n", zs.zev_cnt_znode_rename);
136 	printf("    ZNODE write             : %lu\n", zs.zev_cnt_znode_write);
137 	printf("    ZNODE truncate          : %lu\n",
138 	    zs.zev_cnt_znode_truncate);
139 	printf("    ZNODE setattr           : %lu\n", zs.zev_cnt_znode_setattr);
140 	printf("    ZNODE acl               : %lu\n", zs.zev_cnt_znode_acl);
141 	return EXIT_SUCCESS;
142 }
143 
144 static void
145 zev_print_inode_info(char *name, zev_inode_info_t *info)
146 {
147 	zpf("  %s.inode: %llu", name, info->ino);
148 	zpf("  %s.gen: %llu", name, info->gen);
149 	zpf("  %s.mtime: %llu", name, info->mtime);
150 	zpf("  %s.ctime: %llu", name, info->ctime);
151 	zpf("  %s.size: %llu", name, info->size);
152 	zpf("  %s.mode: %llo", name, info->mode);
153 	zpf("  %s.links: %llu", name, info->links);
154 	zpf("  %s.type: %lu", name, info->type);
155 	zpf("  %s.flags: %lu", name, info->flags);
156 }
157 
158 static void
159 zev_print_mark_payload(zev_mark_t *rec)
160 {
161 	int i;
162 	int j;
163 	uint8_t *p;
164 	char c;
165 
166 	zpf("  payload:");
167 	p = (uint8_t *)ZEV_PAYLOAD(rec);
168 	for (i=0; i<rec->payload_len; i+=16) {
169 		printf("  ");
170 		for (j=i; j<rec->payload_len && j<i+16; j++) {
171 			printf("%02x ", p[j]);
172 			if (j == i + 7)
173 				printf(" ");
174 		}
175 		if (grep_friendly)
176 			continue;
177 		for (; j<i+16; j++) {
178 			printf("   ");
179 			if (j == i + 7)
180 				printf(" ");
181 		}
182 		printf("    ");
183 		for (j=i; j<rec->payload_len && j<i+16; j++) {
184 			c = '.';
185 			if (p[j] >= ' ' && p[j] <= '~')
186 				c = p[j];
187 			printf("%c", c);
188 			if (j == i + 7)
189 				printf(" ");
190 		}
191 		printf("\n");
192 	}
193 }
194 
195 static void
196 zev_print_error(char *buf)
197 {
198 	zev_error_t *rec = (zev_error_t *)buf;
199 	time_t op_time = rec->op_time;
200 	char *ct = ctime(&op_time); ct[24] = '\0';
201 
202 	if (verbose) {
203 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
204 		zpf("  guid: %llu", rec->guid);
205 		zpf("  failed.op: %s",
206 		    zev_op_name[rec->failed_op - ZEV_OP_MIN]);
207 		zpf("  message: %s", ZEV_ERRSTR(rec));
208 		znl();
209 	} else {
210 		printf("%s %s: failed_op=%s msg=%s\n",
211 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
212 		       zev_op_name[rec->failed_op - ZEV_OP_MIN],
213 		       ZEV_ERRSTR(rec));
214 	}
215 }
216 
217 static void
218 zev_print_mark(char *buf)
219 {
220 	zev_mark_t *rec = (zev_mark_t *)buf;
221 	time_t op_time = rec->op_time;
222 	char *ct = ctime(&op_time); ct[24] = '\0';
223 
224 	if (verbose) {
225 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
226 		zpf("  guid: %llu", rec->guid);
227 		zpf("  mark.id: %llu", rec->mark_id);
228 		zpf("  payload.len: %llu", rec->payload_len);
229 		if (rec->payload_len)
230 			zev_print_mark_payload(rec);
231 		znl();
232 	} else {
233 		printf("%s %s: guid=%llu mark_id=%lld payload_len=%ld "
234 		       "payload=\"%.*s\"\n",
235 		       ct, zev_op_name[rec->op - ZEV_OP_MIN], rec->guid,
236 		       rec->mark_id, rec->payload_len,
237 		       rec->payload_len, (char *)(rec + 1));
238 	}
239 }
240 
241 static void
242 zev_print_zfs_mount(char *buf)
243 {
244 	zev_zfs_mount_t *rec = (zev_zfs_mount_t *)buf;
245 	time_t op_time = rec->op_time;
246 	char *ct = ctime(&op_time); ct[24] = '\0';
247 
248 	if (verbose) {
249 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
250 		zpf("  guid: %llu", rec->guid);
251 		zpf("  dataset: %s", ZEV_DATASET(rec));
252 		zpf("  mountpoint: %s", ZEV_MOUNTPOINT(rec));
253 		zpf("  remount: %s", rec->remount ? "true" : "false");
254 		zev_print_inode_info("root", &rec->root);
255 		znl();
256 	} else {
257 		printf("%s %s: guid=%llu remount=%s dataset='%s' "
258 		       "mountpoint='%s'\n",
259 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
260 		       rec->guid,
261 		       rec->remount ? "true" : "false",
262 		       ZEV_DATASET(rec),
263 		       ZEV_MOUNTPOINT(rec));
264 	}
265 }
266 
267 static void
268 zev_print_zfs_umount(char *buf)
269 {
270 	zev_zfs_umount_t *rec = (zev_zfs_umount_t *)buf;
271 	time_t op_time = rec->op_time;
272 	char *ct = ctime(&op_time); ct[24] = '\0';
273 
274 	if (verbose) {
275 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
276 		zpf("  guid: %llu", rec->guid);
277 		zev_print_inode_info("covered", &rec->covered);
278 		znl();
279 	} else {
280 		printf("%s %s: guid=%llu\n",
281 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
282 		       rec->guid);
283 	}
284 }
285 
286 static void
287 zev_print_zvol_truncate(char *buf)
288 {
289 	zev_zvol_truncate_t *rec = (zev_zvol_truncate_t *)buf;
290 	time_t op_time = rec->op_time;
291 	char *ct = ctime(&op_time); ct[24] = '\0';
292 
293 	if (verbose) {
294 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
295 		zpf("  guid: %llu", rec->guid);
296 		zpf("  txg: %llu", rec->txg);
297 		zpf("  offset: %llu", rec->offset);
298 		zpf("  length: %llu", rec->length);
299 		znl();
300 	} else {
301 		printf("%s %s: guid=%llu offset=%llu length=%llu\n",
302 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
303 		       rec->guid,
304 		       rec->offset,
305 		       rec->length);
306 	}
307 }
308 
309 static void
310 zev_print_zvol_write(char *buf)
311 {
312 	zev_print_zvol_truncate(buf);
313 }
314 
315 static void
316 zev_print_znode_close_after_update(char *buf)
317 {
318 	zev_znode_close_after_update_t *rec =
319 	    (zev_znode_close_after_update_t *)buf;
320 	time_t op_time = rec->op_time;
321 	char *ct = ctime(&op_time); ct[24] = '\0';
322 
323 	if (verbose) {
324 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
325 		zpf("  guid: %llu", rec->guid);
326 		zev_print_inode_info("file", &rec->file);
327 		znl();
328 	} else {
329 		printf("%s %s: guid=%llu file=%llu.%llu\n",
330 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
331 		       rec->guid,
332 		       rec->file.ino, rec->file.gen);
333 	}
334 }
335 
336 static void
337 zev_print_znode_create(char *buf)
338 {
339 	zev_znode_create_t *rec = (zev_znode_create_t *)buf;
340 	time_t op_time = rec->op_time;
341 	char *ct = ctime(&op_time); ct[24] = '\0';
342 	zev_sig_t *sig;
343 	char sigval[(SHA1_DIGEST_LENGTH * 2) + 1];
344 
345 	if (verbose) {
346 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
347 		zpf("  guid: %llu", rec->guid);
348 		zpf("  txg: %llu", rec->txg);
349 		zpf("  name: '%s'", ZEV_NAME(rec));
350 		sig = &rec->signature;
351 		sig2hex_direct(sig->value, sigval);
352 		zpf("  sig: level %d, offset %llu, value %s",
353 		    sig->level, sig->block_offset, sigval);
354 		zev_print_inode_info("file", &rec->file);
355 		zev_print_inode_info("parent", &rec->parent);
356 		znl();
357 	} else {
358 		printf("%s %s: guid=%llu parent=%llu.%llu file=%llu.%llu "
359 		       "file.mtime=%llu, parent.mtime=%llu, name='%s'\n",
360 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
361 		       rec->guid,
362 		       rec->parent.ino, rec->parent.gen,
363 		       rec->file.ino, rec->file.gen,
364 		       rec->file.mtime, rec->parent.mtime,
365 		       ZEV_NAME(rec));
366 	}
367 }
368 
369 static void
370 zev_print_znode_mkdir(char *buf)
371 {
372 	zev_print_znode_create(buf);
373 }
374 
375 static void
376 zev_print_znode_make_xattr_dir(char *buf)
377 {
378 	zev_print_znode_create(buf);
379 }
380 
381 static void
382 zev_print_znode_remove(char *buf)
383 {
384 	zev_znode_remove_t *rec = (zev_znode_remove_t *)buf;
385 	time_t op_time = rec->op_time;
386 	char *ct = ctime(&op_time); ct[24] = '\0';
387 
388 	if (verbose) {
389 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
390 		zpf("  guid: %llu", rec->guid);
391 		zpf("  txg: %llu", rec->txg);
392 		zpf("  file.name: '%s'", ZEV_NAME(rec));
393 		zev_print_inode_info("file", &rec->file);
394 		zev_print_inode_info("parent", &rec->parent);
395 		znl();
396 	} else {
397 		printf("%s %s: guid=%llu parent=%llu.%llu "
398 		       "file.mtime=%llu name='%s'\n",
399 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
400 		       rec->guid,
401 		       rec->parent.ino, rec->parent.gen,
402 		       rec->file.mtime,
403 		       ZEV_NAME(rec));
404 	}
405 }
406 
407 static void
408 zev_print_znode_rmdir(char *buf)
409 {
410 	zev_print_znode_remove(buf);
411 }
412 
413 static void
414 zev_print_znode_link(char *buf)
415 {
416 	zev_znode_link_t *rec = (zev_znode_link_t *)buf;
417 	time_t op_time = rec->op_time;
418 	char *ct = ctime(&op_time); ct[24] = '\0';
419 
420 	if (verbose) {
421 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
422 		zpf("  guid: %llu", rec->guid);
423 		zpf("  txg: %llu", rec->txg);
424 		zpf("  link.name: '%s'", ZEV_NAME(rec));
425 		zev_print_inode_info("file", &rec->file);
426 		zev_print_inode_info("parent", &rec->parent);
427 		znl();
428 	} else {
429 		printf("%s %s: parent=%llu.%llu file=%llu.%llu "
430 		       "file.ctime=%llu parent.ctime=%llu name='%s'\n",
431 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
432 		       rec->parent.ino, rec->parent.gen,
433 		       rec->file.ino, rec->file.gen,
434 		       rec->file.ctime, rec->parent.ctime,
435 		       ZEV_NAME(rec));
436 	}
437 }
438 
439 static void
440 zev_print_znode_symlink(char *buf)
441 {
442 	zev_znode_symlink_t *rec = (zev_znode_symlink_t *)buf;
443 	time_t op_time = rec->op_time;
444 	char *ct = ctime(&op_time); ct[24] = '\0';
445 	zev_sig_t *sig;
446 	char sigval[(SHA1_DIGEST_LENGTH * 2) + 1];
447 
448 	if (verbose) {
449 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
450 		zpf("  guid: %llu", rec->guid);
451 		zpf("  txg: %llu", rec->txg);
452 		zpf("  symlink.name: '%s'", ZEV_NAME(rec));
453 		zpf("  symlink.link: '%s'", ZEV_LINK(rec));
454 		sig = &rec->signature;
455 		sig2hex_direct(sig->value, sigval);
456 		zpf("  sig: level %d, offset %llu, value %s",
457 		    sig->level, sig->block_offset, sigval);
458 		zev_print_inode_info("file", &rec->file);
459 		zev_print_inode_info("parent", &rec->parent);
460 		znl();
461 	} else {
462 		printf("%s %s: parent=%llu.%llu file=%llu.%llu "
463 		       "name='%s' link='%s'\n",
464 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
465 		       rec->parent.ino, rec->parent.gen,
466 		       rec->file.ino, rec->file.gen,
467 		       ZEV_NAME(rec),
468 		       ZEV_LINK(rec));
469 	}
470 }
471 
472 static void
473 zev_print_znode_rename(char *buf)
474 {
475 	zev_znode_rename_t *rec = (zev_znode_rename_t *)buf;
476 	time_t op_time = rec->op_time;
477 	char *ct = ctime(&op_time); ct[24] = '\0';
478 
479 	if (verbose) {
480 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
481 		zpf("  guid: %llu", rec->guid);
482 		zpf("  txg: %llu", rec->txg);
483 		zpf("  file.srcname: '%s'", ZEV_SRCNAME(rec));
484 		zpf("  file.dstname: '%s'", ZEV_DSTNAME(rec));
485 		zev_print_inode_info("file", &rec->file);
486 		if (rec->clobbered_file.ino)
487 			zev_print_inode_info("clobbered_file",
488 			                     &rec->clobbered_file);
489 		zev_print_inode_info("srcdir", &rec->srcdir);
490 		zev_print_inode_info("dstdir", &rec->dstdir);
491 		znl();
492 	} else {
493 		printf("%s %s: srcdir=%llu.%llu dstdir=%llu.%llu "
494 		       "file=%llu.%llu file.mtime=%llu, file.ctime=%llu, "
495 		       "srcdir.mtime=%llu, srcdir.ctime=%llu, "
496 		       "dstdir.mtime=%llu, dstdir.ctime=%llu, "
497 		       "srcname='%s' dstname='%s'\n",
498 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
499 		       rec->srcdir.ino, rec->srcdir.gen,
500 		       rec->dstdir.ino, rec->dstdir.gen,
501 		       rec->file.ino, rec->file.gen,
502 		       rec->file.mtime, rec->file.ctime,
503 		       rec->srcdir.mtime, rec->srcdir.ctime,
504 		       rec->dstdir.mtime, rec->dstdir.ctime,
505 		       ZEV_SRCNAME(rec),
506 		       ZEV_DSTNAME(rec));
507 	}
508 }
509 
510 static void
511 zev_print_znode_write(char *buf)
512 {
513 	zev_znode_write_t *rec = (zev_znode_write_t *)buf;
514 	time_t op_time = rec->op_time;
515 	char *ct = ctime(&op_time); ct[24] = '\0';
516 	zev_sig_t *sig;
517 	char sigval[(SHA1_DIGEST_LENGTH * 2) + 1];
518 	int i;
519 
520 	if (verbose) {
521 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
522 		zpf("  guid: %llu", rec->guid);
523 		zpf("  txg: %llu", rec->txg);
524 		zpf("  offset: %llu", rec->offset);
525 		zpf("  length: %llu", rec->length);
526 		zev_print_inode_info("file", &rec->file);
527 		znl();
528 		for (i=0; i<rec->signature_cnt; i++) {
529 			sig = (zev_sig_t *)ZEV_SIGNATURES(rec);
530 			sig += i;
531 			sig2hex_direct(sig->value, sigval);
532 			zpf("  sig: level %d, offset %llu, value %s",
533 			    sig->level, sig->block_offset, sigval);
534 		}
535 	} else {
536 		printf("%s %s: file=%llu.%llu offset=%llu length=%llu\n",
537 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
538 		       rec->file.ino, rec->file.gen,
539 		       rec->offset, rec->length);
540 	}
541 }
542 
543 static void
544 zev_print_znode_truncate(char *buf)
545 {
546 	zev_print_znode_write(buf);
547 }
548 
549 static void
550 zev_print_znode_setattr(char *buf)
551 {
552 	zev_znode_setattr_t *rec = (zev_znode_setattr_t *)buf;
553 	time_t op_time = rec->op_time;
554 	char *ct = ctime(&op_time); ct[24] = '\0';
555 
556 	if (verbose) {
557 		zpf("%s %s", ct, zev_op_name[rec->op - ZEV_OP_MIN]);
558 		zpf("  guid: %llu", rec->guid);
559 		zpf("  txg: %llu", rec->txg);
560 		zev_print_inode_info("file", &rec->file);
561 		znl();
562 	} else {
563 		printf("%s %s: file=%llu.%llu mtime=%llu\n",
564 		       ct, zev_op_name[rec->op - ZEV_OP_MIN],
565 		       rec->file.ino, rec->file.gen, rec->file.mtime);
566 	}
567 }
568 
569 static void
570 zev_print_znode_acl(char *buf)
571 {
572 	zev_print_znode_setattr(buf);
573 }
574 
575 static void
576 zev_print_event(char *buf, int len)
577 {
578 	int record_len;
579 	int op;
580 
581 	record_len = *(uint32_t *)buf;
582 	if (record_len != len) {
583 		fprintf(stderr, "record length mismatch: got %d, expected %d\n",
584 		        record_len, len);
585 		exit(1);
586 	}
587 	op = *((uint32_t *)buf + 1);
588 	if (op < ZEV_OP_MIN || op > ZEV_OP_MAX) {
589 		fprintf(stderr, "unknown op code: %d\n", op);
590 		exit(1);
591 	}
592 	switch (op) {
593 	case ZEV_OP_ERROR:
594 		zev_print_error(buf);
595 		break;
596 	case ZEV_OP_MARK:
597 		zev_print_mark(buf);
598 		break;
599 	case ZEV_OP_ZFS_MOUNT:
600 		zev_print_zfs_mount(buf);
601 		break;
602 	case ZEV_OP_ZFS_UMOUNT:
603 		zev_print_zfs_umount(buf);
604 		break;
605 	case ZEV_OP_ZVOL_TRUNCATE:
606 		zev_print_zvol_truncate(buf);
607 		break;
608 	case ZEV_OP_ZVOL_WRITE:
609 		zev_print_zvol_write(buf);
610 		break;
611 	case ZEV_OP_ZNODE_CLOSE_AFTER_UPDATE:
612 		zev_print_znode_close_after_update(buf);
613 		break;
614 	case ZEV_OP_ZNODE_CREATE:
615 		zev_print_znode_create(buf);
616 		break;
617 	case ZEV_OP_ZNODE_MKDIR:
618 		zev_print_znode_mkdir(buf);
619 		break;
620 	case ZEV_OP_ZNODE_MAKE_XATTR_DIR:
621 		zev_print_znode_make_xattr_dir(buf);
622 		break;
623 	case ZEV_OP_ZNODE_REMOVE:
624 		zev_print_znode_remove(buf);
625 		break;
626 	case ZEV_OP_ZNODE_RMDIR:
627 		zev_print_znode_rmdir(buf);
628 		break;
629 	case ZEV_OP_ZNODE_LINK:
630 		zev_print_znode_link(buf);
631 		break;
632 	case ZEV_OP_ZNODE_SYMLINK:
633 		zev_print_znode_symlink(buf);
634 		break;
635 	case ZEV_OP_ZNODE_RENAME:
636 		zev_print_znode_rename(buf);
637 		break;
638 	case ZEV_OP_ZNODE_WRITE:
639 		zev_print_znode_write(buf);
640 		break;
641 	case ZEV_OP_ZNODE_TRUNCATE:
642 		zev_print_znode_truncate(buf);
643 		break;
644 	case ZEV_OP_ZNODE_SETATTR:
645 		zev_print_znode_setattr(buf);
646 		break;
647 	case ZEV_OP_ZNODE_ACL:
648 		zev_print_znode_acl(buf);
649 		break;
650 	default:
651 		fprintf(stderr, "unhandled op code: %d\n", op);
652 		exit(1);
653 	}
654 }
655 
656 static int
657 zev_poll_events(int fd, int create_tmp_queue)
658 {
659 	struct pollfd pfd[1];
660 	int ret;
661 	char buf[4096];
662 	zev_event_t *ev;
663 	int off = 0;
664 	zev_ioctl_add_queue_t aq;
665 	int q_fd;
666 
667 	if (create_tmp_queue) {
668 		aq.zev_max_queue_len = 0;
669 		aq.zev_flags = ZEV_FL_INITIALLY_EMPTY;
670 		snprintf(aq.zev_name, ZEV_MAX_QUEUE_NAME_LEN,
671 			 "zevadm.%ld.%ld", time(NULL), getpid());
672 		aq.zev_namelen = strlen(aq.zev_name);
673 
674 		if (ioctl(fd, ZEV_IOC_ADD_QUEUE, &aq)) {
675 			perror("adding temporary queue failed");
676 			return (EXIT_FAILURE);
677 		}
678 
679 		snprintf(buf, sizeof(buf),
680 		         "/devices/pseudo/zev@0:%s", aq.zev_name);
681 		q_fd = open(buf, O_RDONLY);
682 		if (q_fd < 0) {
683 			perror("opening queue device failed");
684 			return (EXIT_FAILURE);
685 		}
686 	} else {
687 		q_fd = fd;
688 	}
689 
690 	while (1) {
691 		pfd[0].fd = q_fd;
692 		pfd[0].events = POLLIN;
693 		ret = poll(pfd, 1, 1000);
694 		if (ret < 0) {
695 			perror("poll failed");
696 			close(q_fd);
697 			return(EXIT_FAILURE);
698 		}
699 		if (!(pfd[0].revents & POLLIN))
700 			continue;
701 		/* data available */
702 		ret = read(q_fd, buf, sizeof(buf));
703 		if (ret < 0) {
704 			perror("read failed");
705 			close(q_fd);
706 			return(EXIT_FAILURE);
707 		}
708 		if (ret == 0)
709 			continue;
710 		while (ret > off) {
711 			ev = (zev_event_t *)(buf + off);
712 			zev_print_event(buf + off, ev->header.record_len);
713 			off += ev->header.record_len;
714 		}
715 		off = 0;
716 	}
717 	if (create_tmp_queue)
718 		close(q_fd);
719 	return EXIT_SUCCESS;
720 }
721 
722 static int
723 zev_dump_spool(int fd)
724 {
725 	int len;
726 	char buf[4096];
727 	int off = 0;
728 
729 	while (1) {
730 		len = read(fd, buf + off, sizeof(buf) - off);
731 		if (len == -1) {
732 			fprintf(stderr, "reading from spool failed: %s\n",
733 				strerror(errno));
734 			return EXIT_FAILURE;
735 		}
736 		if (len == 0)
737 			break;
738 
739 		len += off;
740 		off = 0;
741 		while (len > off + sizeof(uint32_t)) {
742 			uint32_t evlen;
743 			char *mp;
744 			zev_event_t *ev;
745 
746 			ev = (zev_event_t *)(buf + off);
747 			evlen = ev->header.record_len;
748 			if (len < off + evlen + 1)
749 				break;
750 			mp = buf + off + evlen;
751 			if (!memchr(mp, 0, len - off - evlen))
752 				break;
753 			zev_print_event(buf + off, ev->header.record_len);
754 			off += ev->header.record_len + strlen(mp) + 1;
755 		}
756 
757 		memmove(buf, buf + off, len - off);
758 		off = len - off;
759 	}
760 
761 	return EXIT_SUCCESS;
762 }
763 
764 static void
765 usage(char *progname)
766 {
767 	fprintf(stderr, "usage: %s [-d <dev>] [options]\n", progname);
768 	fprintf(stderr, "\n");
769 	fprintf(stderr, " Status information:\n");
770 	fprintf(stderr, "   -s                   show zev statistics\n");
771 	fprintf(stderr, "   -p                   poll for ZFS events\n");
772 	fprintf(stderr, "   -f <name>            dump events from spool\n");
773 	fprintf(stderr, "   -D                   print zev module debug "
774 	        "information\n");
775 	fprintf(stderr, "   -T <interval> <cnt>  zevstat mode\n");
776 	fprintf(stderr, "   -R <base filename>   zevreport mode\n");
777 	fprintf(stderr, "\n");
778 	fprintf(stderr, " Tune zev module settings:\n");
779 	fprintf(stderr, "   -Q <bytes>           set maximum event queue "
780 	        "length\n");
781 	fprintf(stderr, "   -m <pool>            mute pool, no events for "
782 	        "this pool\n");
783 	fprintf(stderr, "   -M <pool>            unmute pool\n");
784 	fprintf(stderr, "\n");
785 	fprintf(stderr, " Queue management:\n");
786 	fprintf(stderr, "   -l                   list queues\n");
787 	fprintf(stderr, "   -a <name>            add non-blocking queue\n");
788 	fprintf(stderr, "   -A <name>            add blocking queue\n");
789 	fprintf(stderr, "   -r <name>            remove queue\n");
790 	fprintf(stderr, "   -b <name>            make queue non-blocking "
791 	        "(default)\n");
792 	fprintf(stderr, "   -B <name>            make queue block when full\n");
793 	fprintf(stderr, "   -P <name>            display queue properties\n");
794 	fprintf(stderr, "   -L <name> <bytes>    set maximum event queue "
795 	        "length\n");
796 	fprintf(stderr, "   -t <name> <bytes>    set queue length poll "
797 	        "throttle\n");
798 	fprintf(stderr, "\n");
799 	fprintf(stderr, " Other options:\n");
800 	fprintf(stderr, "   -d <dev>             non-default device file. "
801 	        "('%s')\n", ZEV_DEVICE);
802 	fprintf(stderr, "   -q <name>            use device file for this "
803 		"queue name\n");
804 	fprintf(stderr, "   -k <guid>:<payload>  queue mark event\n");
805 	fprintf(stderr, "   -c <filename>        list file's content "
806 		"checksums\n");
807 	fprintf(stderr, "   -v                   verbose: additional output "
808 	        "for some operations\n");
809 	fprintf(stderr, "   -g                   grep-friendly event output, "
810 	        "one event per line\n");
811 	exit (EXIT_FAILURE);
812 }
813 
814 static void
815 zevstat_usage(char *progname)
816 {
817 	fprintf(stderr, "usage: %s [-v] <interval> [count]\n", progname);
818 	fprintf(stderr, "   -v   verbose, show counters for all event types\n");
819 	exit (EXIT_FAILURE);
820 }
821 
822 static void
823 zevreport_usage(char *progname)
824 {
825 	fprintf(stderr, "usage: %s <output base filename>\n", progname);
826 	exit (EXIT_FAILURE);
827 }
828 
829 static int
830 zev_add_queue(int fd, char *arg, int blocking)
831 {
832 	zev_ioctl_add_queue_t aq;
833 	int namelen;
834 
835 	namelen = strlen(arg);
836 	if (namelen > ZEV_MAX_QUEUE_NAME_LEN) {
837 		fprintf(stderr, "queue name too long: %s\n", arg);
838 		return (EXIT_FAILURE);
839 	}
840 
841 	aq.zev_namelen = namelen;
842 	strcpy(aq.zev_name, arg);
843 	aq.zev_flags = ZEV_FL_PERSISTENT | ZEV_FL_INITIALLY_EMPTY;
844 	if (blocking) {
845 		aq.zev_flags |= ZEV_FL_BLOCK_WHILE_QUEUE_FULL;
846 		aq.zev_max_queue_len = ZEV_MAX_QUEUE_LEN;
847 	} else {
848 		aq.zev_max_queue_len = (1024 * 1024);
849 	}
850 
851 	if (ioctl(fd, ZEV_IOC_ADD_QUEUE, &aq)) {
852 		perror("adding queue failed");
853 		return (EXIT_FAILURE);
854 	}
855 	return (0);
856 }
857 
858 static int
859 zev_remove_queue(int fd, char *arg)
860 {
861 	zev_ioctl_remove_queue_t aq;
862 	int namelen;
863 
864 	namelen = strlen(arg);
865 	if (namelen > ZEV_MAX_QUEUE_NAME_LEN) {
866 		fprintf(stderr, "queue name too long: %s\n", arg);
867 		return (EXIT_FAILURE);
868 	}
869 
870 	aq.zev_queue_name.zev_namelen = namelen;
871 	strcpy(aq.zev_queue_name.zev_name, arg);
872 
873 	if (ioctl(fd, ZEV_IOC_REMOVE_QUEUE, &aq)) {
874 		perror("removing queue failed");
875 		return (EXIT_FAILURE);
876 	}
877 	return (0);
878 }
879 
880 static int
881 zev_set_global_max_queue_len(int fd, char *arg)
882 {
883 	uint64_t maxqueuelen;
884 
885 	if (!arg) {
886 		fprintf(stderr, "missing queue length parameter\n");
887 		return (EXIT_FAILURE);
888 	}
889 
890 	errno = 0;
891 	maxqueuelen = strtol(arg, (char **)NULL, 10);
892 	if (errno) {
893 		fprintf(stderr, "invalid queue length parameter: %s\n", arg);
894 		return (EXIT_FAILURE);
895 	}
896 	if (ioctl(fd, ZEV_IOC_SET_MAX_QUEUE_LEN, &maxqueuelen)) {
897 		perror("setting max queue length failed");
898 		return (EXIT_FAILURE);
899 	}
900 	return (0);
901 }
902 
903 static int
904 zev_mute_unmute_impl(int fd, char *poolname, int mute)
905 {
906 	zev_ioctl_poolarg_t pa;
907 	int len;
908 	int op = mute ? ZEV_IOC_MUTE_POOL : ZEV_IOC_UNMUTE_POOL;
909 	len = strlen(poolname);
910 	if (len <= 0 || len >= sizeof(pa.zev_poolname)) {
911 		fprintf(stderr, "invalid poolname: %s\n", poolname);
912 		return (EXIT_FAILURE);
913 	}
914 	strcpy(pa.zev_poolname, poolname);
915 	pa.zev_poolname_len = len;
916 	if (ioctl(fd, op, &pa)) {
917 		perror("muting pool data failed");
918 		return (EXIT_FAILURE);
919 	}
920 	return (0);
921 }
922 
923 int
924 zev_mute_pool(int fd, char *poolname)
925 {
926 	return zev_mute_unmute_impl(fd, poolname, 1);
927 }
928 
929 int
930 zev_unmute_pool(int fd, char *poolname)
931 {
932 	return zev_mute_unmute_impl(fd, poolname, 0);
933 }
934 
935 static int
936 zev_debug_info(int fd)
937 {
938 	zev_ioctl_debug_info_t di;
939 
940 	if (ioctl(fd, ZEV_IOC_GET_DEBUG_INFO, &di)) {
941 		perror("getting zev debug info failed");
942 		return (EXIT_FAILURE);
943 	}
944 
945 	printf("memory allocated: %llu bytes\n", di.zev_memory_allocated);
946 	printf("checksum cache size: %llu\n", di.zev_chksum_cache_size);
947 	printf("checksum cache hits: %llu\n", di.zev_chksum_cache_hits);
948 	printf("checksum cache misses: %llu\n", di.zev_chksum_cache_misses);
949 	return 0;
950 }
951 
952 static int
953 zev_mark(int fd, char *arg)
954 {
955 	zev_ioctl_mark_t *mark;
956 	uint64_t guid;
957 	int len;
958 	char *p;
959 
960 	p = strchr(arg, ':');
961 	if (!p) {
962 		fprintf(stderr, "expected value is <guid>:<payload>, "
963 		        "e.g. '123:hello'\n");
964 		exit (EXIT_FAILURE);
965 	}
966 	*p = '\n';
967 	p++;
968 
969 	errno = 0;
970 	guid = strtoll(arg, (char **)NULL, 10);
971 	if (errno) {
972 		fprintf(stderr, "guid must be a number.\n");
973 		exit (EXIT_FAILURE);
974 	}
975 
976 	len = strlen(p);
977 
978 	mark = malloc(sizeof(*mark) + len + 1);
979 	if (!mark) {
980 		fprintf(stderr, "can't allocate mark structure: %s\n",
981 		        strerror(errno));
982 		exit (EXIT_FAILURE);
983 	}
984 	mark->zev_guid = guid;
985 	mark->zev_mark_id = 0;
986 	mark->zev_payload_len = len;
987 	strcpy(ZEV_PAYLOAD(mark), p);
988 
989 	if (ioctl(fd, ZEV_IOC_MARK, mark)) {
990 		perror("queueing mark failed");
991 		return (EXIT_FAILURE);
992 	}
993 
994 	printf("mark id: %lu\n", mark->zev_mark_id);
995 	return (0);
996 }
997 
998 static int
999 zev_queue_blocking(int fd, char *arg, int block)
1000 {
1001 	zev_ioctl_get_queue_properties_t gqp;
1002 
1003 	gqp.zev_queue_name.zev_namelen = strlen(arg);
1004 	if (gqp.zev_queue_name.zev_namelen > ZEV_MAX_QUEUE_NAME_LEN) {
1005 		fprintf(stderr, "queue name too long.\n");
1006 		return EXIT_FAILURE;
1007 	}
1008 	strcpy(gqp.zev_queue_name.zev_name, arg);
1009 
1010 	if (ioctl(fd, ZEV_IOC_GET_QUEUE_PROPERTIES, &gqp)) {
1011 		perror("getting queue properties failed");
1012 		return (EXIT_FAILURE);
1013 	}
1014 	if (block) {
1015 		gqp.zev_flags |= ZEV_FL_BLOCK_WHILE_QUEUE_FULL;
1016 	} else {
1017 		gqp.zev_flags &= ~ZEV_FL_BLOCK_WHILE_QUEUE_FULL;
1018 	}
1019 	if (ioctl(fd, ZEV_IOC_SET_QUEUE_PROPERTIES, &gqp)) {
1020 		perror("setting queue properties failed");
1021 		return (EXIT_FAILURE);
1022 	}
1023 	return (0);
1024 }
1025 
1026 static int
1027 zev_set_max_queue_len(int fd, char *arg, char *len)
1028 {
1029 	zev_ioctl_get_queue_properties_t gqp;
1030 
1031 	if (!len) {
1032 		fprintf(stderr, "queue size parameter missing.\n");
1033 		return EXIT_FAILURE;
1034 	}
1035 
1036 	gqp.zev_queue_name.zev_namelen = strlen(arg);
1037 	if (gqp.zev_queue_name.zev_namelen > ZEV_MAX_QUEUE_NAME_LEN) {
1038 		fprintf(stderr, "queue name too long.\n");
1039 		return EXIT_FAILURE;
1040 	}
1041 	strcpy(gqp.zev_queue_name.zev_name, arg);
1042 
1043 	if (ioctl(fd, ZEV_IOC_GET_QUEUE_PROPERTIES, &gqp)) {
1044 		perror("getting queue properties failed");
1045 		return (EXIT_FAILURE);
1046 	}
1047 	gqp.zev_max_queue_len = atol(len);
1048 	if (gqp.zev_max_queue_len == 0 && strcmp("0", len)) {
1049 		fprintf(stderr, "queue size parameter garbled.\n");
1050 		return (EXIT_FAILURE);
1051 	}
1052 	if (gqp.zev_max_queue_len > ZEV_MAX_QUEUE_LEN) {
1053 		fprintf(stderr, "queue size parameter out of bounds.\n");
1054 		return (EXIT_FAILURE);
1055 	}
1056 
1057 	if (ioctl(fd, ZEV_IOC_SET_QUEUE_PROPERTIES, &gqp)) {
1058 		perror("setting queue properties failed");
1059 		return (EXIT_FAILURE);
1060 	}
1061 	return (0);
1062 }
1063 
1064 static int
1065 zev_set_poll_wakeup_queue_len(int fd, char *arg, char *len)
1066 {
1067 	zev_ioctl_get_queue_properties_t gqp;
1068 
1069 	if (!len) {
1070 		fprintf(stderr, "poll throttle parameter missing.\n");
1071 		return EXIT_FAILURE;
1072 	}
1073 
1074 	gqp.zev_queue_name.zev_namelen = strlen(arg);
1075 	if (gqp.zev_queue_name.zev_namelen > ZEV_MAX_QUEUE_NAME_LEN) {
1076 		fprintf(stderr, "queue name too long.\n");
1077 		return EXIT_FAILURE;
1078 	}
1079 	strcpy(gqp.zev_queue_name.zev_name, arg);
1080 
1081 	if (ioctl(fd, ZEV_IOC_GET_QUEUE_PROPERTIES, &gqp)) {
1082 		perror("getting queue properties failed");
1083 		return (EXIT_FAILURE);
1084 	}
1085 	gqp.zev_poll_wakeup_threshold = atol(len);
1086 	if (gqp.zev_poll_wakeup_threshold == 0 && strcmp("0", len)) {
1087 		fprintf(stderr, "poll throttle parameter garbled.\n");
1088 		return (EXIT_FAILURE);
1089 	}
1090 	if (gqp.zev_poll_wakeup_threshold > ZEV_MAX_POLL_WAKEUP_QUEUE_LEN) {
1091 		fprintf(stderr, "poll throttle parameter out of bounds.\n");
1092 		return (EXIT_FAILURE);
1093 	}
1094 
1095 	if (ioctl(fd, ZEV_IOC_SET_QUEUE_PROPERTIES, &gqp)) {
1096 		perror("setting queue properties failed");
1097 		return (EXIT_FAILURE);
1098 	}
1099 	return (0);
1100 }
1101 
1102 static int
1103 zev_queue_properties(int fd, char *arg)
1104 {
1105 	zev_ioctl_get_queue_properties_t gqp;
1106 
1107 	gqp.zev_queue_name.zev_namelen = strlen(arg);
1108 	if (gqp.zev_queue_name.zev_namelen > ZEV_MAX_QUEUE_NAME_LEN) {
1109 		fprintf(stderr, "queue name too long.\n");
1110 		return EXIT_FAILURE;
1111 	}
1112 	strcpy(gqp.zev_queue_name.zev_name, arg);
1113 
1114 	if (ioctl(fd, ZEV_IOC_GET_QUEUE_PROPERTIES, &gqp)) {
1115 		perror("getting queue properties failed");
1116 		return (EXIT_FAILURE);
1117 	}
1118 
1119 	printf("queue        : %s\n", arg);
1120 	printf("max size     : %" PRIu64 "\n", gqp.zev_max_queue_len);
1121 	printf("poll throttle: %" PRIu64 "\n", gqp.zev_poll_wakeup_threshold);
1122 	printf("persistent   : %s\n",
1123 		gqp.zev_flags & ZEV_FL_PERSISTENT ? "yes" : "no");
1124 	printf("blocking     : %s\n",
1125 		gqp.zev_flags & ZEV_FL_BLOCK_WHILE_QUEUE_FULL ? "yes" : "no");
1126 
1127 	return (0);
1128 }
1129 
1130 static int
1131 zev_list_queues(int fd)
1132 {
1133 	zev_ioctl_get_queue_properties_t gqp;
1134 	zev_ioctl_get_queue_list_t gql;
1135 	zev_ioctl_get_queue_statistics_t gs;
1136 	uint64_t	i;
1137 	char		name[ZEV_MAX_QUEUE_NAME_LEN+1];
1138 
1139 	if (ioctl(fd, ZEV_IOC_GET_QUEUE_LIST, &gql)) {
1140 		perror("getting queue list failed");
1141 		return (EXIT_FAILURE);
1142 	}
1143 
1144 	printf("Name                                     Size       "
1145 	       "Max Size   Wakeup Per Block\n");
1146 
1147 	for (i=0; i<gql.zev_n_queues; i++) {
1148 		strncpy(name, gql.zev_queue_name[i].zev_name,
1149 		        ZEV_MAX_QUEUE_NAME_LEN);
1150 		name[gql.zev_queue_name[i].zev_namelen] = '\0';
1151 
1152 		memcpy(gqp.zev_queue_name.zev_name,
1153 		    gql.zev_queue_name[i].zev_name, ZEV_MAX_QUEUE_NAME_LEN);
1154 		gqp.zev_queue_name.zev_namelen =
1155 		    gql.zev_queue_name[i].zev_namelen;
1156 
1157 		if (ioctl(fd, ZEV_IOC_GET_QUEUE_PROPERTIES, &gqp)) {
1158 			if (errno == ENOENT)
1159 				continue;
1160 			perror("getting queue properties failed");
1161 			return (EXIT_FAILURE);
1162 		}
1163 
1164 		memcpy(gs.zev_queue_name.zev_name,
1165 		    gql.zev_queue_name[i].zev_name, ZEV_MAX_QUEUE_NAME_LEN);
1166 		gs.zev_queue_name.zev_namelen =
1167 		    gql.zev_queue_name[i].zev_namelen;
1168 
1169 		if (ioctl(fd, ZEV_IOC_GET_QUEUE_STATISTICS, &gs)) {
1170 			if (errno == ENOENT)
1171 				continue;
1172 			perror("getting statistics data failed");
1173 			return (EXIT_FAILURE);
1174 		}
1175 
1176 		printf("%-40s %-10" PRIu64 " %-10" PRIu64 " %-6" PRIu64
1177 		       " %-3s %-3s\n",
1178 			name,
1179 			gs.zev_statistics.zev_queue_len,
1180 			gqp.zev_max_queue_len,
1181 			gqp.zev_poll_wakeup_threshold,
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 void
1545 zev_sigint(int sig)
1546 {
1547 	fflush(stdout);
1548 }
1549 
1550 int
1551 main(int argc, char **argv)
1552 {
1553 	int fd;
1554 	int c;
1555 	extern char *optarg;
1556 	int create_tmp_queue = 1;
1557 	char buf[MAXPATHLEN];
1558 	int mode = 0;
1559 	char *arg = NULL;
1560 	char *arg2 = NULL;
1561 	char *p;
1562 
1563 	sigset(SIGINT, zev_sigint);
1564 
1565 	/* open device */
1566 	fd = open(zev_device, O_RDONLY);
1567 	if (fd < 0) {
1568 		perror("opening zev device failed");
1569 		return EXIT_FAILURE;
1570 	}
1571 
1572 	p = strrchr(argv[0], '/');
1573 	if (!p) {
1574 		p = argv[0];
1575 	} else {
1576 		p++;
1577 	}
1578 	if (!strcmp(p, "zevstat")) {
1579 		mode = MD_ZEVSTAT;
1580 		if (argc < 2)
1581 			zevstat_usage(argv[0]);
1582 		if (!strcmp(argv[1], "-v")) {
1583 			if (argc < 3)
1584 				zevstat_usage(argv[0]);
1585 			verbose++;
1586 			arg = argv[2];
1587 			arg2 = argv[3];
1588 		} else {
1589 			arg = argv[1];
1590 			arg2 = argv[2];
1591 		}
1592 		return zev_zevstat(fd, arg, arg2, NULL);
1593 	} else if(!strcmp(p, "zevreport")) {
1594 		mode = MD_ZEV_REPORT;
1595 		if (argc != 2)
1596 			zevreport_usage(argv[0]);
1597 		return zev_report(fd, argv[1]);
1598 	}
1599 
1600 	while ((c = getopt(argc, argv,
1601 	   "gvspc:d:Dlk:L:q:Q:t:m:M:a:A:r:P:b:B:T:R:f:h?")) != -1) {
1602 		switch(c) {
1603 		case 'g':
1604 			grep_friendly++;
1605 			verbose++;
1606 			break;
1607 		case 'v':
1608 			verbose++;
1609 			break;
1610 		case 's':
1611 			mode = MD_STATISTICS;
1612 			break;
1613 		case 'p':
1614 			mode = MD_POLL_EVENTS;
1615 			break;
1616 		case 'c':
1617 			mode = MD_CHECKSUMS;
1618 			arg = optarg;
1619 			break;
1620 		case 'D':
1621 			mode = MD_DEBUG_INFO;
1622 			break;
1623 		case 'd':
1624 			close(fd);
1625 			zev_device = optarg;
1626 			fd = open(zev_device, O_RDONLY);
1627 			if (fd < 0) {
1628 				perror("opening zev device failed");
1629 				return EXIT_FAILURE;
1630 			}
1631 			create_tmp_queue = 0;
1632 			break;
1633 		case 'q':
1634 			snprintf(buf, sizeof(buf),
1635 				 "/devices/pseudo/zev@0:%s", optarg);
1636 			close(fd);
1637 			zev_device = buf;
1638 			fd = open(zev_device, O_RDONLY);
1639 			if (fd < 0) {
1640 				perror("opening zev device failed");
1641 				return EXIT_FAILURE;
1642 			}
1643 			create_tmp_queue = 0;
1644 			break;
1645 		case 'f':
1646 			fd = open(optarg, O_RDONLY);
1647 			if (fd < 0) {
1648 				perror("opening spool file failed");
1649 				return EXIT_FAILURE;
1650 			}
1651 			mode = MD_DUMP_SPOOL;
1652 			break;
1653 		case 'l':
1654 			mode = MD_LIST_QUEUES;
1655 			break;
1656 		case 'Q':
1657 			mode = MD_SET_GLOBAL_MAX_QUEUE_LEN;
1658 			arg = optarg;
1659 			break;
1660 		case 'L':
1661 			mode = MD_SET_MAX_QUEUE_LEN;
1662 			arg = optarg;
1663 			arg2 = argv[optind];
1664 			break;
1665 		case 'T':
1666 			mode = MD_ZEVSTAT;
1667 			arg = optarg;
1668 			arg2 = argv[optind];
1669 			break;
1670 		case 'R':
1671 			mode = MD_ZEV_REPORT;
1672 			arg = optarg;
1673 			break;
1674 		case 't':
1675 			mode = MD_SET_POLL_WAKEUP_QUEUE_LEN;
1676 			arg = optarg;
1677 			arg2 = argv[optind];
1678 			break;
1679 		case 'm':
1680 			mode = MD_MUTE_POOL;
1681 			arg = optarg;
1682 			break;
1683 		case 'M':
1684 			mode = MD_UNMUTE_POOL;
1685 			arg = optarg;
1686 			break;
1687 		case 'k':
1688 			mode = MD_MARK;
1689 			arg = optarg;
1690 			break;
1691 		case 'a':
1692 			mode = MD_ADD_QUEUE;
1693 			arg = optarg;
1694 			break;
1695 		case 'A':
1696 			mode = MD_ADD_BLOCKING_QUEUE;
1697 			arg = optarg;
1698 			break;
1699 		case 'r':
1700 			mode = MD_REMOVE_QUEUE;
1701 			arg = optarg;
1702 			break;
1703 		case 'b':
1704 			mode = MD_QUEUE_BLOCKING;
1705 			arg = optarg;
1706 			break;
1707 		case 'B':
1708 			mode = MD_QUEUE_NONBLOCKING;
1709 			arg = optarg;
1710 			break;
1711 		case 'P':
1712 			mode = MD_QUEUE_PROPERTIES;
1713 			arg = optarg;
1714 			break;
1715 		case 'h':
1716 		case '?':
1717 		default:
1718 			usage(argv[0]);
1719 		}
1720 	}
1721 
1722 	switch (mode) {
1723 	case MD_STATISTICS:
1724 		return zev_statistics(fd);
1725 	case MD_POLL_EVENTS:
1726 		return zev_poll_events(fd, create_tmp_queue);
1727 	case MD_DUMP_SPOOL:
1728 		return zev_dump_spool(fd);
1729 	case MD_CHECKSUMS:
1730 		return zev_checksum(fd, arg);
1731 	case MD_DEBUG_INFO:
1732 		return zev_debug_info(fd);
1733 	case MD_LIST_QUEUES:
1734 		return zev_list_queues(fd);
1735 	case MD_SET_GLOBAL_MAX_QUEUE_LEN:
1736 		return zev_set_global_max_queue_len(fd, arg);
1737 	case MD_SET_MAX_QUEUE_LEN:
1738 		return zev_set_max_queue_len(fd, arg, arg2);
1739 	case MD_SET_POLL_WAKEUP_QUEUE_LEN:
1740 		return zev_set_poll_wakeup_queue_len(fd, arg, arg2);
1741 	case MD_ZEVSTAT:
1742 		return zev_zevstat(fd, arg, arg2, NULL);
1743 	case MD_ZEV_REPORT:
1744 		return zev_report(fd, arg);
1745 	case MD_MUTE_POOL:
1746 		return zev_mute_pool(fd, arg);
1747 	case MD_UNMUTE_POOL:
1748 		return zev_unmute_pool(fd, arg);
1749 	case MD_MARK:
1750 		return zev_mark(fd, arg);
1751 	case MD_ADD_QUEUE:
1752 		return zev_add_queue(fd, arg, 0);
1753 	case MD_ADD_BLOCKING_QUEUE:
1754 		return zev_add_queue(fd, arg, 1);
1755 	case MD_REMOVE_QUEUE:
1756 		return zev_remove_queue(fd, arg);
1757 	case MD_QUEUE_BLOCKING:
1758 		return zev_queue_blocking(fd, arg, 0);
1759 	case MD_QUEUE_NONBLOCKING:
1760 		return zev_queue_blocking(fd, arg, 1);
1761 	case MD_QUEUE_PROPERTIES:
1762 		return zev_queue_properties(fd, arg);
1763 	default:
1764 		close(fd);
1765 		usage(argv[0]);
1766 		return EXIT_FAILURE;
1767 	};
1768 }
1769 
1770