Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

338
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda