file_operation, inode, file

File _operations结构体

       file _operation就 把系统调用和驱动程序关联起来的关键数据结构 (file_operation中的函数可以认为是底层驱动的接口)。 这个结构 的每一个成员都对应着一个系统调用。读取file _operation中相应的函数指针,接着把控制权转交给函数,从而完成了Linux设备驱动程序的工作。

    在系统内部,I/O设备的存取操作通过特定的入口点来进行,而这组特定的入口点恰恰 由设备驱动程序提供的。通常这组设备驱动程序接口 由结构file _operations结构体 向系统说明的,它定义在include/linux/fs.h中。

传统上, 一个 file _operation 结构或者其一个指针称为 fops( 或者它的一些变体). 结构中的每个成员必须指向驱动中的函数, 这些函数实现一个特别的操作(read/write/ioctl等), 或者对于不支持的操作留置为 NULL. 当指定为 NULL 指针时内核的确切的行为 每个函数不同的。

在你通读 file _operations 方法的列表时, 你会注意到不少参数包含字串 __user. 这种注解 一种文档形式, 注意, 一个指针 一个不能被直接引用的用户空间地址. 对于正常的编译, __user 没有效果, 但 它可被外部检查软件使用来找出对用户空间地址的错误使用。

注册设备编号仅仅 驱动代码必须进行的诸多任务中的第一个。首先需要涉及一个别的,大部分的基础性的驱动操作包括 3 个重要的内核数据结构,称为 file _operations,file ,和 inode 。需要对这些结构的基本了解才能够做大量感兴趣的事情。

    struct file _operations 一个字符设备把驱动的操作和设备号联系在一起的纽带(通过cdev_add()函数实现), 一系列指针的集合,每个被打开的文件都对应于一系列的操作,这就 file _operations,用来执行一系列的系统调用。
    struct file 代表一个打开的文件,在执行file _operation中的open操作时被创建,这里需要注意的 与用户空间inode指针的区别,一个在内核,而file 指针在用户空间,由c库来定义。
    struct inode 被内核用来代表一个文件,注意和struct file 的区别,struct inode一个 代表文件,struct file 一个 代表打开的文件
struct
inode包括很重要的二个成员:
dev_t       i_rdev   设备文件的设备号
struct cdev *i_cdev 代表字符设备的数据结构
struct
inode结构 用来在内核内部表示文件的.同一个文件可以被打开好多次,所以可以对应很多struct file ,但 只对应一个struct inode.

 

       对于一种底层硬件设备来说,只有一个file_operation结构体,但可以有多个设备cdev(对应多个inode结构体,一个cdev对应一个inode),但这些设备共用一个file_operation.(结合globalmem来总结的)。

 

内核中用inode结构表示具体的文件,而用file结构表示打开的文件描述符。Linux2.6.27内核中,inode结构体具体定义如下:
struct inode
{
struct hlist_node   i_hash;
struct list_head   i_list;
struct list_head   i_sb_list;
struct list_head   i_dentry;
unsigned long       i_ino;
atomic_t       i_count;
unsigned int       i_nlink;
uid_t           i_uid;       
/*inode的id*/
gid_t           i_gid;       /*inode的组id*/
dev_t           i_rdev; //该成员表示设备文件的inode结构,它包含了真正的设备编号。
u64           i_version;
loff_t           i_size;  
/*inode多代表的文件的大小*/
#ifdef __NEED_I_SIZE_ORDERED
seqcount_t       i_size_seqcount;
#endif
struct timespec       i_atime;       
/*inode最后一次存取的时间*/
struct timespec       i_mtime;       /*inode最后一次修改的时间*/
struct timespec       i_ctime;       /*inode的创建时间*/
unsigned int       i_blkbits;          /*inode在做I/O时的区块大小*/
blkcnt_t       i_blocks;                 /*inode所石油的block块数*/
unsigned short          i_bytes;
umode_t           i_mode;   
/*inode的权限*/
spinlock_t       i_lock;   /* i_blocks, i_bytes, maybe i_size */
struct mutex       i_mutex;
struct rw_semaphore   i_alloc_sem;
const struct inode_operations   *i_op;
const struct file_operations   *i_fop;   /* former ->i_op->default_file_ops */
struct super_block   *i_sb;
struct file_lock   *i_flock;
struct address_space   *i_mapping;
struct address_space   i_data;
#ifdef CONFIG_QUOTA
struct dquot       *i_dquot[MAXQUOTAS];
#endif
struct list_head   i_devices;   
/*若是字符设备,为其对应的cdev结构体指针*/
union {
struct pipe_inode_info   *i_pipe;
struct block_device   *i_bdev;    
/*若是块设备,为其对应的cdev结构体指针*/
struct cdev       *i_cdev; //该成员表示字符设备的内核的 内部结构。当inode指向一个字符设备文件时,该成员包含了指向struct cdev结构的指针,其中cdev结构是字符设备结构体。
};
int           i_cindex;

__u32           i_generation;

#ifdef CONFIG_DNOTIFY
unsigned long       i_dnotify_mask; /* Directory notify events */
struct dnotify_struct   *i_dnotify; /* for directory notifications */
#endif

#ifdef CONFIG_INOTIFY
struct list_head   inotify_watches; /* watches on this inode */
struct mutex       inotify_mutex;   /* protects the watches list */
#endif

unsigned long       i_state;
unsigned long       dirtied_when;   /* jiffies of first dirtying */

unsigned int       i_flags;

atomic_t       i_writecount;
#ifdef CONFIG_SECURITY
void           *i_security;
#endif
void           *i_private; /* fs or device private pointer */
};

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值