linux-kernel - open 对普通文件和设备驱动程序如何工作

标签 linux-kernel operating-system filesystems linux-device-driver system-calls

目前,我正在学习 Linux 设备驱动程序。并被困在打开设备文件的工作原理上?

到目前为止我得到了什么......
考虑一个打开普通文件的简单代码..

#incldue<stdio.h>
int main() {
   FILE fp;
   char buffer[20];
   fp = fopen(/home/yoggi/foo.txt, "r");
   fread(buffer, 5, 1, fp);
}

在上面的程序中,c-library 函数 fopen() 是系统调用 的包装函数。打开() , 实习生叫 VFS 层中的 sys_open() 或 file_open() 功能。由于linux支持多个文件系统,虚拟文件​​系统将控制权交给实际的文件系统处理程序来打开那个文件。
1) How does virtual file system(VFS) get to know on which file system the 
   underline file resides?
2) How does it then calls the file_open or open function of that particular
   filesystem to open file.

在设备驱动程序的情况下,会发生类似的事情。假设一个简单的设备驱动程序。
#include <linux/module.h>
// othher includes... 
static dev_t first; // Global variable for the first device number 
static struct cdev c_dev; // Global variable for the character device structure
static struct class *cl; // Global variable for the device class
static int my_open(struct inode *i, struct file *f)
{
   printk(KERN_INFO "Driver: open()\n");
   return 0;
} 
static ssize_t my_read(struct file *f, char __user *buf, size_t len, loff_t *off)
{
   printk(KERN_INFO "Driver: read()\n");
   return 0;
}
struct file_operations pugs_fops =
{
 .owner = THIS_MODULE,
 .open = my_open,
 .read = my_read,
};

static int __init ofcd_init(void) /* Constructor */
{
  printk(KERN_INFO "Namaskar: ofcd registered");
  if (alloc_chrdev_region(&first, 0, 1, "Shweta") < 0)
  {
    return -1;
  }
  if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL)
  {
     unregister_chrdev_region(first, 1);
     return -1;
 }
 if (device_create(cl, NULL, first, NULL, "mynull") == NULL)
 {
    class_destroy(cl);
    unregister_chrdev_region(first, 1);
    return -1;
 }
   cdev_init(&c_dev, &pugs_fops);
 if (cdev_add(&c_dev, first, 1) == -1)
 {
   device_destroy(cl, first);
   class_destroy(cl);
   unregister_chrdev_region(first, 1);
   return -1;
 }
  return 0;
}

static void __exit ofcd_exit(void) /* Destructor */
{
 cdev_del(&c_dev);
 device_destroy(cl, first);
 class_destroy(cl);
 unregister_chrdev_region(first, 1);
 printk(KERN_INFO "Alvida: ofcd unregistered");
} 
module_init(ofcd_init);
module_exit(ofcd_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Kumar Pugalia <email_at_sarika-pugs_dot_com>");
MODULE_DESCRIPTION("Our First Character Driver");

首先,我们为设备分配主要的次要号码。注册设备文件范围并将设备文件操作链接到设备驱动程序函数。

我没有得到的一些术语是..
1) What does actually cdev_add() do? in terms of registering a device to the 
   kernel.
2) Registering a device to the kernel means?
3) How does a open(/dev/mynull, O_RONLY); called on a device file actually calls 
   the open function of driver which is mapped while initializing the device 
   by calling routine cdev_init(&c_dev, &pugs_fops); ?

最佳答案

1) How does virtual file system(VFS) get to know on which file system the underline file resides?



您必须通过 指定您尝试打开的文件全路径名(或当前工作目录)。
因此,通过向后遍历此目录路径,挂载点的第一个匹配项(最深路径)将提供挂载的文件系统、文件系统类型和设备。

每个文件系统在挂载时都会提供此信息并保存在挂载表中。
您可以使用 mount 查看此(当前状态)信息命令。

2) How does it then calls the file_open or open function of that particular filesystem to open file.



一旦知道文件系统,ops检索该 fs 的结构,并且 open()可以调用入口点。

1) What does actually cdev_add() do? in terms of registering a device to the kernel.



驱动程序注册(例如 cdev_init() 用于字符设备)安装驱动程序的 ops列出驱动程序可以执行的功能的入口点的结构。cdev_add()通知内核驱动程序可以控制该字符设备类型的特定实例。该设备实例被分配了一个次要编号,该编号与 /dev 中的设备名称相关联。在驱动程序中声明信息。
请注意,char 以外的设备类型(例如网络或平台(总线)设备)属于不同的子系统,并使用不同的注册程序。

2) Registering a device to the kernel means?



现在已启用对该设备的访问。

3) How does a open(/dev/mynull, O_RONLY); called on a device file actually calls the open function of driver which is mapped while initializing the device by calling routine cdev_init(&c_dev, &pugs_fops); ?



司机的init()例程只应在加载驱动程序时调用一次。此例程应探测设备的所有实例的存在和运行状态。应获取中断线、DMA channel 和 I/O 端口和/或内存空间等资源。驱动程序注册其 ops带内核的结构。驱动程序向内核注册设备的每个实例。
open()用户空间中的调用由 C 库处理。 /dev设备名称被转换为设备主编号(标识哪个设备子系统或类,例如 tty 或音频,必须处理请求)和次编号(标识使用哪个设备驱动程序以及访问哪个设备实例) .处理器切换到管理模式,以便内核驱动程序的 open()可以调用例程,它是从驱动程序的 ops 中检索到的。结构体。有关 ops 的更多信息结构见这个other answer .

关于linux-kernel - open 对普通文件和设备驱动程序如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14501437/

相关文章:

linux-kernel - 一个中断处理程序可以被同一个中断处理程序抢占吗?

c - 给定路径的文件是否存在

ios - 如何回到我的项目 Xcode 10 的旧版本

c - 如何测量 linux 内核中的调度延迟?

java - 从 LKM/kernel-space 启动 Android Activity

ruby - 在 Ruby 中检测 Linux 发行版/平台

x86 - 多级分页如何节省内存?

linux - Hadoop HDFS : DateNode directory on system partition?

Linux内核: is vfs_write thread safe?

android - 如何从我的 Android 版本中删除导航栏?