¿Hay una mejor solución para obtener el número menor?
¿Puedo evitar comprobar la versión del kernel?
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); }Sí, hay una mejor manera: no se moleste en mirar el dentry cuando lo que quiere está allí como un campo de 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 // ... } Puede acceder a f->f_inode directamente o usar la función file_inode() , de esta manera también puede evitar las comprobaciones de la versión del kernel.
static long unlocked_ioctl(struct file *f, unsigned int o, unsigned long d) { int minor = iminor(file_inode(f)); // ... }Como complemento a la respuesta de Marco Bonelli, se agregó file_inode() en el kernel 3.9, por lo que si es necesario admitir versiones anteriores del kernel, se debe agregar algún código de compatibilidad del kernel. Yo uso algo como lo siguiente:
/* * 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