您的位置:首页 > Web前端 > Node.js

struct file_operations 和 struct inode 详解

2015-08-25 14:17 549 查看
内核使用三种数据结构表示打开的文件:

(1)每个进程在进程表中都有一个记录项,记录项包含一张打开的文件描述符表,可将视为矢量,每个描述符占用一项,与每个文件描述符相关联的是:

a)文件描述符标志(close_on_exec).

b))指向一个文件表项的指针。

(2)内核为所用打开文件维持一张文件表,每个文件表项包含:

a)文件状态标志(读写等等)

b)当前文件偏移量。

c)指向该文件V节点表项的指针。

(3)每个打开文件(或设备)都有一个V-node结构。v节点包含了文件类型和对此文件进行各种操作的函数指针。对于大多数文件V节点还包含文件的i节点(索引节点)。这些信息时在打开文件时从磁盘上读入内存的。对于linux没有V节点。

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

};

1、file结构体

文件结构体代表一个打开的文件(设备对应于设备文件),系统在每次打开一个文件的时候都会创建一个file结构体表示此时打开的文件,又内核在打开时创建,当文件的所有实例被关闭的时候(关闭该文件同时引用该文件)该file结构体将被内核释放。

2、inode结构体

内核中用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;

gid_t i_gid;

dev_t i_rdev; //该成员表示设备文件的inode结构,它包含了真正的设备编号。

u64 i_version;

loff_t i_size;

#ifdef __NEED_I_SIZE_ORDERED

seqcount_t i_size_seqcount;

#endif

struct timespec i_atime;

struct timespec i_mtime;

struct timespec i_ctime;

unsigned int i_blkbits;

blkcnt_t i_blocks;

unsigned short i_bytes;

umode_t i_mode;

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;

union {

struct pipe_inode_info *i_pipe;

struct block_device *i_bdev;

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 */

};

inode 译成中文就是索引节点。每个存储设备或存储设备的分区(存储设备是硬盘、软盘、U盘 ... ... )被格式化为文件系统后,应该有两部份,一部份是inode,另一部份是Block,Block是用来存储数据用的。而inode呢,就是用来存储这些数据的信息,这些信息包括文件大小、属主、归属的用户组、读写权限等。inode为每个文件进行信息索引,所以就有了inode的数值。操作系统根据指令,能通过inode值最快的找到相对应的文件。

做个比喻,比如一本书,存储设备或分区就相当于这本书,Block相当于书中的每一页,inode 就相当于这本书前面的目录,一本书有很多的内容,如果想查找某部份的内容,我们可以先查目录,通过目录能最快的找到我们想要看的内容。

当我们用ls 查看某个目录或文件时,如果加上-i 参数,就可以看到inode节点了;比如ls -li lsfile.sh ,最前面的数值就是inode信息
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: