struct file & struct inode解析

来源:互联网 发布:thomson one数据库 编辑:程序博客网 时间:2024/04/29 22:35

内核中用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 */
};
2、struct file ──字符设备驱动相关重要结构

文件结构代表一个打开的文件描述符,它不是专门给驱动程序使用的,系统中每一个打开的文件在内核中都有一个关联的struct file。它由内核在open时创建,并传递给在文件上操作的任何函数,知道最后关闭。当文件的所有实例都关闭之后,内核释放这个数据结构。
struct file {
/*
* fu_list becomes invalid after file_free is called and queued via
* fu_rcuhead for RCU freeing
*/
union {
struct list_head    fu_list;
struct rcu_head     fu_rcuhead;
} f_u;
struct path        f_path;
#define f_dentry    f_path.dentry   //该成员是对应的 目录结构 。
#define f_vfsmnt    f_path.mnt
const struct file_operations    *f_op; //该操作 是定义文件关联的操作的。内核在执行open时对这个 指针赋值。
atomic_long_t        f_count;
unsigned int         f_flags; //该成员是文件标志。
mode_t            f_mode;
loff_t            f_pos;
struct fown_struct    f_owner;
unsigned int        f_uid, f_gid;
struct file_ra_state    f_ra;

u64            f_version;
#ifdef CONFIG_SECURITY
void            *f_security;
#endif
/* needed for tty driver, and maybe others */
void            *private_data;//该成员是系统调用时保存状态信息非常有用的资源。

#ifdef CONFIG_EPOLL
/* Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head    f_ep_links;
spinlock_t        f_ep_lock;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space    *f_mapping;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};

原创粉丝点击