Linux檔案系統

【星星之火】發表於2020-10-03

引入:VFS

VFS(Virtual Filesystem Switch)稱為虛擬檔案系統或虛擬檔案系統轉換,是一個核心軟體層,在具體的檔案系統之上抽象的一層,用來處理與Posix檔案系統相關的所有呼叫,給各種檔案系統提供一個通用的介面,使上層的應用程式能夠使用通用的介面訪問不同檔案系統,同時也為不同檔案系統的通訊提供了媒介

VFS提供了一個抽象層,將POSIX API介面與不同儲存裝置的具體介面實現進行了分離,使得底層的檔案系統型別、裝置型別對上層應用程式透明。

linux下“一切皆檔案”是Unix/Linux的基本哲學之一。unix標準檔案:普通檔案,目錄檔案,符號連結檔案,裝置檔案,管道檔案

通用檔案模型

 

資料型別Superblockinodedentryfile
功能儲存檔案系統後設資料檔案相關的後設資料檔案(目錄)名稱和具體的inode的對應關係,快取最近最常訪問的檔案或目錄,提示系統效能 
儲存內容檔案系統型別、大小、狀態(存放於磁碟檔案系統控制塊)檔案的所有者(使用者、組)、訪問時間、檔案型別等(存放於磁碟檔案控制塊)  
是否儲存在介質中
何時建立mount時從介質中讀取,並常駐記憶體   
重要成員

 s_list、

 

   
涉及操作函式*s_op   
維護所有SB物件以雙向迴圈連結串列連結在一起   
是否快取   

 

Superblock

其成員相關資訊在介質中儲存,mount時讀取介質資訊並在記憶體中建立。

結構體定義

struct super_block {
	struct list_head	s_list;		/* 支援掛載多個檔案系統,並將super_block鏈起來 */
	dev_t			s_dev;		/* search index; _not_ kdev_t */
	unsigned char		s_blocksize_bits;
	unsigned long		s_blocksize;
	loff_t			s_maxbytes;	/* Max file size */
	struct file_system_type	*s_type; /* 檔案系統型別 */
	const struct super_operations	*s_op; /* 檔案系統相關操作函式 */
	const struct dquot_operations	*dq_op;
	const struct quotactl_ops	*s_qcop;
	const struct export_operations *s_export_op;
	unsigned long		s_flags;
	unsigned long		s_magic;
	struct dentry		*s_root;/* 該檔案系統的根目錄 */
	struct rw_semaphore	s_umount;
	int			s_count;
	atomic_t		s_active;
#ifdef CONFIG_SECURITY
	void                    *s_security;
#endif
	const struct xattr_handler **s_xattr;

	struct list_head	s_inodes;	/* all inodes */
	struct hlist_bl_head	s_anon;		/* anonymous dentries for (nfs) exporting */
	struct list_head	s_mounts;	/* list of mounts; _not_ for fs use */
	struct block_device	*s_bdev;
	struct backing_dev_info *s_bdi;
	struct mtd_info		*s_mtd;
	struct hlist_node	s_instances;
	unsigned int		s_quota_types;	/* Bitmask of supported quota types */
	struct quota_info	s_dquot;	/* Diskquota specific options */

	struct sb_writers	s_writers;

	char s_id[32];				/* Informational name */
	u8 s_uuid[16];				/* UUID */

	void 			*s_fs_info;	/* Filesystem private info */
	unsigned int		s_max_links;
	fmode_t			s_mode;

	/* Granularity of c/m/atime in ns.
	   Cannot be worse than a second */
	u32		   s_time_gran;

	/*
	 * The next field is for VFS *only*. No filesystems have any business
	 * even looking at it. You had been warned.
	 */
	struct mutex s_vfs_rename_mutex;	/* Kludge */

	/*
	 * Filesystem subtype.  If non-empty the filesystem type field
	 * in /proc/mounts will be "type.subtype"
	 */
	char *s_subtype;

	/*
	 * Saved mount options for lazy filesystems using
	 * generic_show_options()
	 */
	char __rcu *s_options;
	const struct dentry_operations *s_d_op; /* default d_op for dentries */

	/*
	 * Saved pool identifier for cleancache (-1 means none)
	 */
	int cleancache_poolid;

	struct shrinker s_shrink;	/* per-sb shrinker handle */

	/* Number of inodes with nlink == 0 but still referenced */
	atomic_long_t s_remove_count;

	/* Being remounted read-only */
	int s_readonly_remount;

	/* AIO completions deferred from interrupt context */
	struct workqueue_struct *s_dio_done_wq;
	struct hlist_head s_pins;

	/*
	 * Keep the lru lists last in the structure so they always sit on their
	 * own individual cachelines.
	 */
	struct list_lru		s_dentry_lru ____cacheline_aligned_in_smp;
	struct list_lru		s_inode_lru ____cacheline_aligned_in_smp;
	struct rcu_head		rcu;

	/*
	 * Indicates how deep in a filesystem stack this SB is
	 */
	int s_stack_depth;
};

重要成員

型別型別名成員名作用
structlist_heads_list支援掛載多個檔案系統,並將super_block鏈起來
structdentrys_root該檔案系統的根目錄
structsuper_operations*s_op檔案系統相關操作函式
  *s_d_op 
structfile_system_type*s_type檔案系統型別名稱,mount時指定型別名
void *s_fs_info指向具體檔案系統的超級塊資訊
    
    
    
    

 

Index node(inode)

靜態:建立檔案系統時生成inode,儲存在具體儲存裝置上,記錄了檔案系統的元資訊;

動態:VFS在記憶體中使用inode資料結構,來管理檔案系統的檔案物件,記錄了檔案物件的詳細資訊,部分欄位與關聯的檔案物件有關,會動態建立。

 

結構體定義

 

/*
 * Keep mostly read-only and often accessed (especially for
 * the RCU path lookup and 'stat' data) fields at the beginning
 * of the 'struct inode'
 */
struct inode {
	umode_t			i_mode;
	unsigned short		i_opflags;
	kuid_t			i_uid;
	kgid_t			i_gid;
	unsigned int		i_flags;

#ifdef CONFIG_FS_POSIX_ACL
	struct posix_acl	*i_acl;
	struct posix_acl	*i_default_acl;
#endif

	const struct inode_operations	*i_op;
	struct super_block	*i_sb;
	struct address_space	*i_mapping;

#ifdef CONFIG_SECURITY
	void			*i_security;
#endif

	/* Stat data, not accessed from path walking */
	unsigned long		i_ino;
	/*
	 * 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;
	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;

#ifdef __NEED_I_SIZE_ORDERED
	seqcount_t		i_size_seqcount;
#endif

	/* Misc */
	unsigned long		i_state;
	struct mutex		i_mutex;

	unsigned long		dirtied_when;	/* jiffies of first dirtying */
	unsigned long		dirtied_time_when;

	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;
		struct rcu_head		i_rcu;
	};
	u64			i_version;
	atomic_t		i_count;
	atomic_t		i_dio_count;
	atomic_t		i_writecount;
#ifdef CONFIG_IMA
	atomic_t		i_readcount; /* struct files open RO */
#endif
	const struct file_operations	*i_fop;	/* former ->i_op->default_file_ops */
	struct file_lock_context	*i_flctx;
	struct address_space	i_data;
	struct list_head	i_devices;
	union {
		struct pipe_inode_info	*i_pipe;
		struct block_device	*i_bdev;
		struct 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

	void			*i_private; /* fs or device private pointer */
};

 

重要成員

 

型別型別名成員名作用
    
structinode_operations*i_op索引節點操作函式列表
structsuper_block*i_sb指向所屬的超級塊物件
long\i_ino索引節點編號
short\i_bytes

最後一個塊的有效位元組數

int i_blkbits每個塊所佔位數
blkcnt_t i_blocks檔案所佔塊數
    

 

 

Directory entry(dentry)

 

結構體定義

struct dentry {
	/* RCU lookup touched fields */
	unsigned int d_flags;		/* protected by d_lock */
	seqcount_t d_seq;		/* per dentry seqlock */
	struct hlist_bl_node d_hash;	/* lookup hash list */
	struct dentry *d_parent;	/* parent directory */
	struct qstr d_name;
	struct inode *d_inode;		/* Where the name belongs to - NULL is
					 * negative */
	unsigned char d_iname[DNAME_INLINE_LEN];	/* small names */

	/* Ref lookup also touches following */
	struct lockref d_lockref;	/* per-dentry lock and refcount */
	const struct dentry_operations *d_op;
	struct super_block *d_sb;	/* The root of the dentry tree */
	unsigned long d_time;		/* used by d_revalidate */
	void *d_fsdata;			/* fs-specific data */

	struct list_head d_lru;		/* LRU list */
	struct list_head d_child;	/* child of parent list */
	struct list_head d_subdirs;	/* our children */
	/*
	 * d_alias and d_rcu can share memory
	 */
	union {
		struct hlist_node d_alias;	/* inode alias list */
	 	struct rcu_head d_rcu;
	} d_u;
};

重要成員

型別型別名成員名作用
int \d_flags標記,如是否為掛載點
  d_hashhash連結串列
structdentry d_parent當前目錄項的父目錄項
unsigned char d_iname檔名
struct qstr d_name

 

長檔名

 

  d_inode與此dentry關聯的檔案inode
struct list_head d_subdirs子目錄項鍊表
struct super_block *d_sb所屬檔案系統超級塊

File
 

結構體定義

重要成員

型別型別名成員名作用
    
    
    
    
   

 

 

 

 

檔案系統分析

 

 

 

 

https://blog.csdn.net/jasonchen_gbd/article/details/51511261

https://blog.csdn.net/jinking01/article/details/90669534

相關文章