Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

336
Visualizações
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);
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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));
    // ...
}
over 4 years ago · Santiago Trujillo Relatório

0

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
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda