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