谢谢你看这篇文章。我正在尝试修补网络块设备驱动程序。如果您需要查看它们的来源,请访问 http://code.ximeta.com。
我注意到 lock_kernel() 从 linux 2.6.37 开始似乎已被弃用。我阅读了“ioctl() 的新方法”,发现设备驱动程序现在应该在操作之前执行特定的锁定。
因此,如果可能的话,我想要一些建议来代替它。
我在块文件夹部分中找到了我认为相关的当前代码中的两个部分。
Source
block->io.c
->ctrldev.c
我把每个片段的片段供你考虑。
io.c 包含一个对 lock_kernel 的调用:
NDAS_SAL_API xbool sal_file_get_size(sal_file file, xuint64* size)
{
definitions and declarations etc..
lock_kernel();
#ifdef HAVE_UNLOCKED_IOCTL
if (filp->f_op->unlocked_ioctl) {
some small statements
error = filp->f_op->unlocked_ioctl(filp, BLKGETSIZE64, (unsigned long)size);
actions if error or not etc.
}
#endif
unlock_kernel();
return ret;
}
而 ctrldev.c 包含主要的 io 函数:
#include <linux/spinlock.h> // spinklock_t
#include <linux/semaphore.h> // struct semaphore
#include <asm/atomic.h> // atomic
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/ide.h>
#include <linux/smp_lock.h>
#include <linux/time.h>
......
int ndas_ctrldev_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
lots of operations and functions.
return result;
}
后来 ndas_ctrldev_ioctl 函数设置为以前的 .ioctl。
static struct file_operations ndasctrl_fops = {
.write = ndas_ctrldev_write,
.read = ndas_ctrldev_read,
.open = ndas_ctrldev_open,
.release = ndas_ctrldev_release,
.ioctl = ndas_ctrldev_ioctl,
};
现在我想转换它以避免使用 lock_kernel();
根据我的理解,我将对前面的部分进行如下修改:
NDAS_SAL_API xbool sal_file_get_size(sal_file file, xuint64* size)
{
definitions and declarations etc..
#ifndef HAVE_UNLOCKED_IOCTL
lock_kernel();
#endif
#ifdef HAVE_UNLOCKED_IOCTL
if (filp->f_op->unlocked_ioctl) {
some small statements
error = filp->f_op->unlocked_ioctl(filp, BLKGETSIZE64, (unsigned long)size);
actions if error or not etc.
}
#endif
#ifndef HAVE_UNLOCKED_IOCTL
unlock_kernel();
#endif
return ret;
}
#ifdef HAVE_UNLOCKED_IOCTL
long ndas_ctrldev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
#else
int ndas_ctrldev_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
#endif
{
#ifdef HAVE_UNLOCKED_IOCTL
! add some sort of lock here !
#endif
lots of operations and functions.
#ifdef HAVE_UNLOCKED_IOCTL
! add unlock statement here !
#endif
return result;
}
static struct file_operations ndasctrl_fops = {
.write = ndas_ctrldev_write,
.read = ndas_ctrldev_read,
.open = ndas_ctrldev_open,
.release = ndas_ctrldev_release,
#ifdef HAVE_UNLOCKED_IOCTL
.unlocked_ioctl = ndas_ctrldev_ioctl,
#else
.ioctl = ndas_ctrldev_ioctl,
#endif
};
所以,我想请教以下建议。
这看起来像正确的程序吗?
我是否理解将锁移动到 io 函数中是否正确?
根据 crtrldev.c 中包含的内容,您能推荐任何您头顶上的锁吗?(我试图研究其他一些处理 filp 和 lock_kernel 的驱动程序,但我太菜鸟了,无法立即找到答案。)