xref: /titanic_44/usr/src/common/zev/zev.h (revision 94875cb8c67cf63584fdddc66a57b6a1f86cb3e3)
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 	uint32_t		srcname_len;
233 	uint32_t		dstname_len;
234 	/* srcname follows */
235 	/* dstname follows */
236 } zev_znode_rename_t;
237 
238 typedef struct zev_znode_write_t {
239 	ZEV_COMMON_FIELDS;
240 	uint64_t		txg;
241 	zev_inode_info_t	file;
242 	uint64_t		offset;
243 	uint64_t		length;
244 	uint64_t		signature_cnt;
245 	/* signatures follow */
246 } zev_znode_write_t;
247 
248 typedef struct zev_znode_truncate_t {
249 	ZEV_COMMON_FIELDS;
250 	uint64_t		txg;
251 	zev_inode_info_t	file;
252 	uint64_t		offset;
253 	uint64_t		length;
254 	uint64_t		signature_cnt;
255 	/* signatures follow */
256 } zev_znode_truncate_t;
257 
258 typedef struct zev_znode_setattr_t {
259 	ZEV_COMMON_FIELDS;
260 	uint64_t		txg;
261 	zev_inode_info_t	file;
262 } zev_znode_setattr_t;
263 
264 typedef struct zev_znode_acl_t {
265 	ZEV_COMMON_FIELDS;
266 	uint64_t		txg;
267 	zev_inode_info_t	file;
268 } zev_znode_acl_t;
269 
270 /* convenience helper definition */
271 typedef union {
272 	zev_header_t				header;
273 
274 	zev_error_t				error;
275 	zev_mark_t				mark;
276 	union {
277 		zev_zfs_mount_t			mount;
278 		zev_zfs_umount_t		umount;
279 	} zfs;
280 	union {
281 		zev_zvol_truncate_t		truncate;
282 		zev_zvol_write_t		write;
283 	} zvol;
284 	union {
285 		zev_znode_close_after_update_t	close;
286 		zev_znode_create_t		create;
287 		zev_znode_mkdir_t		mkdir;
288 		zev_znode_make_xattr_dir_t	mkxattrdir;
289 		zev_znode_remove_t		remove;
290 		zev_znode_rmdir_t		rmdir;
291 		zev_znode_link_t		link;
292 		zev_znode_symlink_t		symlink;
293 		zev_znode_rename_t		rename;
294 		zev_znode_write_t		write;
295 		zev_znode_truncate_t		truncate;
296 		zev_znode_setattr_t		setattr;
297 		zev_znode_acl_t			acl;
298 	} znode;
299 } zev_event_t;
300 
301 
302 
303 typedef struct zev_statistics_t {
304 	uint64_t	zev_queue_len;
305 	uint64_t	zev_bytes_read;
306 	uint64_t	zev_bytes_discarded;
307 	/* runtime settings */
308 	uint64_t	zev_max_queue_len;
309 	/* counters */
310 	uint64_t	zev_cnt_total_events;
311 	uint64_t	zev_cnt_discarded_events;
312 	uint64_t	zev_cnt_errors;
313 	uint64_t	zev_cnt_marks;
314 	/* zfsvfs ops */
315 	uint64_t	zev_cnt_zfs_mount;
316 	uint64_t	zev_cnt_zfs_umount;
317 	/* zvol ops */
318 	uint64_t	zev_cnt_zvol_write;
319 	uint64_t	zev_cnt_zvol_truncate;
320 	/* znode ops */
321 	uint64_t	zev_cnt_znode_close_after_update;
322 	uint64_t	zev_cnt_znode_create;
323 	uint64_t	zev_cnt_znode_remove;
324 	uint64_t	zev_cnt_znode_link;
325 	uint64_t	zev_cnt_znode_symlink;
326 	uint64_t	zev_cnt_znode_rename;
327 	uint64_t	zev_cnt_znode_write;
328 	uint64_t	zev_cnt_znode_truncate;
329 	uint64_t	zev_cnt_znode_setattr;
330 	uint64_t	zev_cnt_znode_acl;
331 } zev_statistics_t;
332 
333 typedef struct zev_queue_name {
334 	uint64_t		zev_namelen;
335 	char			zev_name[ZEV_MAX_QUEUE_NAME_LEN];
336 } zev_queue_name_t;
337 
338 typedef struct zev_ioctl_poolarg {
339 	uint64_t	zev_poolname_len;
340 	char		zev_poolname[MAXPATHLEN];
341 } zev_ioctl_poolarg_t;
342 
343 typedef struct zev_ioctl_mark {
344 	uint64_t	zev_mark_id;
345 	uint64_t	zev_guid;
346 	uint32_t	zev_payload_len;
347 	uint32_t	padding;
348 	/* payload follows */
349 } zev_ioctl_mark_t;
350 
351 typedef struct zev_ioctl_get_gen {
352 	/* input */
353 	uint64_t	inode;
354 	uint32_t	fd;	/* open fd to any object on the same fs */
355 	/* noput */
356 	uint32_t	padding;
357 	/* output */
358 	uint64_t	generation;
359 	uint64_t	crtime;
360 	uint64_t	guid;
361 	char		dataset[MAXPATHLEN];
362 } zev_ioctl_get_gen_t;
363 
364 typedef struct zev_ioctl_set_queue_properties {
365 	uint64_t		zev_max_queue_len;
366 	uint64_t		zev_poll_wakeup_threshold;
367 	uint16_t		zev_flags;
368 	uint16_t		padding1;
369 	uint32_t		padding2;
370 	zev_queue_name_t	zev_queue_name;
371 } zev_ioctl_set_queue_properties_t;
372 
373 typedef struct zev_ioctl_set_queue_properties zev_ioctl_get_queue_properties_t;
374 
375 typedef struct zev_ioctl_add_queue {
376 	uint64_t	zev_max_queue_len;
377 	uint32_t	padding;
378 	uint16_t	zev_flags;
379 	uint16_t	zev_namelen;
380 	char		zev_name[ZEV_MAX_QUEUE_NAME_LEN];
381 } zev_ioctl_add_queue_t;
382 
383 typedef struct zev_ioctl_remove_queue {
384 	zev_queue_name_t	zev_queue_name;
385 } zev_ioctl_remove_queue_t;
386 
387 typedef struct zev_ioctl_get_queue_statistics {
388 	zev_queue_name_t	zev_queue_name;
389 	zev_statistics_t	zev_statistics;
390 } zev_ioctl_get_queue_statistics_t;
391 
392 typedef struct zev_ioctl_debug_info {
393 	uint64_t	zev_memory_allocated;
394 	uint64_t	zev_chksum_cache_size;
395 	uint64_t	zev_chksum_cache_hits;
396 	uint64_t	zev_chksum_cache_misses;
397 } zev_ioctl_debug_info_t;
398 
399 typedef struct zev_ioctl_get_queue_list {
400 	uint64_t		zev_n_queues;
401 	zev_queue_name_t	zev_queue_name[ZEV_MAX_QUEUES];
402 } zev_ioctl_get_queue_list_t;
403 
404 typedef struct zev_ioctl_get_signatures {
405 	/* in */
406 	uint64_t	zev_offset;
407 	uint64_t	zev_len;
408 	uint32_t	zev_fd;
409 	uint32_t	zev_bufsize;
410 	/* out */
411 	uint64_t	zev_signature_cnt;
412 	/* up to zev_bufsize bytes of checksums will be written here */
413 } zev_ioctl_get_signatures_t;
414 
415 #pragma pack()
416 
417 #ifdef _KERNEL
418 
419 extern uint64_t zev_memory_allocated;
420 extern uint64_t zev_memory_freed;
421 
422 #define ZEV_MEM_ADD(memsize)						\
423 	do {								\
424 		int64_t tmp_delta = (int64_t)(memsize);			\
425 		atomic_add_64(&zev_memory_allocated, tmp_delta);	\
426 	} while(0)
427 
428 #define ZEV_MEM_SUB(memsize)						\
429 	do {								\
430 		int64_t tmp_delta = (int64_t)(memsize);			\
431 		atomic_add_64(&zev_memory_freed, tmp_delta);		\
432 	} while(0)
433 
434 void *zev_alloc(ssize_t sz);
435 void *zev_zalloc(ssize_t sz);
436 void zev_free(void *ptr, ssize_t sz);
437 
438 typedef struct zev_msg_t {
439 	struct zev_msg_t	*next;
440 	struct zev_msg_t	*prev;
441 	uint64_t		 seq;
442 	uint16_t		 size;
443 	uint16_t		 read;
444 	/* data follows */
445 } zev_msg_t;
446 
447 void zev_queue_error(int op, char *fmt, ...);
448 void zev_queue_message(int op, zev_msg_t *msg);
449 void zev_queue_message_nvlist(int op, nvlist_t *nvl);
450 int zev_skip_pool(objset_t *os);
451 
452 #endif
453 
454 #endif /* __ZEV_H__ */
455 
456