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