4

Linux 2.4 から 3.0 に移植しようとしている Linux ドライバーがいくつかあります。この長い期間の間に、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() メソッドに表示されたときにグローバル ポインターを「保存」する必要がありますか? 他に良い方法があれば避けたいです。

4

2 に答える 2

4

カーネル空間の 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
于 2012-09-05T13:47:33.773 に答える
3

おっと、カーネルを調べたり、他のドライバーを調べたりして、それを理解しました (なぜ以前にそうすることに気がつかなかったのかわかりません)。他の誰かが興味を持っている場合は、次のように ioctl に渡されたファイル ポインタから i ノードを取得できます。

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

これが悪い考えである理由を誰かが知っている場合 (私は別のドライバーから取得しただけです)、またはより良い/好ましい方法がある場合はお知らせください。

于 2012-09-05T13:40:59.043 に答える