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