Write to debugfs from Linux Kernel Module

2.5k views Asked by At

I've managed to successfully create a dentry in the matching path, but now how do I actually write there?

struct dentry* log_dir = debugfs_create_dir ("my_module", NULL);
struct dentry* log_file = debugfs_create_dir ("log", 0777, log_dir, NULL, NULL);
1

There are 1 answers

0
dragosht On BEST ANSWER

I'd say the best reference for what you need to do would be the debugfs.txt documentation file within the kernel source tree.

I also assume you made a mistake in your code sample here:

struct dentry* log_file = debugfs_create_dir ("log", 0777, log_dir, NULL, NULL);

Since it looks like you're trying to create a file, not another directory. So I guess what you wanted to do is more like this:

struct dentry* log_file = debugfs_create_file("log", 0777, log_dir, NULL, &log_fops);

where log_fops would maybe be something like this:

static const struct file_operations log_fops = {
    .owner  =   THIS_MODULE,
    .read   =   log_read,
    .write  =   log_write, /* maybe you don't need this */
};

And, of course you'd also need to implement your log_read and log_write functions:

ssize_t log_read(struct file *file, char __user *buff, size_t count, loff_t *offset);

ssize_t log_write(struct file *file, const char __user *buff, size_t count, loff_t *offset);