【问题标题】:Where do you get inode functionality from?您从哪里获得 inode 功能?
【发布时间】:2012-08-30 05:45:40
【问题描述】:

我有一些 linux 驱动程序正在尝试从 linux 2.4 移植到 3.0。在这段漫长的时间里,ioctl(现在为unlocked_ioctl)的参数列表发生了一些变化:

-static int can_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
+static long can_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 

代码使用 inode 获取次要版本并将其传递给其他一些命令。现在 inode 不是 ioctl 参数列表中给出的“free-be”,我怎样才能得到它?

是否可以从文件指针派生?或者当它出现在 _open() 方法中时,我应该“保存”一个指向它的全局指针吗?如果有更好的方法,我宁愿避免这种情况。

【问题讨论】:

    标签: c linux linux-kernel ioctl inode


    【解决方案1】:

    你可以从内核空间的struct file * file中获取inode。这是简短的结构图

    文件->f_path.d.dentry->d_inode;你可以找到下面的定义。

    dcache.h 中的 dentry 结构

    struct dentry {
        atomic_t d_count;
        unsigned int d_flags;       /* protected by d_lock */
        spinlock_t d_lock;      /* per dentry lock */
        int d_mounted;
        struct inode *d_inode;      /* Where the name belongs to - NULL is
                                     * negative */
        /*
         * The next three fields are touched by __d_lookup.  Place them here
         * so they all fit in a cache line.
         */
        struct hlist_node d_hash;   /* lookup hash list */
    

    对于fs.h中的文件结构

     file {
               /*  
                * fu_list becomes invalid after file_free is called and queued via
                * fu_rcuhead for RCU freeing
                */
               ...
               struct path             f_path;
               #define f_dentry        f_path.dentry
               #define f_vfsmnt        f_path.mnt
               const struct file_operations    *f_op;
               spinlock_t              f_lock;  /* f_ep_links, f_flags, no IRQ */
               #ifdef CONFIG_SMP
    

    【讨论】:

    • file->fpath.d.dentry->d_inode.. 你的意思是 file->f_path.dentry->d_inode 对吧?
    【解决方案2】:

    哎呀,只是通过查看内核并查看其他驱动程序来解决这个问题(不知道为什么我以前没有想到这样做)。如果其他人有兴趣,您可以从传递给 ioctl 的文件指针中获取 inode:

    long can_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    {
        struct inode *inode = file->f_path.dentry->d_inode;
    

    如果有人知道为什么这是一个坏主意(我只是从另一个司机那里得到的),或者如果有更好/首选的方法,请告诉我。

    【讨论】:

    • 注意文件是否可以为 NULL(不知道是否会发生这种情况):: `inode = (file) ?文件->f_path.dentry->d_inode: NULL;
    • 好点。我的代码无法进入 fd 的 ioctl 调用,因此无法打开(因此在我的情况下 file* 不能为 NULL),但将检查放在那里是一个很好的做法。
    • 不,“把检查放在那里”不是一个好习惯,NULL 文件* 将是一个内核 BUG,所以你能做的最好的事情就是崩溃。
    猜你喜欢
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 2010-12-18
    • 2010-09-19
    相关资源
    最近更新 更多