Hugo REYMOND
Hugo REYMOND

Reputation: 369

Impossible to delete file in debugfs

I'm playing with debugfs. In a module, I've created a directory 'test_debugfs' in the debugfs filesystem (mounted at /sys/kernel/debug) and a file 'demo_file'.

// Create the test_debufs in /sys/kernel/debug
struct dentry * my_dirent;
static int __init my_module_init_module(void) {
    my_dirent = debugfs_create_dir("test_debugfs", NULL);
    debugfs_create_file("demo_file", 0666, my_dirent, NULL, &fops_debugfs);
}

Unfortunately, I forgot to remove the directory on module unload, and now I cannot remove the demo_file anymore.

# rmmod my_module
# cd /sys/kernel/debug/test_debugfs
# ls
demo_file
# rm -rf demo_file
rm: cannot remove 'demo_file': Operation not permitted
# sstat
  File: demo_file
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 6h/6d   Inode: 16426       Links: 1
Access: (0666/-rw-rw-rw-)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-04-28 10:20:14.807999989 +0200
Modify: 2021-04-28 10:20:14.807999989 +0200
Change: 2021-04-28 10:20:14.807999989 +0200
 Birth: -

After rebooting my machine, the demo_file is still there.

Do you know how I could remove it ?

Answer: Thanks to Varun, I managed to remove the file directly in the module with this code:

struct dentry * my_dirent;
static int __init my_module_init_module(void) {
    struct path path;
    ret = kern_path("/sys/kernel/debug/test_debugfs", LOOKUP_DIRECTORY, &path);
    if (ret)
         pr_err("Failed to lookup /sys/kernel/debug/test_debugfs err %d\n", ret);
    else
        debugfs_remove_recursive(path.dentry);
}

Upvotes: 1

Views: 1006

Answers (1)

Varun
Varun

Reputation: 477

You cannot use rm command to remove file from debug_fs ..

The debugfs filesystem does not support unlink function in the directory inode operations. Hence rm command will fail

You have to use debugfs function void debugfs_remove(struct dentry *dentry) where dentry parameter is the return value from debugfs_create_file function call

Upvotes: 2

Related Questions