xref: /titanic_41/usr/src/uts/common/sys/fs/zev.h (revision 4b0d01e9d944e10498c80bc88d80a2f5cdd9be22)
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 
74 /* checksum block sizes */
75 #define ZEV_L0_SIZE	4096
76 #define ZEV_L1_SIZE	(256 * ZEV_L0_SIZE)
77 
78 /* zfs event records (as they are represented through the character device) */
79 
80 #pragma pack(1)
81 typedef struct zev_inode_info_t {
82 	uint64_t	ino;
83 	uint64_t	gen;
84 	uint64_t	mtime;
85 	uint64_t	ctime;
86 	uint64_t	size;
87 	uint64_t	mode;
88 	uint64_t	links;
89 	uint32_t	type;
90 	uint32_t	flags;
91 } zev_inode_info_t;
92 
93 #define ZEV_ERRSTR(rec)		((char *)(rec + 1))
94 #define ZEV_DATASET(rec)	((char *)(rec + 1))
95 #define ZEV_MOUNTPOINT(rec)	(((char *)(rec + 1)) + rec->dataset_len + 1)
96 #define ZEV_NAME(rec)		((char *)(rec + 1))
97 #define ZEV_SRCNAME(rec)	((char *)(rec + 1))
98 #define ZEV_DSTNAME(rec)	(((char *)(rec + 1)) + rec->srcname_len + 1)
99 #define ZEV_LINK(rec)		(((char *)(rec + 1)) + rec->name_len + 1)
100 #define ZEV_PAYLOAD(rec)	((char *)(rec + 1))
101 #define ZEV_SIGNATURES(rec)	((char *)(rec + 1))
102 
103 #define ZEV_COMMON_FIELDS						\
104 	uint32_t		record_len;				\
105 	uint32_t		op;					\
106 	uint64_t		op_time;				\
107 	uint64_t		guid
108 
109 typedef struct zev_sig_t {
110 	uint16_t	level;
111 	uint16_t	padding1;
112 	uint32_t	padding2;
113 	uint64_t	block_offset;
114 	uint8_t		value[SHA1_DIGEST_LENGTH];  /* 20 bytes -> no padding */
115 } zev_sig_t;
116 
117 typedef struct zev_header_t {
118 	ZEV_COMMON_FIELDS;
119 } zev_header_t;
120 
121 typedef struct zev_error_t {
122 	ZEV_COMMON_FIELDS;
123 	uint32_t		failed_op;
124 	uint32_t		errstr_len;
125 	/* error string follows */
126 } zev_error_t;
127 
128 typedef struct zev_mark_t {
129 	ZEV_COMMON_FIELDS;
130 	uint64_t		mark_id;
131 	uint32_t		payload_len;
132 	uint32_t		padding;
133 	/* payload follows */
134 } zev_mark_t;
135 
136 typedef struct zev_zfs_mount_t {
137 	ZEV_COMMON_FIELDS;
138 	zev_inode_info_t	root;
139 	uint32_t		remount;
140 	uint32_t		dataset_len;
141 	uint32_t		mountpoint_len;
142 	uint32_t		padding;
143 	/* dataset follows */
144 	/* mountpoint follows */
145 } zev_zfs_mount_t;
146 
147 typedef struct zev_zfs_umount_t {
148 	ZEV_COMMON_FIELDS;
149 } zev_zfs_umount_t;
150 
151 typedef struct zev_zvol_truncate_t {
152 	ZEV_COMMON_FIELDS;
153 	uint64_t		txg;
154 	uint64_t		offset;
155 	uint64_t		length;
156 	uint32_t		dataset_len;
157 	uint32_t		padding;
158 	/* dataset follows */
159 } zev_zvol_truncate_t;
160 
161 typedef struct zev_zvol_write_t {
162 	ZEV_COMMON_FIELDS;
163 	uint64_t		txg;
164 	uint64_t		offset;
165 	uint64_t		length;
166 	uint32_t		dataset_len;
167 	uint32_t		padding;
168 	/* dataset follows */
169 } zev_zvol_write_t;
170 
171 typedef struct zev_znode_close_after_update_t {
172 	ZEV_COMMON_FIELDS;
173 	zev_inode_info_t	file;
174 } zev_znode_close_after_update_t;
175 
176 typedef struct zev_znode_create_t {
177 	ZEV_COMMON_FIELDS;
178 	uint64_t		txg;
179 	zev_inode_info_t	file;
180 	zev_inode_info_t	parent;
181 	uint32_t		name_len;
182 	uint32_t		padding;
183 	/* name follows */
184 } zev_znode_create_t;
185 
186 typedef struct zev_znode_create_t zev_znode_mkdir_t;
187 typedef struct zev_znode_create_t zev_znode_make_xattr_dir_t;
188 
189 typedef struct zev_znode_remove_t {
190 	ZEV_COMMON_FIELDS;
191 	uint64_t		txg;
192 	zev_inode_info_t	file;
193 	zev_inode_info_t	parent;
194 	uint32_t		name_len;
195 	uint32_t		padding;
196 	/* name follows */
197 } zev_znode_remove_t;
198 
199 typedef struct zev_znode_remove_t zev_znode_rmdir_t;
200 
201 typedef struct zev_znode_link_t {
202 	ZEV_COMMON_FIELDS;
203 	uint64_t		txg;
204 	zev_inode_info_t	parent;
205 	zev_inode_info_t	file;
206 	uint32_t		name_len;
207 	uint32_t		padding;
208 	/* new_name follows */
209 } zev_znode_link_t;
210 
211 typedef struct zev_znode_symlink_t {
212 	ZEV_COMMON_FIELDS;
213 	uint64_t		txg;
214 	zev_inode_info_t	parent;
215 	zev_inode_info_t	file;
216 	uint32_t		name_len;
217 	uint32_t		link_len;
218 	/* name follows */
219 	/* link follows */
220 } zev_znode_symlink_t;
221 
222 typedef struct zev_znode_rename_t {
223 	ZEV_COMMON_FIELDS;
224 	uint64_t		txg;
225 	zev_inode_info_t	srcdir;
226 	zev_inode_info_t	dstdir;
227 	zev_inode_info_t	file;
228 	uint32_t		srcname_len;
229 	uint32_t		dstname_len;
230 	/* srcname follows */
231 	/* dstname follows */
232 } zev_znode_rename_t;
233 
234 typedef struct zev_znode_write_t {
235 	ZEV_COMMON_FIELDS;
236 	uint64_t		txg;
237 	zev_inode_info_t	file;
238 	uint64_t		offset;
239 	uint64_t		length;
240 	uint64_t		signature_cnt;
241 	/* signatures follow */
242 } zev_znode_write_t;
243 
244 typedef struct zev_znode_truncate_t {
245 	ZEV_COMMON_FIELDS;
246 	uint64_t		txg;
247 	zev_inode_info_t	file;
248 	uint64_t		offset;
249 	uint64_t		length;
250 	uint64_t		signature_cnt;
251 	/* signatures follow */
252 } zev_znode_truncate_t;
253 
254 typedef struct zev_znode_setattr_t {
255 	ZEV_COMMON_FIELDS;
256 	uint64_t		txg;
257 	zev_inode_info_t	file;
258 } zev_znode_setattr_t;
259 
260 typedef struct zev_znode_acl_t {
261 	ZEV_COMMON_FIELDS;
262 	uint64_t		txg;
263 	zev_inode_info_t	file;
264 } zev_znode_acl_t;
265 
266 /* convenience helper definition */
267 typedef union {
268 	zev_header_t				header;
269 
270 	zev_error_t				error;
271 	zev_mark_t				mark;
272 	union {
273 		zev_zfs_mount_t			mount;
274 		zev_zfs_umount_t		umount;
275 	} zfs;
276 	union {
277 		zev_zvol_truncate_t		truncate;
278 		zev_zvol_write_t		write;
279 	} zvol;
280 	union {
281 		zev_znode_close_after_update_t	close;
282 		zev_znode_create_t		create;
283 		zev_znode_mkdir_t		mkdir;
284 		zev_znode_make_xattr_dir_t	mkxattrdir;
285 		zev_znode_remove_t		remove;
286 		zev_znode_rmdir_t		rmdir;
287 		zev_znode_link_t		link;
288 		zev_znode_symlink_t		symlink;
289 		zev_znode_rename_t		rename;
290 		zev_znode_write_t		write;
291 		zev_znode_truncate_t		truncate;
292 		zev_znode_setattr_t		setattr;
293 		zev_znode_acl_t			acl;
294 	} znode;
295 } zev_event_t;
296 
297 
298 
299 typedef struct zev_statistics_t {
300 	uint64_t	zev_queue_len;
301 	uint64_t	zev_bytes_read;
302 	uint64_t	zev_bytes_discarded;
303 	/* runtime settings */
304 	uint64_t	zev_max_queue_len;
305 	/* counters */
306 	uint64_t	zev_cnt_total_events;
307 	uint64_t	zev_cnt_discarded_events;
308 	uint64_t	zev_cnt_errors;
309 	uint64_t	zev_cnt_marks;
310 	/* zfsvfs ops */
311 	uint64_t	zev_cnt_zfs_mount;
312 	uint64_t	zev_cnt_zfs_umount;
313 	/* zvol ops */
314 	uint64_t	zev_cnt_zvol_write;
315 	uint64_t	zev_cnt_zvol_truncate;
316 	/* znode ops */
317 	uint64_t	zev_cnt_znode_close_after_update;
318 	uint64_t	zev_cnt_znode_create;
319 	uint64_t	zev_cnt_znode_remove;
320 	uint64_t	zev_cnt_znode_link;
321 	uint64_t	zev_cnt_znode_symlink;
322 	uint64_t	zev_cnt_znode_rename;
323 	uint64_t	zev_cnt_znode_write;
324 	uint64_t	zev_cnt_znode_truncate;
325 	uint64_t	zev_cnt_znode_setattr;
326 	uint64_t	zev_cnt_znode_acl;
327 } zev_statistics_t;
328 
329 typedef struct zev_queue_name {
330 	uint64_t		zev_namelen;
331 	char			zev_name[ZEV_MAX_QUEUE_NAME_LEN];
332 } zev_queue_name_t;
333 
334 typedef struct zev_ioctl_poolarg {
335 	uint64_t	zev_poolname_len;
336 	char		zev_poolname[MAXPATHLEN];
337 } zev_ioctl_poolarg_t;
338 
339 typedef struct zev_ioctl_mark {
340 	uint64_t	zev_mark_id;
341 	uint64_t	zev_guid;
342 	uint32_t	zev_payload_len;
343 	uint32_t	padding;
344 	/* payload follows */
345 } zev_ioctl_mark_t;
346 
347 typedef struct zev_ioctl_get_gen {
348 	/* input */
349 	uint64_t	inode;
350 	uint32_t	fd;	/* open fd to any object on the same fs */
351 	/* noput */
352 	uint32_t	padding;
353 	/* output */
354 	uint64_t	generation;
355 	uint64_t	crtime;
356 	uint64_t	guid;
357 	char		dataset[MAXPATHLEN];
358 } zev_ioctl_get_gen_t;
359 
360 typedef struct zev_ioctl_set_queue_properties {
361 	uint64_t		zev_max_queue_len;
362 	uint64_t		zev_poll_wakeup_threshold;
363 	uint16_t		zev_flags;
364 	uint16_t		padding1;
365 	uint32_t		padding2;
366 	zev_queue_name_t	zev_queue_name;
367 } zev_ioctl_set_queue_properties_t;
368 
369 typedef struct zev_ioctl_set_queue_properties zev_ioctl_get_queue_properties_t;
370 
371 typedef struct zev_ioctl_add_queue {
372 	uint64_t	zev_max_queue_len;
373 	uint32_t	padding;
374 	uint16_t	zev_flags;
375 	uint16_t	zev_namelen;
376 	char		zev_name[ZEV_MAX_QUEUE_NAME_LEN];
377 } zev_ioctl_add_queue_t;
378 
379 typedef struct zev_ioctl_remove_queue {
380 	zev_queue_name_t	zev_queue_name;
381 } zev_ioctl_remove_queue_t;
382 
383 typedef struct zev_ioctl_get_queue_statistics {
384 	zev_queue_name_t	zev_queue_name;
385 	zev_statistics_t	zev_statistics;
386 } zev_ioctl_get_queue_statistics_t;
387 
388 typedef struct zev_ioctl_debug_info {
389 	uint64_t	zev_memory_allocated;
390 	uint64_t	zev_chksum_cache_size;
391 	uint64_t	zev_chksum_cache_hits;
392 	uint64_t	zev_chksum_cache_misses;
393 } zev_ioctl_debug_info_t;
394 
395 typedef struct zev_ioctl_get_queue_list {
396 	uint64_t		zev_n_queues;
397 	zev_queue_name_t	zev_queue_name[ZEV_MAX_QUEUES];
398 } zev_ioctl_get_queue_list_t;
399 
400 typedef struct zev_ioctl_get_signatures {
401 	/* in */
402 	uint64_t	zev_offset;
403 	uint64_t	zev_len;
404 	uint32_t	zev_fd;
405 	uint32_t	zev_bufsize;
406 	/* out */
407 	uint64_t	zev_signature_cnt;
408 	/* up to zev_bufsize bytes of checksums will be written here */
409 } zev_ioctl_get_signatures_t;
410 
411 #pragma pack()
412 
413 #ifdef _KERNEL
414 
415 extern uint64_t zev_memory_allocated;
416 extern uint64_t zev_memory_freed;
417 
418 #define ZEV_MEM_ADD(memsize)						\
419 	do {								\
420 		int64_t tmp_delta = (int64_t)(memsize);			\
421 		atomic_add_64(&zev_memory_allocated, tmp_delta);	\
422 	} while(0)
423 
424 #define ZEV_MEM_SUB(memsize)						\
425 	do {								\
426 		int64_t tmp_delta = (int64_t)(memsize);			\
427 		atomic_add_64(&zev_memory_freed, tmp_delta);		\
428 	} while(0)
429 
430 void *zev_alloc(ssize_t sz);
431 void *zev_zalloc(ssize_t sz);
432 void zev_free(void *ptr, ssize_t sz);
433 
434 typedef struct zev_msg_t {
435 	struct zev_msg_t	*next;
436 	struct zev_msg_t	*prev;
437 	uint64_t		 seq;
438 	uint16_t		 size;
439 	uint16_t		 read;
440 	/* data follows */
441 } zev_msg_t;
442 
443 void zev_queue_error(int op, char *fmt, ...);
444 void zev_queue_message(int op, zev_msg_t *msg);
445 void zev_queue_message_nvlist(int op, nvlist_t *nvl);
446 int zev_skip_pool(objset_t *os);
447 
448 #endif
449 
450 #endif /* __ZEV_H__ */
451 
452