You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

内核模块在5.10.113版本编译报错:modpost: "vfs_fstatat"未定义问题求助

Fixing vfs_stat Compilation Error in Linux Kernel 5.10+

Hey there, let's break down why you're seeing that vfs_fstatat undefined error and how to fix it for kernel 5.10.113.

What's Causing the Issue?

Starting with Linux kernel 5.10, the vfs_stat() function you're using isn't a standalone exported function anymore—it's now a macro that expands to a call to vfs_fstatat(). The problem is vfs_fstatat() isn't exported to kernel modules (it's not in the module symbol table), so your module can't link against it.

Your existing code handles the user-space access switching correctly for 5.10+, but the vfs_stat() call itself is no longer compatible with module code in newer kernels.

The Fix

We need to switch to using kernel APIs that are exported and intended for module use in 5.10+. Here's the updated version of your file_stat function:

#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/uaccess.h>

int file_stat(char *path, struct kstat *st){
    mm_segment_t oldfs;
    int ret = 0;

    #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)
        // Keep your existing logic for kernels older than 5.10
        oldfs = get_fs();
        #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
            set_fs(get_ds());
        #else
            set_fs(KERNEL_DS);
        #endif
        ret = vfs_stat(path, st);
        set_fs(oldfs);
    #else
        // New logic for 5.10+ kernels
        struct path file_path;
        // Resolve user-space path to kernel's internal path structure
        ret = user_path_at_empty(current->fs, path, AT_FDCWD, &file_path, 0);
        if (!ret) {
            // Get file attributes using the exported vfs_getattr
            ret = vfs_getattr(&file_path, st, STATX_ALL, AT_STATX_SYNC_AS_STAT);
            path_put(&file_path); // Don't forget to release the path reference
        }
    #endif

    return ret;
}

Key Changes Explained

  • Pre-5.10 Kernels: We leave your original code intact—vfs_stat() is still a valid, exported function here, and the fs switching logic works as expected.
  • 5.10+ Kernels:
    • user_path_at_empty() safely converts the user-space path string into a kernel struct path, which is the preferred way to handle paths in modern kernel code.
    • vfs_getattr() is the exported API for retrieving file attributes in 5.10+. It handles all necessary permission checks and populates the struct kstat correctly, just like vfs_stat() used to.
    • Always call path_put() after working with a struct path to release its reference and avoid memory leaks.

Quick Checks

  • Make sure your module's Makefile is pointing to the correct 5.10 kernel headers—mismatched headers can cause unexpected errors.
  • If you hit issues with STATX_ALL, verify that CONFIG_STATX is enabled in your kernel config (it's usually enabled by default in most distributions).

内容的提问来源于stack exchange,提问作者Jarvis Bao

火山引擎 最新活动