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