网上购物哪个平台能买到正品,青岛网站seo分析,2017年做哪个网站致富,网站制作培训班今天在微信群里有人提问#xff0c;如果创建一个文件#xff0c;创建这个文件的时间是保存在哪里的。所以就查到了这篇文章。在介绍inode结构体之前先做一个链接文件的实验#xff1a;1.创建一个普通的文件test.txt#xff0c;并写入内容查看#xff0c;如下2.创建test.tx… 今天在微信群里有人提问如果创建一个文件创建这个文件的时间是保存在哪里的。所以就查到了这篇文章。在介绍inode结构体之前先做一个链接文件的实验1.创建一个普通的文件test.txt并写入内容查看如下2.创建test.txt的硬链接文件并测试如下3.创建test.txt的软连接文件并测试如下4.ls命令查看文件相关信息如下根据现象可以发现test.txt文件的硬链接文件test_hardlink的inode号和原文件一样而它的软链接文件tesrt_softlink的inode号就和原文件不一样根据了解我们知道硬链接文件是原来文件的副本只是文件名不一样而已软连接文件是一个新的文件实际上硬链接文件在磁盘上和原文件使用的是同一个inode节点软连接文件使用不同的inode节点来管理文件。那么inode究竟是什么它在内核中处于什么的地位下面我们来介绍下内核中的inode结构大部分做了注释struct inode {umode_t i_mode;//文件的访问权限eg:rwxrwxrwxunsigned short i_opflags;kuid_t i_uid;//inode拥有者idkgid_t i_gid;//inode拥有者组idunsigned int i_flags;//inode标志可以是S_SYNC,S_NOATIME,S_DIRSYNC等#ifdef CONFIG_FS_POSIX_ACLstruct posix_acl *i_acl;struct posix_acl *i_default_acl;
#endifconst struct inode_operations *i_op;//inode操作struct super_block *i_sb;//所属的超级快/*address_space并不代表某个地址空间而是用于描述页高速缓存中的页面的一个文件对应一个address_space一个address_space与一个偏移量能够确定一个一个也高速缓存中的页面。i_mapping通常指向i_data,不过两者是有区别的i_mapping表示应该向谁请求页面i_data表示被改inode读写的页面。*/struct address_space *i_mapping;#ifdef CONFIG_SECURITYvoid *i_security;
#endif/* Stat data, not accessed from path walking */unsigned long i_ino;//inode号/** Filesystems may only read i_nlink directly. They shall use the* following functions for modification:** (set|clear|inc|drop)_nlink* inode_(inc|dec)_link_count*/union {const unsigned int i_nlink;//硬链接个数unsigned int __i_nlink;};dev_t i_rdev;//如果inode代表设备i_rdev表示该设备的设备号loff_t i_size;//文件大小struct timespec i_atime;//最近一次访问文件的时间struct timespec i_mtime;//最近一次修改文件的时间struct timespec i_ctime;//最近一次修改inode的时间spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */unsigned short i_bytes;//文件中位于最后一个块的字节数unsigned int i_blkbits;//以bit为单位的块的大小blkcnt_t i_blocks;//文件使用块的数目#ifdef __NEED_I_SIZE_ORDEREDseqcount_t i_size_seqcount;//对i_size进行串行计数
#endif/* Misc */unsigned long i_state;//inode状态可以是I_NEW,I_LOCK,I_FREEING等struct mutex i_mutex;//保护inode的互斥锁//inode第一次为脏的时间 以jiffies为单位unsigned long dirtied_when; /* jiffies of first dirtying */struct hlist_node i_hash;//散列表struct list_head i_wb_list; /* backing dev IO list */struct list_head i_lru; /* inode LRU list */struct list_head i_sb_list;//超级块链表union {struct hlist_head i_dentry;//所有引用该inode的目录项形成的链表struct rcu_head i_rcu;};u64 i_version;//版本号 inode每次修改后递增atomic_t i_count;//引用计数atomic_t i_dio_count;atomic_t i_writecount;//记录有多少个进程以可写的方式打开此文件const struct file_operations *i_fop; /* former -i_op-default_file_ops */struct file_lock *i_flock;//文件锁链表struct address_space i_data;
#ifdef CONFIG_QUOTAstruct dquot *i_dquot[MAXQUOTAS];//inode磁盘限额
#endif/*公用同一个驱动的设备形成链表比如字符设备在open时会根据i_rdev字段查找相应的驱动程序并使i_cdev字段指向找到的cdev然后inode添加到struct cdev中的list字段形成的链表中*/struct list_head i_devices;union {struct pipe_inode_info *i_pipe;//如果文件是一个管道则使用i_pipestruct block_device *i_bdev;//如果文件是一个块设备则使用i_bdevstruct cdev *i_cdev;//如果文件是一个字符设备这使用i_cdev};__u32 i_generation;#ifdef CONFIG_FSNOTIFY//目录通知事件掩码__u32 i_fsnotify_mask; /* all events this inode cares about */struct hlist_head i_fsnotify_marks;
#endif#ifdef CONFIG_IMAatomic_t i_readcount; /* struct files open RO */
#endif//存储文件系统或者设备的私有信息void *i_private; /* fs or device private pointer */
};
实际上inode是VFS使用的一个对象用于存放内核在操作文件或目录时所需要的全部信息。索引节点有两种一种是这里所说的VFS索引节点存在内存中另一种是具体文件系统的索引节点存在于磁盘上使用时将其读入内存填充VFS的索引节点之后对VFS索引节点的任何修改都将写回磁盘更新磁盘的索引节点。对于inod需要知道1对于Unix风格的文件系统来说这些信息可以从磁盘索引节点直接读入。如果一个文件 系统没有索引节点那么不管这些相关信息在磁盘上市怎么存放的文件系统都必须从中提取这些信息。没有索引的文件系统通常将文件的描述信息作为文件的一部分来存放。这些文件系统与Unix风格的文件系统不同没有将数据与控制信息分开存放。而有些现代的文件系统使用数据库来存储文件的数据。但是不管哪种情况、采用哪种方式索引节点对象必须在内存中创建以便文件系统来使用。2一个索引节点代表了文件系统的一个文件在文件创建时创建文件删除时销毁但是索引节点仅在当文件被访问时才在内存中创建且无论有多少个副本访问这个文件inode只存在一份。3inode只是用于描述文件的元数据信息并不是文件的数据文件的数据会根据inode的信息存放在一个数据块中例如test.txt文件ls -l看到的信息就是它的属性元信息“hello”数据存放在另一个数据块中。4可以简单理解为ls -l 看到的就是此文件的inode信息。5inode可以描述像普通文件、目录这样的磁盘文件他也可以描述设备或者管道这样的文件不过这些特殊的文件一般只存在inode块不分配数据块因为索引节点中有一些特殊文件相关的项比如i_pipe项就指向一个代表有名管道的数据结构i_bdev块设备结构体i_cdev指向字符设备结构体。这三个指针被放在一个共用体中因为一个给定的索引节点每次只能表示三者之一或者均不。6有时某些文件系统可能并不能完整地包含索引节点结构体所要求的所有信息。例如有的文件系统可能并不记录文件的访问时间这时该文件系统可以在实现中选择合适的办法来解决和这个问题。它可以在i_atime中存储0或者让i_atime等于i_mtime,或者只在内存中更新i_atime而不将其写回磁盘或者由文件系统 的实现者来决定。介绍完了inode接下来看看inode的操作i_opstruct inode_operations {struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);void * (*follow_link) (struct dentry *, struct nameidata *);int (*permission) (struct inode *, int);struct posix_acl * (*get_acl)(struct inode *, int);int (*readlink) (struct dentry *, char __user *,int);void (*put_link) (struct dentry *, struct nameidata *, void *);int (*create) (struct inode *,struct dentry *, umode_t, bool);int (*link) (struct dentry *,struct inode *,struct dentry *);int (*unlink) (struct inode *,struct dentry *);int (*symlink) (struct inode *,struct dentry *,const char *);int (*mkdir) (struct inode *,struct dentry *,umode_t);int (*rmdir) (struct inode *,struct dentry *);int (*mknod) (struct inode *,struct dentry *,umode_t,dev_t);int (*rename) (struct inode *, struct dentry *,struct inode *, struct dentry *);int (*setattr) (struct dentry *, struct iattr *);int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);ssize_t (*listxattr) (struct dentry *, char *, size_t);int (*removexattr) (struct dentry *, const char *);int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,u64 len);int (*update_time)(struct inode *, struct timespec *, int);int (*atomic_open)(struct inode *, struct dentry *,struct file *, unsigned open_flag,umode_t create_mode, int *opened);int (*tmpfile) (struct inode *, struct dentry *, umode_t);int (*set_acl)(struct inode *, struct posix_acl *, int);
} ____cacheline_aligned;
下面介绍常用的各种借口函数在给定的节点上可能是由VFS执行这些函数也可能由具体的文件系统执行该函数在在特定目录中寻找索引节点改索引节点要对应于dentry中给出的文件名。struct dentry * (*lookup) (struct inode *dir,struct dentry *dentry , unsigned int )
该函数在在特定目录中寻找索引节点改索引节点要对应于dentry中给出的文件名。int (*create) (struct inode *dir,struct dentry *dentry , umode_t mode, bool )
VFS通过系统调用create()和open()来调用改函数从而为dentry对象创建一个新的索引节点。在创建时使用mode指定初始模式int (link) (struct dentry *old_denrty,struct inode dir,struct dentry *dentry);
该函数被系统调用link()调用用来创建硬链接。硬链接名称由dentry指定连接对象是dir目录中old_denrty目录项所代表的文件。int (unlink) (struct inode *dir,struct dentry dentry);
该函数被系统调用ulink()调用从目录dir中删除由目录项dentry指定的索引节点对象。int (*symlink) (struct inode *dir,struct dentry *dentry ,const char *symname);
该函数被系统调用symlink()调用,创建符号链接。改符号链接的名称由symname指定连接对象是dir目录中的dentry目录项。int (*mkdir) (struct inode *dir,struct dentry *dentry ,umode_t mode);
该函数被系统调用mkdir()调用,创建一个新的目录。创建时使用mode指定初始模式。int (*rmdir) (struct inode *dir,struct dentry *dentry );
该函数被系统调用rmdir()调用,删除dir目录中的dentry 目录项代表的文件。int (*mknod) (struct inode *dir,struct dentry *dentry ,umode_t mode,dev_t rdev);
该函数被系统调用mknod()调用,创建特殊文件设备文件、命名管道或套接字。要创建的文件在dir目录中其目录项为dentry关联的设备为rdev,初始权限有mode指定。int (*rename) (struct inode *old_dir, struct dentry *old_dentry ,struct inode *new_dir ,struct dentry *new_dentry );
VFS调用该函数来移动文件。文件路径在old_dir目录中源文件由old_dentry目录项指定目标路径在new_dir中目标文件由new_dentry指定。推荐阅读专辑|Linux文章汇总专辑|程序人生专辑|C语言我的知识小密圈关注公众号后台回复「1024」获取学习资料网盘链接。欢迎点赞关注转发在看您的每一次鼓励我都将铭记于心~