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