|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// clang-format off |
| 4 | +#include "vmlinux.h" |
| 5 | + |
| 6 | +#include "kdev.h" |
| 7 | +#include "types.h" |
| 8 | +#include "maps.h" |
| 9 | + |
| 10 | +#include <bpf/bpf_core_read.h> |
| 11 | +#include <bpf/bpf_helpers.h> |
| 12 | +// clang-format on |
| 13 | + |
| 14 | +#define BTRFS_SUPER_MAGIC 0x9123683E |
| 15 | + |
| 16 | +/** |
| 17 | + * Retrieve the inode and device numbers and return them as a new key. |
| 18 | + * |
| 19 | + * Different filesystems may `stat` files in different ways, if support |
| 20 | + * for a new filesystem is needed, add it here. |
| 21 | + * |
| 22 | + * Most Linux filesystems use the following generic function to fill |
| 23 | + * these fields when running `stat`: |
| 24 | + * https://github.com/torvalds/linux/blob/7d0a66e4bb9081d75c82ec4957c50034cb0ea449/fs/stat.c#L82 |
| 25 | + * |
| 26 | + * The method used to retrieve the device is different in btrfs and can |
| 27 | + * be found here: |
| 28 | + * https://github.com/torvalds/linux/blob/7d0a66e4bb9081d75c82ec4957c50034cb0ea449/fs/btrfs/inode.c#L8038 |
| 29 | + */ |
| 30 | +__always_inline static inode_key_t inode_to_key(struct inode* inode) { |
| 31 | + inode_key_t key = {0}; |
| 32 | + if (inode == NULL) { |
| 33 | + return key; |
| 34 | + } |
| 35 | + |
| 36 | + unsigned long magic = inode->i_sb->s_magic; |
| 37 | + switch (magic) { |
| 38 | + case BTRFS_SUPER_MAGIC: |
| 39 | + if (bpf_core_type_exists(struct btrfs_inode)) { |
| 40 | + struct btrfs_inode* btrfs_inode = container_of(inode, struct btrfs_inode, vfs_inode); |
| 41 | + key.inode = inode->i_ino; |
| 42 | + key.dev = BPF_CORE_READ(btrfs_inode, root, anon_dev); |
| 43 | + break; |
| 44 | + } |
| 45 | + // If the btrfs_inode does not exist, most likely it is not |
| 46 | + // supported on the system. Fallback to the generic implementation |
| 47 | + // just in case. |
| 48 | + default: |
| 49 | + key.inode = inode->i_ino; |
| 50 | + key.dev = inode->i_sb->s_dev; |
| 51 | + break; |
| 52 | + } |
| 53 | + |
| 54 | + // Encode the device so it matches with the result of `stat` in |
| 55 | + // userspace |
| 56 | + key.dev = new_encode_dev(key.dev); |
| 57 | + |
| 58 | + return key; |
| 59 | +} |
| 60 | + |
| 61 | +__always_inline static inode_value_t* inode_get(struct inode_key_t* inode) { |
| 62 | + if (inode == NULL) { |
| 63 | + return NULL; |
| 64 | + } |
| 65 | + return bpf_map_lookup_elem(&inode_map, inode); |
| 66 | +} |
| 67 | + |
| 68 | +__always_inline static long inode_remove(struct inode_key_t* inode) { |
| 69 | + if (inode == NULL) { |
| 70 | + return 0; |
| 71 | + } |
| 72 | + return bpf_map_delete_elem(&inode_map, inode); |
| 73 | +} |
| 74 | + |
| 75 | +typedef enum inode_monitored_t { |
| 76 | + NOT_MONITORED = 0, |
| 77 | + MONITORED, |
| 78 | +} inode_monitored_t; |
| 79 | + |
| 80 | +/** |
| 81 | + * Check if the provided inode is being monitored. |
| 82 | + * |
| 83 | + * The current implementation is very basic and might seem like |
| 84 | + * overkill, but in the near future this function will be extended to |
| 85 | + * check if the parent of the provided inode is monitored and provide |
| 86 | + * different results for handling more complicated scenarios. |
| 87 | + */ |
| 88 | +__always_inline static inode_monitored_t inode_is_monitored(const inode_value_t* inode) { |
| 89 | + if (inode != NULL) { |
| 90 | + return MONITORED; |
| 91 | + } |
| 92 | + |
| 93 | + return NOT_MONITORED; |
| 94 | +} |
| 95 | + |
| 96 | +__always_inline static void inode_copy_or_reset(inode_key_t* dst, const inode_key_t* src) { |
| 97 | + if (dst == NULL) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + if (src != NULL) { |
| 102 | + dst->inode = src->inode; |
| 103 | + dst->dev = src->dev; |
| 104 | + } else { |
| 105 | + dst->inode = 0; |
| 106 | + dst->dev = 0; |
| 107 | + } |
| 108 | +} |
0 commit comments