linux内核 之struct file、struct inode、struct file_operations、struct cdev之间的关系

linux内核 之struct file、struct inode、struct file_operations、struct cdev之间的关系_第1张图片

                       struct cdev与struct file_operations的关系

linux内核 之struct file、struct inode、struct file_operations、struct cdev之间的关系_第2张图片

                             各种数据结构之间的关系




struct file:(表示已打开的文件)

(路径:linux-3.13.10\include\linux\fs.h)

(描述进程中打开的文件,进程中只要调用了open就有一个该对象。具体描述了打开文件的路径,权限,标志,内部偏移。file结构体是用来维护打开的文件的

struct file {

union {
struct llist_node fu_llist;
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path;
#define f_dentry f_path.dentry
struct inode *f_inode; /* cached value */
const struct file_operations *f_op;


/*
* Protects f_ep_links, f_flags, f_pos vs i_size in lseek SEEK_CUR.
* Must not be taken from IRQ context.
*/
spinlock_t f_lock;
atomic_long_t f_count;
unsigned int f_flags;
fmode_t f_mode;
loff_t f_pos;
struct fown_struct f_owner;
const struct cred *f_cred;
struct file_ra_state f_ra;


u64 f_version;

        

        …… …… ……

};

struct file,在linux/fs.h中声明,表示已打开的文件

重要的成员有struct file_operations(字符设备操作函数)struct inode(linux 节点)





struct inode:(linux节点)

(路径:linux-3.13.10\include\linux\fs.h)

struct inode {

……  ……

union {
const unsigned int i_nlink;
unsigned int __i_nlink;
};
dev_t i_rdev;
loff_t i_size;
struct timespec i_atime;
struct timespec i_mtime;
struct timespec i_ctime;
spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
unsigned short          i_bytes;
unsigned int i_blkbits;

blkcnt_t i_blocks;

…… ……

struct list_head i_devices;
union {
struct pipe_inode_info *i_pipe;
struct block_device *i_bdev;
struct cdev *i_cdev;

};

struct inode,有2个重要的成员:

dev_t   i_rdev:设备号

structcdev   *i_cdev:字符设备的地址





struct file_operations(文件操作接口)

(路径:linux-3.13.10\include\linux\fs.h)

(file_operations是一个函数指针的集合,用于存放我们定义的用于操作设备的函数的指针,如果我们不定义,它默认保留为NULL。)

struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*iterate) (struct file *, struct dir_context *);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
 loff_t len);
int (*show_fdinfo)(struct seq_file *m, struct file *f);

};



struct cdev (在linux内核中用cdev描述一个字符设备)

(路径:linux-3.13.10\include\linux\cdev.h)

#ifndef _LINUX_CDEV_H
#define _LINUX_CDEV_H

#include
#include
#include

struct file_operations;
struct inode;
struct module;

struct cdev {
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};

void cdev_init(struct cdev *, const struct file_operations *);
struct cdev *cdev_alloc(void);
void cdev_put(struct cdev *p);
int cdev_add(struct cdev *, dev_t, unsigned);
void cdev_del(struct cdev *);
void cd_forget(struct inode *);
extern struct backing_dev_info directly_mappable_cdev_bdi;

#endif


初始化字符设备的函数cdev_init。

输入参数:

structcdev*  :字符设备指针

Structfile_operations *: 字符设备操作函数指针



添加字符设备到linux内核函数cdev_add

输入参数:

structcdev*  :字符设备指针

dev_t设备号

unsigned设备数量



从linux内核中卸载字符设备cdev_del。

输入参数:

structcdev*  :字符设备指针


参考:https://blog.csdn.net/qq_27977257/article/details/54928863


你可能感兴趣的:(嵌入式linux驱动,linux,struct,file,struct,inode,struct,file_operations,struct,cdev)