建议直接从inode 获取设备号

来源:互联网 发布:android 移动网络状态 编辑:程序博客网 时间:2024/06/16 07:01

The inode Structure

The inode structure is used by the kernel internally to represent files. Therefore, it is
different from the file structure that represents an open file descriptor. There can be
numerous file structures representing multiple open descriptors on a single file, but

they all point to a single inode structure.


The inode structure contains a great deal of information about the file. As a general

rule, only two fields of this structure are of interest for writing driver code:


dev_t i_rdev;

For inodes that represent device files, this field contains the actual device number.


struct cdev *i_cdev;
struct cdev is the kernel’s internal structure that represents char devices; this
field contains a pointer to that structure when the inode refers to a char device

file.


The type of i_rdev changed over the course of the 2.5 development series, breaking a

lot of drivers. As a way of encouraging more portable programming, the kernel developers have added two macros that can be used to obtain the major and minor number from an inode:

i_rdev的类型在2.5版本中发生了改变,很多驱动因此不能运行,为了代码的移植性,内核开发者特意加了两个宏iminorimajor,让我们直接从inode中就可以获得到主次设备号:


unsigned int iminor(struct inode *inode);/* #include <linux/fs.h> */

unsigned int imajor(struct inode *inode);


In the interest of not being caught by the next change, these macros should be used

instead of manipulating i_rdev directly.

为了避免以后的变更带来的影响,我们最好使用上述的宏来获取主次号,而不是直接对inode结构中的i_rdev进行操作。ldd3.P55


0 0
原创粉丝点击