linux文件系统中的inode结构用于描述一个文件,包含文件大小、文件属性、文件状态等信息,其定义如下:
struct inode {
umode_t i_mode;
unsigned short i_opflags;
kuid_t i_uid;
kgid_t i_gid;
unsigned int i_flags;
const struct inode_operations *i_op;
struct super_block *i_sb;
struct address_space *i_mapping;
unsigned long i_ino;
union {
const unsigned int i_nlink;
unsigned int __i_nlink;
};
dev_t i_rdev;
loff_t i_size;
struct timespec64 i_atime;
struct timespec64 i_mtime;
struct timespec64 i_ctime;
spinlock_t i_lock;
unsigned short i_bytes;
u8 i_blkbits;
u8 i_write_hint;
blkcnt_t i_blocks;
unsigned long i_state;
struct rw_semaphore i_rwsem;
unsigned long dirtied_when;
unsigned long dirtied_time_when;
struct hlist_node i_hash;
struct list_head i_io_list;
struct list_head i_lru;
struct list_head i_sb_list;
struct list_head i_wb_list;
union {
struct hlist_head i_dentry;
struct rcu_head i_rcu;
};
atomic64_t i_version;
atomic64_t i_sequence;
atomic_t i_count;
atomic_t i_dio_count;
atomic_t i_writecount;
union {
const struct file_operations *i_fop;
void (*free_inode)(struct inode *);
};
struct file_lock_context *i_flctx;
struct address_space i_data;
struct list_head i_devices;
union {
struct pipe_inode_info *i_pipe;
struct cdev *i_cdev;
char *i_link;
unsigned i_dir_seq;
};
__u32 i_generation;
void *i_private;
}
inode成员
i_mode 表示inode类型以及权限,定义如下:
文件类型: #define S_IFMT 00170000 #define S_IFSOCK 0140000 #define S_IFLNK 0120000 #define S_IFREG 0100000 #define S_IFBLK 0060000 #define S_IFDIR 0040000 #define S_IFCHR 0020000 #define S_IFIFO 0010000 #define S_ISUID 0004000 #define S_ISGID 0002000 #define S_ISVTX 0001000 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) 文件权限 #define S_IRWXU 00700 #define S_IRUSR 00400 #define S_IWUSR 00200 #define S_IXUSR 00100 #define S_IRWXG 00070 #define S_IRGRP 00040 #define S_IWGRP 00020 #define S_IXGRP 00010 #define S_IRWXO 00007 #define S_IROTH 00004 #define S_IWOTH 00002 #define S_IXOTH 00001 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO) #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO) #define S_IRUGO (S_IRUSR|S_IRGRP|S_IROTH) #define S_IWUGO (S_IWUSR|S_IWGRP|S_IWOTH) #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
i_flags inode标志位,独立于文件系统的标志位,定义如下:
#define S_SYNC (1 << 0) #define S_NOATIME (1 << 1) #define S_APPEND (1 << 2) #define S_IMMUTABLE (1 << 3) #define S_DEAD (1 << 4) #define S_NOQUOTA (1 << 5) #define S_DIRSYNC (1 << 6) #define S_NOCMTIME (1 << 7) #define S_SWAPFILE (1 << 8) #define S_PRIVATE (1 << 9) #define S_IMA (1 << 10) #define S_AUTOMOUNT (1 << 11) #define S_NOSEC (1 << 12) #define S_DAX (1 << 13) #define S_ENCRYPTED (1 << 14) #define S_CASEFOLD (1 << 15) #define S_VERITY (1 << 16)
i_state inode状态位
#define I_DIRTY_SYNC (1 << 0) #define I_DIRTY_DATASYNC (1 << 1) #define I_DIRTY_PAGES (1 << 2) #define __I_NEW 3 #define I_NEW (1 << __I_NEW) #define I_WILL_FREE (1 << 4) #define I_FREEING (1 << 5) #define I_CLEAR (1 << 6) #define __I_SYNC 7 #define I_SYNC (1 << __I_SYNC) #define I_REFERENCED (1 << 8) #define __I_DIO_WAKEUP 9 #define I_DIO_WAKEUP (1 << __I_DIO_WAKEUP) #define I_linkABLE (1 << 10) #define I_DIRTY_TIME (1 << 11) #define I_WB_SWITCH (1 << 13) #define I_OVL_INUSE (1 << 14) #define I_CREATING (1 << 15) #define I_DonTCACHE (1 << 16) #define I_SYNC_QUEUED (1 << 17)
i_uid,i_gid 文件所属用户、组
i_atime/i_mtime/i_ctime inode 访问时间、数据修改时间、状态改变时间。
i_nlink 硬链接数,对这个成员的操作函数:drop_nlink、clear_nlink、set_nlink、inc_nlink
i_ino 节点号,对一个文件系统的实例,节点号唯一。
i_dev 如果是设备文件,代表设备号。获取major和minor用函数: imajor/iminor
i_size 以字节为单位的文件大小
i_blocks 文件使用的块数
i_blkbits 以bit为单位的块大小
i_bytes 文件在最后一个块中所使用的字节数.
下面几个成员用于将inode插入到相应链表:
i_sb_list superblock的inode链表 i_lru inode LRU链表 i_hash inode hash链表 i_io_list backing dev IO list i_wb_list backing dev writeback list
i_sb 指向相关联的super block.
i_dentry 与inode关联的dentry链表。
i_fop file相关的操作集
i_op inode相关的操作集,其定义如下:
struct inode_operations {
struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
const char * (*get_link) (struct dentry *, struct inode *, struct delayed_call *);
int (*permission) (struct user_namespace *, struct inode *, int);
struct posix_acl * (*get_acl)(struct inode *, int, bool);
int (*readlink) (struct dentry *, char __user *,int);
int (*create) (struct user_namespace *, struct inode *,struct dentry *,umode_t, bool);
int (*link) (struct dentry *,struct inode *,struct dentry *);
int (*unlink) (struct inode *,struct dentry *);
int (*symlink) (struct user_namespace *, struct inode *,struct dentry *,const char *);
int (*mkdir) (struct user_namespace *, struct inode *,struct dentry *,umode_t);
int (*rmdir) (struct inode *,struct dentry *);
int (*mknod) (struct user_namespace *, struct inode *,struct dentry *,umode_t,dev_t);
int (*rename) (struct user_namespace *, struct inode *, struct dentry *,struct inode *, struct dentry *, unsigned int);
int (*setattr) (struct user_namespace *, struct dentry *,struct iattr *);
int (*getattr) (struct user_namespace *, const struct path *,struct kstat *, u32, unsigned int);
ssize_t (*listxattr) (struct dentry *, char *, size_t);
int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,u64 len);
int (*update_time)(struct inode *, struct timespec64 *, int);
int (*atomic_open)(struct inode *, struct dentry *,struct file *, unsigned open_flag,umode_t create_mode);
int (*tmpfile) (struct user_namespace *, struct inode *,struct dentry *, umode_t);
int (*set_acl)(struct user_namespace *, struct inode *,struct posix_acl *, int);
int (*fileattr_set)(struct user_namespace *mnt_userns,struct dentry *dentry, struct fileattr *fa);
int (*fileattr_get)(struct dentry *dentry, struct fileattr *fa);
}



