how does open works for normal file and device drivers

社会主义新天地 提交于 2019-11-29 02:33:12
sawdust

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

You have to specify the file you're trying to open by its full pathname (or the current working directory).
So by traversing this directory path backwards, the first match (the deepest path) to a mount point will provide the mounted filesystem, type of filesystem and the device.

Each filesystem provides this information when it is mounted and is saved in the mount tables.
You can view this (current state) information using the mount command.

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

Once the filesystem is known, the ops structure for that fs is retrieved, and the open() entrypoint can be called.

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

Driver registration (e.g cdev_init() for a char device) installs the driver's ops structure which lists the entrypoints of functions that the driver can perform.
cdev_add() notifies the kernel that the driver can control a specific instance of that char device type. That device instance is assigned a minor number that associates the device name in /dev to state information in the driver.
Note that device types other than char (such a network or platform (bus) devices) belong to a different subsystem, and use different registration procedures.

2) Registering a device to the kernel means?

Access to that device is now enabled.

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); ?

The driver's init() routine should only be called once when the driver is loaded. This routine should probe for the existence and operational state of all instances of the device. Resources such as interrupt lines, DMA channels and I/O port and/or memory space should be acquired. The driver registers its ops structure with kernel. The driver registers each instance of the device with kernel.

The open() call in userspace is handled by the C library. The /dev device name is translated to the device major number (which identifies which device subsystem or class, e.g tty or audio, has to process the request) and the minor number (which identifies which device driver to use and which instance of the device is accessed). The processor is switched to supervisor mode so that the kernel driver's open() routine can be called, which is retrieved from the driver's ops structure. For a bit more on the ops structure see this other answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!