I recently started learning how the Linux kernel works. and I wanted to implement my own iostat. To do this, I need to iterate over each block device and calculate the metrics, the problem is that I don't understand how to iterate over block devices. I take them from /sys/block.
I trying use structure dir_context and function iterate_dir.
static bool iterate_block_devices(struct dir_context *ctx, const char *name, int namelen,
loff_t offset, u64 ino, unsigned int d_type) {
pr_info("Block device: %.*s\n", namelen, name);
return 0; // Continue iteration
}
int read_block_devices(void) {
struct file *dir;
struct dir_context ctx = {
.actor = iterate_block_devices,
};
dir = filp_open("/sys/block", O_DIRECTORY | O_RDONLY, 0);
if (IS_ERR(dir)) {
pr_err("Error opening directory /sys/block\n");
return -1;
}
iterate_dir(dir, &ctx);
filp_close(dir, NULL);
return 0;
}
but at the exit I get: "Block device: . " Please help me solve this problem. I don't understand well how iterate_dir and dir_context work.