简体   繁体   中英

How to get the minor number of a file in a Linux driver?

Is there a better solution to get the minor number?

Can I avoid checking kernel version?

static long unlocked_ioctl(struct file *f, unsigned int o, unsigned long d)
{
#if KERNEL_VERSION(3, 18, 0) > LINUX_VERSION_CODE
    struct inode* inode = f->f_dentry->d_inode;
#else
    struct inode* inode = f->f_path.dentry->d_inode;
#endif

    int minor = iminor(inode);
}

As an addendum to Marco Bonelli's answer, file_inode() was added in the 3.9 kernel, so if earlier kernel versions need to be supported, some kernel compatibility code needs to be added. I use something like the following:

/*
 * The file_dentry() inline function was added in kernel version 4.6.0.
 * Emulate it for earlier kernels.
 */
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
static inline struct dentry *kcompat_file_dentry(const struct file *f)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
    return f->f_dentry;
#else
    return f->f_path.dentry;
#endif
}
#undef file_dentry
#define file_dentry(f) kcompat_file_dentry(f)
#endif

/*
 * The file_inode() inline function was added in kernel 3.9.0.
 * Emulate it for earlier kernels.
 */
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
static inline struct inode *kcompat_file_inode(struct file *f)
{
    return file_dentry(f)->d_inode;
}
#undef file_inode
#define file_inode(f)   kcompat_file_inode(f)
#endif

Yes, there's a better way: don't bother looking at the dentry when what you want is right there as a field of struct file .

struct file {
    union {
        struct llist_node   fu_llist;
        struct rcu_head     fu_rcuhead;
    } f_u;
    struct path     f_path;
    struct inode    *f_inode; // <-- here's your inode
    // ...
}

You can either access f->f_inode directly or use the file_inode() function, this way you can also avoid kernel version checks.

static long unlocked_ioctl(struct file *f, unsigned int o, unsigned long d)
{
    int minor = iminor(file_inode(f));
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM