xref: /titanic_41/usr/src/cmd/zevadm/zevadm.c (revision 182c91496a82a606d31c680cedf9351196cc76e6)
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 
11 #define OP_STATISTICS		(1 << 0)
12 #define OP_POLL_EVENTS		(1 << 1)
13 #define OP_MUTE_POOL		(1 << 2)
14 #define OP_UNMUTE_POOL		(1 << 3)
15 #define OP_SET_MAX_QUEUE_LEN	(1 << 4)
16 
17 #define ZEV_DEVICE "/devices/pseudo/zev@0:zev"
18 
19 static char *zev_device = ZEV_DEVICE;
20 
21 static char *zev_op_name[] = {
22 	"ZEV_OP_ERROR",
23 	"ZEV_OP_MARK",
24 	"ZEV_OP_ZFS_MOUNT",
25 	"ZEV_OP_ZFS_UMOUNT",
26 	"ZEV_OP_ZVOL_WRITE",
27 	"ZEV_OP_ZVOL_TRUNCATE",
28 	"ZEV_OP_ZNODE_CLOSE_AFTER_UPDATE",
29 	"ZEV_OP_ZNODE_CREATE",
30 	"ZEV_OP_ZNODE_MKDIR",
31 	"ZEV_OP_ZNODE_MAKE_XATTR_DIR",
32 	"ZEV_OP_ZNODE_REMOVE",
33 	"ZEV_OP_ZNODE_RMDIR",
34 	"ZEV_OP_ZNODE_LINK",
35 	"ZEV_OP_ZNODE_SYMLINK",
36 	"ZEV_OP_ZNODE_RENAME",
37 	"ZEV_OP_ZNODE_WRITE",
38 	"ZEV_OP_ZNODE_TRUNCATE",
39 	"ZEV_OP_ZNODE_SETATTR",
40 	"ZEV_OP_ZNODE_ACL",
41 	NULL
42 };
43 
44 static void
45 zev_statistics(int fd)
46 {
47 	zev_statistics_t zs;
48 	if (ioctl(fd, ZEV_IOC_GET_STATISTICS, &zs)) {
49 		perror("getting statistics data failed");
50 		exit (EXIT_FAILURE);
51 	}
52 	printf("ZEV module state:\n");
53 
54 	printf("    queue length in bytes   : %lu\n", zs.zev_queue_len);
55 	printf("    queue length limit      : %lu\n", zs.zev_max_queue_len);
56 	printf("    poll wakeup throttle    : %lu\n\n",
57 	    zs.zev_poll_wakeup_queue_len);
58 	printf("    bytes read from device  : %lu\n", zs.zev_bytes_read);
59 	printf("    module internal errors  : %lu\n\n", zs.zev_cnt_errors);
60 
61 	printf("ZFS event statistics:\n");
62 
63 	printf("    total ZFS events        : %lu\n", zs.zev_cnt_total_events);
64 	printf("    ZFS mount               : %lu\n", zs.zev_cnt_zfs_mount);
65 	printf("    ZFS umount              : %lu\n", zs.zev_cnt_zfs_umount);
66 	printf("    ZVOL write              : %lu\n", zs.zev_cnt_zvol_write);
67 	printf("    ZVOL truncate           : %lu\n", zs.zev_cnt_zvol_truncate);
68 	printf("    ZNODE close after update: %lu\n",
69 	    zs.zev_cnt_znode_close_after_update);
70 	printf("    ZNODE create            : %lu\n", zs.zev_cnt_znode_create);
71 	printf("    ZNODE remove            : %lu\n", zs.zev_cnt_znode_remove);
72 	printf("    ZNODE link              : %lu\n", zs.zev_cnt_znode_link);
73 	printf("    ZNODE symlink           : %lu\n", zs.zev_cnt_znode_symlink);
74 	printf("    ZNODE rename            : %lu\n", zs.zev_cnt_znode_rename);
75 	printf("    ZNODE write             : %lu\n", zs.zev_cnt_znode_write);
76 	printf("    ZNODE truncate          : %lu\n",
77 	    zs.zev_cnt_znode_truncate);
78 	printf("    ZNODE setattr           : %lu\n", zs.zev_cnt_znode_setattr);
79 	printf("    ZNODE acl               : %lu\n", zs.zev_cnt_znode_acl);
80 }
81 
82 static void
83 zev_print_error(char *buf)
84 {
85 	zev_error_t *rec = (zev_error_t *)buf;
86 	time_t op_time = rec->op_time;
87 	char *ct = ctime(&op_time); ct[24] = '\0';
88 
89 	printf("%s %s: failed_op=%s msg=%s\n",
90 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
91 	       zev_op_name[rec->failed_op - ZEV_OP_MIN], ZEV_ERRSTR(rec));
92 }
93 
94 static void
95 zev_print_mark(char *buf)
96 {
97 	zev_mark_t *rec = (zev_mark_t *)buf;
98 	time_t op_time = rec->op_time;
99 	char *ct = ctime(&op_time); ct[24] = '\0';
100 
101 	printf("%s %s: guid=%llu mark_id=%lld payload_len=%ld\n",
102 	       ct, zev_op_name[rec->op - ZEV_OP_MIN], rec->guid, rec->mark_id,
103 	       rec->payload_len);
104 }
105 
106 static void
107 zev_print_zfs_mount(char *buf)
108 {
109 	zev_zfs_mount_t *rec = (zev_zfs_mount_t *)buf;
110 	time_t op_time = rec->op_time;
111 	char *ct = ctime(&op_time); ct[24] = '\0';
112 
113 	printf("%s %s: guid=%llu remount=%s dataset='%s' mountpoint='%s'\n",
114 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
115 	       rec->guid,
116 	       rec->remount ? "true" : "false",
117 	       ZEV_DATASET(rec),
118 	       ZEV_MOUNTPOINT(rec));
119 }
120 
121 static void
122 zev_print_zfs_umount(char *buf)
123 {
124 	zev_zfs_umount_t *rec = (zev_zfs_umount_t *)buf;
125 	time_t op_time = rec->op_time;
126 	char *ct = ctime(&op_time); ct[24] = '\0';
127 
128 	printf("%s %s: guid=%llu\n",
129 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
130 	       rec->guid);
131 }
132 
133 static void
134 zev_print_zvol_truncate(char *buf)
135 {
136 	zev_zvol_truncate_t *rec = (zev_zvol_truncate_t *)buf;
137 	time_t op_time = rec->op_time;
138 	char *ct = ctime(&op_time); ct[24] = '\0';
139 
140 	printf("%s %s: guid=%llu offset=%llu length=%llu\n",
141 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
142 	       rec->guid,
143 	       rec->offset,
144 	       rec->length);
145 }
146 
147 static void
148 zev_print_zvol_write(char *buf)
149 {
150 	zev_print_zvol_truncate(buf);
151 }
152 
153 static void
154 zev_print_znode_close_after_update(char *buf)
155 {
156 	zev_znode_close_after_update_t *rec =
157 	    (zev_znode_close_after_update_t *)buf;
158 	time_t op_time = rec->op_time;
159 	char *ct = ctime(&op_time); ct[24] = '\0';
160 
161 	printf("%s %s: guid=%llu file=%llu.%llu\n",
162 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
163 	       rec->guid,
164 	       rec->file.ino, rec->file.gen);
165 }
166 
167 static void
168 zev_print_znode_create(char *buf)
169 {
170 	zev_znode_create_t *rec = (zev_znode_create_t *)buf;
171 	time_t op_time = rec->op_time;
172 	char *ct = ctime(&op_time); ct[24] = '\0';
173 
174 	printf("%s %s: guid=%llu parent=%llu.%llu file=%llu.%llu name='%s'\n",
175 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
176 	       rec->guid,
177 	       rec->parent.ino, rec->parent.gen,
178 	       rec->file.ino, rec->file.gen,
179 	       ZEV_NAME(rec));
180 }
181 
182 static void
183 zev_print_znode_mkdir(char *buf)
184 {
185 	zev_print_znode_create(buf);
186 }
187 
188 static void
189 zev_print_znode_make_xattr_dir(char *buf)
190 {
191 	zev_print_znode_create(buf);
192 }
193 
194 static void
195 zev_print_znode_remove(char *buf)
196 {
197 	zev_znode_remove_t *rec = (zev_znode_remove_t *)buf;
198 	time_t op_time = rec->op_time;
199 	char *ct = ctime(&op_time); ct[24] = '\0';
200 
201 	printf("%s %s: guid=%llu parent=%llu.%llu name='%s'\n",
202 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
203 	       rec->guid,
204 	       rec->parent.ino, rec->parent.gen,
205 	       ZEV_NAME(rec));
206 }
207 
208 static void
209 zev_print_znode_rmdir(char *buf)
210 {
211 	zev_print_znode_remove(buf);
212 }
213 
214 static void
215 zev_print_znode_link(char *buf)
216 {
217 	zev_znode_link_t *rec = (zev_znode_link_t *)buf;
218 	time_t op_time = rec->op_time;
219 	char *ct = ctime(&op_time); ct[24] = '\0';
220 
221 	printf("%s %s: parent=%llu.%llu file=%llu.%llu name='%s'\n",
222 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
223 	       rec->parent.ino, rec->parent.gen,
224 	       rec->file.ino, rec->file.gen,
225 	       ZEV_NAME(rec));
226 	printf("links: %d\n", rec->file.links);
227 }
228 
229 static void
230 zev_print_znode_symlink(char *buf)
231 {
232 	zev_znode_symlink_t *rec = (zev_znode_symlink_t *)buf;
233 	time_t op_time = rec->op_time;
234 	char *ct = ctime(&op_time); ct[24] = '\0';
235 
236 	printf("%s %s: parent=%llu.%llu file=%llu.%llu name='%s' link='%s'\n",
237 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
238 	       rec->parent.ino, rec->parent.gen,
239 	       rec->file.ino, rec->file.gen,
240 	       ZEV_NAME(rec),
241 	       ZEV_LINK(rec));
242 }
243 
244 static void
245 zev_print_znode_rename(char *buf)
246 {
247 	zev_znode_rename_t *rec = (zev_znode_rename_t *)buf;
248 	time_t op_time = rec->op_time;
249 	char *ct = ctime(&op_time); ct[24] = '\0';
250 
251 	printf("%s %s: srcdir=%llu.%llu dstdir=%llu.%llu file=%llu.%llu "
252 	       "srcname='%s' dstname='%s'\n",
253 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
254 	       rec->srcdir.ino, rec->srcdir.gen,
255 	       rec->dstdir.ino, rec->dstdir.gen,
256 	       rec->file.ino, rec->file.gen,
257 	       ZEV_SRCNAME(rec),
258 	       ZEV_DSTNAME(rec));
259 }
260 
261 static void
262 zev_print_znode_write(char *buf)
263 {
264 	zev_znode_write_t *rec = (zev_znode_write_t *)buf;
265 	time_t op_time = rec->op_time;
266 	char *ct = ctime(&op_time); ct[24] = '\0';
267 
268 	printf("%s %s: file=%llu.%llu offset=%llu length=%llu\n",
269 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
270 	       rec->file.ino, rec->file.gen,
271 	       rec->offset, rec->length);
272 }
273 
274 static void
275 zev_print_znode_truncate(char *buf)
276 {
277 	zev_print_znode_write(buf);
278 }
279 
280 static void
281 zev_print_znode_setattr(char *buf)
282 {
283 	zev_znode_setattr_t *rec = (zev_znode_setattr_t *)buf;
284 	time_t op_time = rec->op_time;
285 	char *ct = ctime(&op_time); ct[24] = '\0';
286 
287 	printf("%s %s: file=%llu.%llu\n",
288 	       ct, zev_op_name[rec->op - ZEV_OP_MIN],
289 	       rec->file.ino, rec->file.gen);
290 }
291 
292 static void
293 zev_print_znode_acl(char *buf)
294 {
295 	zev_print_znode_setattr(buf);
296 }
297 
298 static void
299 zev_print_event(char *buf, int len)
300 {
301 	int record_len;
302 	int op;
303 
304 	record_len = *(uint32_t *)buf;
305 	if (record_len != len) {
306 		fprintf(stderr, "record length mismatch: got %d, expected %d\n",
307 		        record_len, len);
308 		exit(1);
309 	}
310 	op = *((uint32_t *)buf + 1);
311 	if (op < ZEV_OP_MIN || op > ZEV_OP_MAX) {
312 		fprintf(stderr, "unknown op code: %d\n", op);
313 		exit(1);
314 	}
315 	switch (op) {
316 	case ZEV_OP_ERROR:
317 		zev_print_error(buf);
318 		break;
319 	case ZEV_OP_MARK:
320 		zev_print_mark(buf);
321 		break;
322 	case ZEV_OP_ZFS_MOUNT:
323 		zev_print_zfs_mount(buf);
324 		break;
325 	case ZEV_OP_ZFS_UMOUNT:
326 		zev_print_zfs_umount(buf);
327 		break;
328 	case ZEV_OP_ZVOL_TRUNCATE:
329 		zev_print_zvol_truncate(buf);
330 		break;
331 	case ZEV_OP_ZVOL_WRITE:
332 		zev_print_zvol_write(buf);
333 		break;
334 	case ZEV_OP_ZNODE_CLOSE_AFTER_UPDATE:
335 		zev_print_znode_close_after_update(buf);
336 		break;
337 	case ZEV_OP_ZNODE_CREATE:
338 		zev_print_znode_create(buf);
339 		break;
340 	case ZEV_OP_ZNODE_MKDIR:
341 		zev_print_znode_mkdir(buf);
342 		break;
343 	case ZEV_OP_ZNODE_MAKE_XATTR_DIR:
344 		zev_print_znode_make_xattr_dir(buf);
345 		break;
346 	case ZEV_OP_ZNODE_REMOVE:
347 		zev_print_znode_remove(buf);
348 		break;
349 	case ZEV_OP_ZNODE_RMDIR:
350 		zev_print_znode_rmdir(buf);
351 		break;
352 	case ZEV_OP_ZNODE_LINK:
353 		zev_print_znode_link(buf);
354 		break;
355 	case ZEV_OP_ZNODE_SYMLINK:
356 		zev_print_znode_symlink(buf);
357 		break;
358 	case ZEV_OP_ZNODE_RENAME:
359 		zev_print_znode_rename(buf);
360 		break;
361 	case ZEV_OP_ZNODE_WRITE:
362 		zev_print_znode_write(buf);
363 		break;
364 	case ZEV_OP_ZNODE_TRUNCATE:
365 		zev_print_znode_truncate(buf);
366 		break;
367 	case ZEV_OP_ZNODE_SETATTR:
368 		zev_print_znode_setattr(buf);
369 		break;
370 	case ZEV_OP_ZNODE_ACL:
371 		zev_print_znode_acl(buf);
372 		break;
373 	default:
374 		fprintf(stderr, "unhandled op code: %d\n", op);
375 		exit(1);
376 	}
377 }
378 
379 static void
380 zev_poll_events(int fd)
381 {
382 	struct pollfd pfd[1];
383 	int ret;
384 	char buf[4096];
385 	zev_event_t	*ev;
386 	int off = 0;
387 	while (1) {
388 		pfd[0].fd = fd;
389 		pfd[0].events = POLLIN;
390 		ret = poll(pfd, 1, 1000);
391 		if (ret < 0) {
392 			perror("poll failed");
393 			exit(EXIT_FAILURE);
394 		}
395 		if (!(pfd[0].revents & POLLIN))
396 			continue;
397 		/* data available */
398 		ret = read(fd, buf, sizeof(buf));
399 		if (ret < 0) {
400 			perror("read failed");
401 			exit(EXIT_FAILURE);
402 		}
403 		if (ret == 0)
404 			continue;
405 		while (ret > off) {
406 			ev = (zev_event_t *)(buf + off);
407 			zev_print_event(buf + off, ev->header.record_len);
408 			off += ev->header.record_len;
409 		}
410 		off = 0;
411 	}
412 	return;
413 }
414 
415 static void
416 usage(char *progname)
417 {
418 	fprintf(stderr, "usage: %s [-s] [-d <dev>]\n", progname);
419 	fprintf(stderr, "   -s         show zev statistics\n");
420 	fprintf(stderr, "   -p         poll for ZFS events\n");
421 	fprintf(stderr, "   -q <bytes> set maximum event queue length\n");
422 	fprintf(stderr, "   -t <bytes> set queue length poll throttle\n");
423 	fprintf(stderr, "   -m <pool>  mute pool, no events for this pool\n");
424 	fprintf(stderr, "   -M <pool>  unmute pool\n");
425 	fprintf(stderr, "   -d <dev>   device file to use. default is '%s'\n",
426 	    ZEV_DEVICE);
427 	fprintf(stderr, "   -k <guid>:<payload>   queue mark event\n");
428 	exit (EXIT_FAILURE);
429 }
430 
431 static int
432 zev_set_max_queue_len(int fd, char *optarg)
433 {
434 	uint64_t maxqueuelen;
435 
436 	errno = 0;
437 	maxqueuelen = strtol(optarg, (char **)NULL, 10);
438 	if (errno) {
439 		fprintf(stderr, "invalid queue length parameter: %s\n", optarg);
440 		return (EXIT_FAILURE);
441 	}
442 	if (ioctl(fd, ZEV_IOC_SET_MAX_QUEUE_LEN, &maxqueuelen)) {
443 		perror("setting max queue length failed");
444 		return (EXIT_FAILURE);
445 	}
446 	return (0);
447 }
448 
449 static int
450 zev_set_poll_wakeup_queue_len(int fd, char *optarg)
451 {
452 	uint64_t queuelen;
453 
454 	errno = 0;
455 	queuelen = strtol(optarg, (char **)NULL, 10);
456 	if (errno) {
457 		fprintf(stderr, "invalid queue length parameter: %s\n", optarg);
458 		return (EXIT_FAILURE);
459 	}
460 	if (ioctl(fd, ZEV_IOC_SET_POLL_WAKEUP_QUEUE_LEN, &queuelen)) {
461 		perror("setting poll wakeup queue length failed");
462 		return (EXIT_FAILURE);
463 	}
464 	return (0);
465 }
466 
467 static int
468 zev_mute_unmute_impl(int fd, char *poolname, int mute)
469 {
470 	zev_ioctl_poolarg_t pa;
471 	int len;
472 	int op = mute ? ZEV_IOC_MUTE_POOL : ZEV_IOC_UNMUTE_POOL;
473 	len = strlen(poolname);
474 	if (len <= 0 || len >= sizeof(pa.zev_poolname)) {
475 		fprintf(stderr, "invalid poolname: %s\n", poolname);
476 		return (EXIT_FAILURE);
477 	}
478 	strcpy(pa.zev_poolname, poolname);
479 	pa.zev_poolname_len = len;
480 	if (ioctl(fd, op, &pa)) {
481 		perror("muting pool data failed");
482 		return (EXIT_FAILURE);
483 	}
484 	return (0);
485 }
486 
487 int
488 zev_mute_pool(int fd, char *poolname)
489 {
490 	return zev_mute_unmute_impl(fd, poolname, 1);
491 }
492 
493 int
494 zev_unmute_pool(int fd, char *poolname)
495 {
496 	return zev_mute_unmute_impl(fd, poolname, 0);
497 }
498 
499 static int
500 zev_mark(int fd, char *arg)
501 {
502 	zev_ioctl_mark_t *mark;
503 	uint64_t guid;
504 	int len;
505 	char *p;
506 
507 	p = strchr(arg, ':');
508 	if (!p) {
509 		fprintf(stderr, "expected value is <guid>:<payload>, "
510 		        "e.g. '123:hello'\n");
511 		exit (EXIT_FAILURE);
512 	}
513 	*p = '\n';
514 	p++;
515 
516 	errno = 0;
517 	guid = strtoll(optarg, (char **)NULL, 10);
518 	if (errno) {
519 		fprintf(stderr, "guid must be a number.\n");
520 		exit (EXIT_FAILURE);
521 	}
522 
523 	len = strlen(p);
524 
525 	mark = malloc(sizeof(*mark) + len + 1);
526 	if (!mark) {
527 		fprintf(stderr, "can't allocate mark structure: %s\n",
528 		        strerror(errno));
529 		exit (EXIT_FAILURE);
530 	}
531 	mark->zev_guid = guid;
532 	mark->zev_mark_id = 0;
533 	mark->zev_payload_len = len;
534 	strcpy(ZEV_PAYLOAD(mark), p);
535 
536 	if (ioctl(fd, ZEV_IOC_MARK, mark)) {
537 		perror("queueing mark failed");
538 		return (EXIT_FAILURE);
539 	}
540 
541 	printf("mark id: %lu\n", mark->zev_mark_id);
542 	return (0);
543 }
544 
545 int
546 main(int argc, char **argv)
547 {
548 	int fd;
549 	int c;
550 	int ops = 0;
551 	extern char *optarg;
552 
553 	/* open device */
554 	fd = open(zev_device, O_RDONLY);
555 	if (fd < 0) {
556 		perror("opening zev device failed");
557 		return EXIT_FAILURE;
558 	}
559 	while ((c = getopt(argc, argv, "spdk:q:t:m:M:h?")) != -1) {
560 		switch(c) {
561 		case 's':
562 			ops |= OP_STATISTICS;
563 			break;
564 		case 'p':
565 			ops |= OP_POLL_EVENTS;
566 			break;
567 		case 'd':
568 			zev_device = optarg;
569 			break;
570 		case 'q':
571 			return zev_set_max_queue_len(fd, optarg);
572 		case 't':
573 			return zev_set_poll_wakeup_queue_len(fd, optarg);
574 		case 'm':
575 			return zev_mute_pool(fd, optarg);
576 		case 'M':
577 			return zev_unmute_pool(fd, optarg);
578 		case 'k':
579 			return zev_mark(fd, optarg);
580 		case 'h':
581 		case '?':
582 		default:
583 			usage(argv[0]);
584 		}
585 	}
586 	if (!ops)
587 		usage(argv[0]);
588 	if (ops & OP_STATISTICS)
589 		zev_statistics(fd);
590 	if (ops & OP_POLL_EVENTS)
591 		zev_poll_events(fd);
592 	close(fd);
593 	return EXIT_SUCCESS;
594 }
595 
596