【转】对于struct file_operations中ioctl消失的学习笔记

很久都没有写驱动代码了,对于一些驱动相关的内核变化也没有怎么关心。这次重游《LDD3》获益良多,其值对于struct file_operations中ioctl的消失也让我长了不少见识。
当年看《LDD3》的时候已经注意到了书中对ioctl的评价不是很好:“ioctl调用的非结构化本质导致众多内核开发者倾向于放弃它。” ,而在这次阅读3.0代码的时候,这个成员在struct file_operations中早已消失了。这个激起了我学习的兴趣,以下是对这个ioctl的学习小结:

1、消失的确切时间
    ioctl的消失到底是从哪个版本开始的?网上给出的时间是2.6.36开始。网上就是这么说,但是自己必须找到代码中的证据。于是我通过git搜索主线内核代码,找到的删除ioctl的那个提交:

  1. commit b19dd42faf413b4705d4adb38521e82d73fa4249
  2. Author: Arnd Bergmann <arnd@arndb.de>
  3. Date: Sun Jul 4 00:15:10 2010 +0200
  4. bkl: Remove locked .ioctl file operation
  5. The last user is gone, so we can safely remove this
  6. Signed-off-by: Arnd Bergmann <arnd@arndb.de>
  7. Cc: John Kacur <jkacur@redhat.com>
  8. Cc: Al Viro <viro@ZenIV.linux.org.uk>
  9. Cc: Thomas Gleixner <tglx@linutronix.de>
  10. Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>

好不容易找到了这个提交,好的,这样就可以确定消失的时间了:

  1. git tag --contains b19dd42
  2. v2.6.36
  3. v2.6.36-rc1
  4. v2.6.36-rc2
  5. v2.6.36-rc3
  6. v2.6.36-rc4
  7. v2.6.36-rc5
  8. v2.6.36-rc6
  9. v2.6.36-rc7
  10. v2.6.36-rc8
  11. ......以下省略ooxx行

可以证明ioctl消失的版本是v2.6.35到v2.6.36-rc1间,于是我导出了v2.6.35到v2.6.36-rc1的补丁,果真在其中!
git diff v2.6.35..v2.6.36-rc1 > ../temp.patch

补丁过大不易上传,请自行生成。

2、消失的原因

   简单的概括:这次ioctl的消失,并不是要把ioctl清理出去,而是要逐步的清理大内核锁(BKL)。

   这个让ioctl消失的过渡期长达5年,从2005年开始内核黑客就开始替换ioctl了。具体的原因lwn.net中有一篇很好的文章:The new way of ioctl()。我将他翻译了一下:ioctl()的新方法(必看)

   当然,顺便了解一下大内核锁也是很有必要的:转载好文:《大内核锁将何去何从》

3、ioctl的替代者

    对于原来的ioctl,其实可以叫做locked ioctl。这个其实是相对于他的替代方法来讲的。我们来看看2.6.35以前在struct file_operations中有关ioctl的成员:

 1 /*
2
3 * NOTE:
4
5 * read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl
6
7 * can be called without the big kernel lock held in all filesystems.
8
9 */
10
11 struct file_operations {
12
13 struct module *owner;
14
15 loff_t (*llseek) (struct file *, loff_t, int);
16
17 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
18
19 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
20
21 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
22
23 ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
24
25 int (*readdir) (struct file *, void *, filldir_t);
26
27 unsigned int (*poll) (struct file *, struct poll_table_struct *);
28
29 int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
30 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
31 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
32
33 int (*mmap) (struct file *, struct vm_area_struct *);
34
35 int (*open) (struct inode *, struct file *);
36
37 int (*flush) (struct file *, fl_owner_t id);
38
39 int (*release) (struct inode *, struct file *);
40
41 int (*fsync) (struct file *, struct dentry *, int datasync);
42
43 int (*aio_fsync) (struct kiocb *, int datasync);
44
45 int (*fasync) (int, struct file *, int);
46
47 int (*lock) (struct file *, int, struct file_lock *);
48
49 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
50
51 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
52
53 int (*check_flags)(int);
54
55 int (*flock) (struct file *, int, struct file_lock *);
56
57 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
58
59 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
60
61 int (*setlease)(struct file *, long, struct file_lock **);
62
63 };



这个结构体其实是在过渡期的结构体,unlocked_ioctl就是ioctl的替代者。对于新的驱动,不要再使用ioctl了,而是使用unlocked_ioctl。

4、调用ioctl与unlocked_ioctl在内核代码上的不同

   其实ioctl与unlocked_ioctl所对应的系统调用都是ioctl。但是在应用层调用ioctl的时候,对于我们实现ioctl或者unlocked_ioctl有什么不同呢?这里我们可以追溯一下ioctl系统调用代码的执行过程,这里我简单的写出这个系统调用对于设备驱动(一般是设备驱动使用ioctl)的执行顺序:(fs/ioctl.c)

 SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)--->do_vfs_ioctl---> vfs_ioctl

ioctl与unlocked_ioctl的区别就体现在了这个vfs_ioctl中,我们先来看ioctl被删除前的函数:

 

/**
* vfs_ioctl - call filesystem specific ioctl methods

* @filp: open file to invoke ioctl method on
* @cmd: ioctl command to execute
* @arg: command-specific argument for ioctl

*
* Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise

* invokes filesystem specific ->ioctl method. If neither method exists,
* returns -ENOTTY.
*
* Returns 0 on success, -errno on error.

*/

static long vfs_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
int error = -ENOTTY;
if (!filp->f_op)
goto out;

if (filp->f_op->unlocked_ioctl) {
error = filp->f_op->unlocked_ioctl(filp, cmd, arg);

if (error == -ENOIOCTLCMD)
error = -EINVAL;
goto out;
} else if (filp->f_op->ioctl) {

lock_kernel();
error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
filp, cmd, arg);
unlock_kernel();

}
out:
return error;
}



从这个函数中我们可以看出:

  1. ioctl是受到大内核锁保护的,而unlocked_ioctl是直接执行的。
  2. unlocked_ioctl优先级高于ioctl,如果存在unlocked_ioctl,则执行unlocked_ioctl,否则才执行ioctl。这个优先级的产生明显是为了过渡。

而在ioctl被删除后,vfs_ioctl函数也做了相应的改变(Linux-3.0):

 

**

* vfs_ioctl - call filesystem specific ioctl methods

* @filp: open file to invoke ioctl method on
* @cmd: ioctl command to execute
* @arg: command-specific argument for ioctl
*
* Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
* returns -ENOTTY.
* Returns 0 on success, -errno on error.
*/
static long vfs_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)

{
int error = -ENOTTY;
if (!filp->f_op || !filp->f_op->unlocked_ioctl)
goto out;
error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
if (error == -ENOIOCTLCMD)
error = -EINVAL;
out:
return error;
}



5、在驱动编程时的注意事项

  • 在注册文件操作方法的结构体struct file_operations的时候原先的.ioctl=OOXX;替换为 .unlocked_ioctl=OOXX;

但是要注意ioctl和unlocked_ioctl的定义有一点不同:unlocked_ioctl少了一个inode参数。但是如果方法中真的需要其中的数据,可以通过filp->f_dentry->d_inode获得。

  • 由于失去了大内核锁的保护,所以必须unlocked_ioctl方法中自行实现锁机制,以保证不会在操作设备的时候(特别在SMP系统中)产生竞态。(也就实现了用小锁替换大锁)
posted @ 2012-03-20 10:02  Jack204  阅读(3165)  评论(0编辑  收藏  举报