xref: /titanic_41/usr/src/uts/common/sys/fs/zev.h (revision 2a796556e7d6dfb15a985d4d879c73b491b7ae11)
1 #ifndef __ZEV_H__
2 #define __ZEV_H__
3 
4 #include <sys/types.h>
5 #include <sys/param.h>
6 #include <sys/atomic.h>
7 #include <sys/sha1.h>
8 
9 #ifdef _KERNEL
10 #include <sys/dmu_objset.h>
11 #include <sys/dsl_dataset.h>
12 #include <sys/zfs_vfsops.h>
13 #include <sys/dsl_dir.h>
14 #include <sys/spa_impl.h>
15 #endif
16 
17 #define ZEV_MAX_QUEUE_NAME_LEN		40
18 #define ZEV_MAX_QUEUES			63
19 #define ZEV_CONTROL_DEVICE_NAME		"ctrl"
20 
21 /* global limit, no queue may grow larger than this. */
22 #define ZEV_MAX_QUEUE_LEN		(1 * 1024 * 1024 * 1024)
23 
24 /* Don't wake up poll()ing processes for every single message. */
25 #define ZEV_DEFAULT_POLL_WAKEUP_QUEUE_LEN	8192
26 #define ZEV_MAX_POLL_WAKEUP_QUEUE_LEN		65536
27 
28 #define ZEVIOC				  ('z' << 8)
29 #define ZEV_IOC_GET_GLOBAL_STATISTICS	  (ZEVIOC | 1)	/* get global stats */
30 #define ZEV_IOC_MUTE_POOL		  (ZEVIOC | 2)	/* no events for pool */
31 #define ZEV_IOC_UNMUTE_POOL		  (ZEVIOC | 3)	/* send pool events */
32 #define ZEV_IOC_SET_MAX_QUEUE_LEN	  (ZEVIOC | 4)	/* when to block ops */
33 #define ZEV_IOC_SET_POLL_WAKEUP_QUEUE_LEN (ZEVIOC | 5)	/* poll throttle */
34 #define ZEV_IOC_MARK			  (ZEVIOC | 6)	/* add mark to queue */
35 #define ZEV_IOC_GET_GEN			  (ZEVIOC | 7)	/* get generation no. */
36 #define	ZEV_IOC_ADD_QUEUE		  (ZEVIOC | 8)  /* create new queue */
37 #define	ZEV_IOC_REMOVE_QUEUE		  (ZEVIOC | 9)  /* delete queue */
38 #define ZEV_IOC_GET_QUEUE_PROPERTIES	  (ZEVIOC | 10)	/* get properties */
39 #define ZEV_IOC_SET_QUEUE_PROPERTIES	  (ZEVIOC | 11)	/* set properties */
40 #define ZEV_IOC_GET_QUEUE_STATISTICS	  (ZEVIOC | 12)	/* get queue stats */
41 #define ZEV_IOC_GET_DEBUG_INFO		  (ZEVIOC | 13)	/* get internal info */
42 #define ZEV_IOC_GET_QUEUE_LIST		  (ZEVIOC | 14)	/* get queue list */
43 #define ZEV_IOC_GET_FILE_SIGNATURES	  (ZEVIOC | 15)	/* get beaver sigs */
44 
45 #define ZEV_OP_MIN			 1
46 #define ZEV_OP_ERROR			 1
47 #define ZEV_OP_MARK			 2
48 #define	ZEV_OP_ZFS_MOUNT		 3
49 #define	ZEV_OP_ZFS_UMOUNT		 4
50 #define	ZEV_OP_ZVOL_WRITE		 5
51 #define	ZEV_OP_ZVOL_TRUNCATE		 6
52 #define	ZEV_OP_ZNODE_CLOSE_AFTER_UPDATE	 7
53 #define	ZEV_OP_ZNODE_CREATE		 8
54 #define	ZEV_OP_ZNODE_MKDIR		 9
55 #define	ZEV_OP_ZNODE_MAKE_XATTR_DIR	10
56 #define	ZEV_OP_ZNODE_REMOVE		11
57 #define	ZEV_OP_ZNODE_RMDIR		12
58 #define	ZEV_OP_ZNODE_LINK		13
59 #define	ZEV_OP_ZNODE_SYMLINK		14
60 #define	ZEV_OP_ZNODE_RENAME		15
61 #define	ZEV_OP_ZNODE_WRITE		16
62 #define	ZEV_OP_ZNODE_TRUNCATE		17
63 #define	ZEV_OP_ZNODE_SETATTR		18
64 #define	ZEV_OP_ZNODE_ACL		19
65 #define	ZEV_OP_MAX			19
66 
67 /* zev event flags */
68 #define	ZEV_FL_XATTR			0x0001
69 
70 /* zev queue flags */
71 #define ZEV_FL_BLOCK_WHILE_QUEUE_FULL	0x0001
72 #define ZEV_FL_PERSISTENT		0x0002
73 #define ZEV_FL_INITIALLY_EMPTY		0x0004
74 
75 /* checksum block sizes */
76 #define ZEV_L0_SIZE	4096
77 #define ZEV_L1_SIZE	(256 * ZEV_L0_SIZE)
78 
79 /* zfs event records (as they are represented through the character device) */
80 
81 #pragma pack(1)
82 typedef struct zev_inode_info_t {
83 	uint64_t	ino;
84 	uint64_t	gen;
85 	uint64_t	mtime;
86 	uint64_t	ctime;
87 	uint64_t	size;
88 	uint64_t	mode;
89 	uint64_t	links;
90 	uint32_t	type;
91 	uint32_t	flags;
92 } zev_inode_info_t;
93 
94 #define ZEV_ERRSTR(rec)		((char *)(rec + 1))
95 #define ZEV_DATASET(rec)	((char *)(rec + 1))
96 #define ZEV_MOUNTPOINT(rec)	(((char *)(rec + 1)) + rec->dataset_len + 1)
97 #define ZEV_NAME(rec)		((char *)(rec + 1))
98 #define ZEV_SRCNAME(rec)	((char *)(rec + 1))
99 #define ZEV_DSTNAME(rec)	(((char *)(rec + 1)) + rec->srcname_len + 1)
100 #define ZEV_LINK(rec)		(((char *)(rec + 1)) + rec->name_len + 1)
101 #define ZEV_PAYLOAD(rec)	((char *)(rec + 1))
102 #define ZEV_SIGNATURES(rec)	((char *)(rec + 1))
103 
104 #define ZEV_COMMON_FIELDS						\
105 	uint32_t		record_len;				\
106 	uint32_t		op;					\
107 	uint64_t		op_time;				\
108 	uint64_t		guid
109 
110 typedef struct zev_sig_t {
111 	uint16_t	level;
112 	uint16_t	padding1;
113 	uint32_t	padding2;
114 	uint64_t	block_offset;
115 	uint8_t		value[SHA1_DIGEST_LENGTH];  /* 20 bytes -> no padding */
116 } zev_sig_t;
117 
118 typedef struct zev_header_t {
119 	ZEV_COMMON_FIELDS;
120 } zev_header_t;
121 
122 typedef struct zev_error_t {
123 	ZEV_COMMON_FIELDS;
124 	uint32_t		failed_op;
125 	uint32_t		errstr_len;
126 	/* error string follows */
127 } zev_error_t;
128 
129 typedef struct zev_mark_t {
130 	ZEV_COMMON_FIELDS;
131 	uint64_t		mark_id;
132 	uint32_t		payload_len;
133 	uint32_t		padding;
134 	/* payload follows */
135 } zev_mark_t;
136 
137 typedef struct zev_zfs_mount_t {
138 	ZEV_COMMON_FIELDS;
139 	zev_inode_info_t	root;
140 	uint32_t		remount;
141 	uint32_t		dataset_len;
142 	uint32_t		mountpoint_len;
143 	uint32_t		padding;
144 	/* dataset follows */
145 	/* mountpoint follows */
146 } zev_zfs_mount_t;
147 
148 typedef struct zev_zfs_umount_t {
149 	ZEV_COMMON_FIELDS;
150 	zev_inode_info_t	covered;
151 } zev_zfs_umount_t;
152 
153 typedef struct zev_zvol_truncate_t {
154 	ZEV_COMMON_FIELDS;
155 	uint64_t		txg;
156 	uint64_t		offset;
157 	uint64_t		length;
158 	uint32_t		dataset_len;
159 	uint32_t		padding;
160 	/* dataset follows */
161 } zev_zvol_truncate_t;
162 
163 typedef struct zev_zvol_write_t {
164 	ZEV_COMMON_FIELDS;
165 	uint64_t		txg;
166 	uint64_t		offset;
167 	uint64_t		length;
168 	uint32_t		dataset_len;
169 	uint32_t		padding;
170 	/* dataset follows */
171 } zev_zvol_write_t;
172 
173 typedef struct zev_znode_close_after_update_t {
174 	ZEV_COMMON_FIELDS;
175 	zev_inode_info_t	file;
176 } zev_znode_close_after_update_t;
177 
178 typedef struct zev_znode_create_t {
179 	ZEV_COMMON_FIELDS;
180 	uint64_t		txg;
181 	zev_inode_info_t	file;
182 	zev_inode_info_t	parent;
183 	zev_sig_t		signature;
184 	uint32_t		name_len;
185 	uint32_t		padding;
186 	/* name follows */
187 } zev_znode_create_t;
188 
189 typedef struct zev_znode_create_t zev_znode_mkdir_t;
190 typedef struct zev_znode_create_t zev_znode_make_xattr_dir_t;
191 
192 typedef struct zev_znode_remove_t {
193 	ZEV_COMMON_FIELDS;
194 	uint64_t		txg;
195 	zev_inode_info_t	file;
196 	zev_inode_info_t	parent;
197 	uint32_t		name_len;
198 	uint32_t		padding;
199 	/* name follows */
200 } zev_znode_remove_t;
201 
202 typedef struct zev_znode_remove_t zev_znode_rmdir_t;
203 
204 typedef struct zev_znode_link_t {
205 	ZEV_COMMON_FIELDS;
206 	uint64_t		txg;
207 	zev_inode_info_t	parent;
208 	zev_inode_info_t	file;
209 	uint32_t		name_len;
210 	uint32_t		padding;
211 	/* new_name follows */
212 } zev_znode_link_t;
213 
214 typedef struct zev_znode_symlink_t {
215 	ZEV_COMMON_FIELDS;
216 	uint64_t		txg;
217 	zev_inode_info_t	parent;
218 	zev_inode_info_t	file;
219 	zev_sig_t		signature;
220 	uint32_t		name_len;
221 	uint32_t		link_len;
222 	/* name follows */
223 	/* link follows */
224 } zev_znode_symlink_t;
225 
226 typedef struct zev_znode_rename_t {
227 	ZEV_COMMON_FIELDS;
228 	uint64_t		txg;
229 	zev_inode_info_t	srcdir;
230 	zev_inode_info_t	dstdir;
231 	zev_inode_info_t	file;
232 	zev_inode_info_t	clobbered_file;
233 	uint32_t		srcname_len;
234 	uint32_t		dstname_len;
235 	/* srcname follows */
236 	/* dstname follows */
237 } zev_znode_rename_t;
238 
239 typedef struct zev_znode_write_t {
240 	ZEV_COMMON_FIELDS;
241 	uint64_t		txg;
242 	zev_inode_info_t	file;
243 	uint64_t		offset;
244 	uint64_t		length;
245 	uint64_t		signature_cnt;
246 	/* signatures follow */
247 } zev_znode_write_t;
248 
249 typedef struct zev_znode_truncate_t {
250 	ZEV_COMMON_FIELDS;
251 	uint64_t		txg;
252 	zev_inode_info_t	file;
253 	uint64_t		offset;
254 	uint64_t		length;
255 	uint64_t		signature_cnt;
256 	/* signatures follow */
257 } zev_znode_truncate_t;
258 
259 typedef struct zev_znode_setattr_t {
260 	ZEV_COMMON_FIELDS;
261 	uint64_t		txg;
262 	zev_inode_info_t	file;
263 } zev_znode_setattr_t;
264 
265 typedef struct zev_znode_acl_t {
266 	ZEV_COMMON_FIELDS;
267 	uint64_t		txg;
268 	zev_inode_info_t	file;
269 } zev_znode_acl_t;
270 
271 /* convenience helper definition */
272 typedef union {
273 	zev_header_t				header;
274 
275 	zev_error_t				error;
276 	zev_mark_t				mark;
277 	union {
278 		zev_zfs_mount_t			mount;
279 		zev_zfs_umount_t		umount;
280 	} zfs;
281 	union {
282 		zev_zvol_truncate_t		truncate;
283 		zev_zvol_write_t		write;
284 	} zvol;
285 	union {
286 		zev_znode_close_after_update_t	close;
287 		zev_znode_create_t		create;
288 		zev_znode_mkdir_t		mkdir;
289 		zev_znode_make_xattr_dir_t	mkxattrdir;
290 		zev_znode_remove_t		remove;
291 		zev_znode_rmdir_t		rmdir;
292 		zev_znode_link_t		link;
293 		zev_znode_symlink_t		symlink;
294 		zev_znode_rename_t		rename;
295 		zev_znode_write_t		write;
296 		zev_znode_truncate_t		truncate;
297 		zev_znode_setattr_t		setattr;
298 		zev_znode_acl_t			acl;
299 	} znode;
300 } zev_event_t;
301 
302 
303 
304 typedef struct zev_statistics_t {
305 	uint64_t	zev_queue_len;
306 	uint64_t	zev_bytes_read;
307 	uint64_t	zev_bytes_discarded;
308 	/* runtime settings */
309 	uint64_t	zev_max_queue_len;
310 	/* counters */
311 	uint64_t	zev_cnt_total_events;
312 	uint64_t	zev_cnt_discarded_events;
313 	uint64_t	zev_cnt_errors;
314 	uint64_t	zev_cnt_marks;
315 	/* zfsvfs ops */
316 	uint64_t	zev_cnt_zfs_mount;
317 	uint64_t	zev_cnt_zfs_umount;
318 	/* zvol ops */
319 	uint64_t	zev_cnt_zvol_write;
320 	uint64_t	zev_cnt_zvol_truncate;
321 	/* znode ops */
322 	uint64_t	zev_cnt_znode_close_after_update;
323 	uint64_t	zev_cnt_znode_create;
324 	uint64_t	zev_cnt_znode_remove;
325 	uint64_t	zev_cnt_znode_link;
326 	uint64_t	zev_cnt_znode_symlink;
327 	uint64_t	zev_cnt_znode_rename;
328 	uint64_t	zev_cnt_znode_write;
329 	uint64_t	zev_cnt_znode_truncate;
330 	uint64_t	zev_cnt_znode_setattr;
331 	uint64_t	zev_cnt_znode_acl;
332 } zev_statistics_t;
333 
334 typedef struct zev_queue_name {
335 	uint64_t		zev_namelen;
336 	char			zev_name[ZEV_MAX_QUEUE_NAME_LEN];
337 } zev_queue_name_t;
338 
339 typedef struct zev_ioctl_poolarg {
340 	uint64_t	zev_poolname_len;
341 	char		zev_poolname[MAXPATHLEN];
342 } zev_ioctl_poolarg_t;
343 
344 typedef struct zev_ioctl_mark {
345 	uint64_t	zev_mark_id;
346 	uint64_t	zev_guid;
347 	uint32_t	zev_payload_len;
348 	uint32_t	padding;
349 	/* payload follows */
350 } zev_ioctl_mark_t;
351 
352 typedef struct zev_ioctl_get_gen {
353 	/* input */
354 	uint64_t	inode;
355 	uint32_t	fd;	/* open fd to any object on the same fs */
356 	/* noput */
357 	uint32_t	padding;
358 	/* output */
359 	uint64_t	generation;
360 	uint64_t	crtime;
361 	uint64_t	guid;
362 	char		dataset[MAXPATHLEN];
363 } zev_ioctl_get_gen_t;
364 
365 typedef struct zev_ioctl_set_queue_properties {
366 	uint64_t		zev_max_queue_len;
367 	uint64_t		zev_poll_wakeup_threshold;
368 	uint16_t		zev_flags;
369 	uint16_t		padding1;
370 	uint32_t		padding2;
371 	zev_queue_name_t	zev_queue_name;
372 } zev_ioctl_set_queue_properties_t;
373 
374 typedef struct zev_ioctl_set_queue_properties zev_ioctl_get_queue_properties_t;
375 
376 typedef struct zev_ioctl_add_queue {
377 	uint64_t	zev_max_queue_len;
378 	uint32_t	padding;
379 	uint16_t	zev_flags;
380 	uint16_t	zev_namelen;
381 	char		zev_name[ZEV_MAX_QUEUE_NAME_LEN];
382 } zev_ioctl_add_queue_t;
383 
384 typedef struct zev_ioctl_remove_queue {
385 	zev_queue_name_t	zev_queue_name;
386 } zev_ioctl_remove_queue_t;
387 
388 typedef struct zev_ioctl_get_queue_statistics {
389 	zev_queue_name_t	zev_queue_name;
390 	zev_statistics_t	zev_statistics;
391 } zev_ioctl_get_queue_statistics_t;
392 
393 typedef struct zev_ioctl_debug_info {
394 	uint64_t	zev_memory_allocated;
395 	uint64_t	zev_chksum_cache_size;
396 	uint64_t	zev_chksum_cache_hits;
397 	uint64_t	zev_chksum_cache_misses;
398 } zev_ioctl_debug_info_t;
399 
400 typedef struct zev_ioctl_get_queue_list {
401 	uint64_t		zev_n_queues;
402 	zev_queue_name_t	zev_queue_name[ZEV_MAX_QUEUES];
403 } zev_ioctl_get_queue_list_t;
404 
405 typedef struct zev_ioctl_get_signatures {
406 	/* in */
407 	uint64_t	zev_offset;
408 	uint64_t	zev_len;
409 	uint32_t	zev_fd;
410 	uint32_t	zev_bufsize;
411 	/* out */
412 	uint64_t	zev_signature_cnt;
413 	/* up to zev_bufsize bytes of checksums will be written here */
414 } zev_ioctl_get_signatures_t;
415 
416 #pragma pack()
417 
418 #ifdef _KERNEL
419 
420 extern uint64_t zev_memory_allocated;
421 extern uint64_t zev_memory_freed;
422 
423 #define ZEV_MEM_ADD(memsize)						\
424 	do {								\
425 		int64_t tmp_delta = (int64_t)(memsize);			\
426 		atomic_add_64(&zev_memory_allocated, tmp_delta);	\
427 	} while(0)
428 
429 #define ZEV_MEM_SUB(memsize)						\
430 	do {								\
431 		int64_t tmp_delta = (int64_t)(memsize);			\
432 		atomic_add_64(&zev_memory_freed, tmp_delta);		\
433 	} while(0)
434 
435 void *zev_alloc(ssize_t sz);
436 void *zev_zalloc(ssize_t sz);
437 void zev_free(void *ptr, ssize_t sz);
438 
439 typedef struct zev_msg_t {
440 	struct zev_msg_t	*next;
441 	struct zev_msg_t	*prev;
442 	uint64_t		 seq;
443 	uint16_t		 size;
444 	uint16_t		 read;
445 	/* data follows */
446 } zev_msg_t;
447 
448 void zev_queue_error(int op, char *fmt, ...);
449 void zev_queue_message(int op, zev_msg_t *msg);
450 void zev_queue_message_nvlist(int op, nvlist_t *nvl);
451 int zev_skip_pool(objset_t *os);
452 int zev_skip_fs(zfsvfs_t *fs);
453 
454 #endif
455 
456 #endif /* __ZEV_H__ */
457 
458