虛擬文件系統(tǒng),也不知道大家聽過沒有,反正我是聽過了!我們知道在計算機行業(yè),很多東西都不是一定有個官方說:朋友,我最大,你們做的東西,都要是這個樣子,否則是非法的。事實上,很多東西都是靠的一種實力,通過實力來慢慢在人們心中成為既定事實。這個事實同樣是沒有官方的。好了,問題來了,沒有官方,就沒有標準,沒有標準就沒有統(tǒng)一,沒有統(tǒng)一那就是三國時代,混戰(zhàn)當道也!
怎么辦?特別是百花爭鳴的文件系統(tǒng),這時linux的內核開發(fā)者們想到了VFS(虛擬文件系統(tǒng))。VFS使得用戶可以直接使用open(),read()和write()這樣的系統(tǒng)調用而不用關注具體文件系統(tǒng)和實際物理介質。也許你感覺不是很新奇啊,告訴你新奇的事情:在老式操作系統(tǒng)上(比如DOS),任何對非本地文件系統(tǒng)的訪問都必須依靠特殊工具才能完成。這種實現(xiàn)的方式是內核在它的底層文件系統(tǒng)接口上建立了一個抽象層。該抽象層是linux能夠支持各種文件系統(tǒng),即便是它們在功能和行為上存在很大差別。為了支持文件系統(tǒng),VFS提供了一個通用文件系統(tǒng)模型,該模型囊括了我們所能想到的文件系統(tǒng)的常用功能和行為。這個VFS抽象層之所以能銜接各種各樣的文件系統(tǒng),是因為它定義了所有文件系統(tǒng)都支持的基本抽象接口和數據結構,同時實際系統(tǒng)也將自身的諸如“如何打開文件”,“目錄是什么”等概念在形式上與VFS的定義保持一致。因為實際文件系統(tǒng)的代碼在統(tǒng)一的接口和數據結構隱藏了具體的實現(xiàn)細節(jié),所以在VFS層和內核的其他部分看來,所有文件系統(tǒng)都是相同的,它們都支持像文件和目錄這樣的概念,同時也支持像創(chuàng)建和刪除文件這樣的操作。
實際文件系統(tǒng)通過編程提供VFS所期望的抽象接口和數據結構,這樣,內核就可以毫不費力地和任何文件系統(tǒng)協(xié)同工作。那么接下的問題,它們直接的關系如何呢,看下邊的例子:
1
write(f,&buf,len);
該代碼不用說,應該明白。這個用戶調用首先被一個通用系統(tǒng)調用sys_write()處理,sys_write()函數要找到f所在的文件系統(tǒng)實際給出的是哪個寫操作,然后再執(zhí)行該操作。實際文件系統(tǒng)的寫方法是文件系統(tǒng)實現(xiàn)的一部分,數據最終通過該操作寫入介質。下圖給出流程:
下面我就先從整體上對unix(linux)文件系統(tǒng)做個概述,然后在具體下去。Unix使用了四種和文件系統(tǒng)相關的傳統(tǒng)抽象概念,如下:
1.文件:就是一個有序字節(jié)串。
2.目錄項:文件是放在目錄中,目錄又可以層層嵌套,形成文件路徑。路徑中的每一項就叫做目錄項。目錄是文件,這個文件列出了該目錄下的所有文件.
3.索引節(jié)點:一個文件其實是由兩部分組成:相關信息和文件本身。這里的相關信息指的是訪問控制權限,大小,擁有者,創(chuàng)建時間等。文件相關信息也叫
做元數據,被存儲在一個單獨的數據結構中,這個結構就叫做索引點(index node,簡寫inode)。
4.安裝點(掛載點):文件系統(tǒng)被安裝在一個特定的安裝點上,該安裝點在全局層次結構中被稱為命名空間,所有的已安裝文件系統(tǒng)都作為根文件樹的樹葉出
現(xiàn)在系統(tǒng)中。
5.超級塊:是一種包含文件系統(tǒng)信息的數據結構,里邊是文件系統(tǒng)的控制信息。
對應于上圖,我們知道VFS是介于用戶文件和文件系統(tǒng)之間的一個概念,所以如果文件系統(tǒng)想要穿透VFS供用戶空間使用,就必須經過封裝,提供一個符合這些概念的界面。上述每個元素都對應一個對象,該對象有屬性結構體,描述了該對象的屬性。有操作結構體,包含了自身所支持的操作,下面詳細介紹:
1.超級塊對象:代表一個已安裝的文件系統(tǒng)。由數據結構super_block結構體表示,定義在linux/fs.h中。如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
struct super_block {
struct list_head s_list; /* list of all superblocks */
dev_t s_dev; /* identifier */
unsigned long s_blocksize; /* block size in bytes */
unsigned long s_old_blocksize; /* old block size in bytes */
unsigned char s_blocksize_bits; /* block size in bits */
unsigned char s_dirt; /* dirty flag */
unsigned long long s_maxbytes; /* max file size */
struct file_system_type s_type; /* filesystem type */
struct super_operations s_op; /* superblock methods */
struct dquot_operations *dq_op; /* quota methods */
struct quotactl_ops *s_qcop; /* quota control methods */
struct export_operations *s_export_op; /* export methods */
unsigned long s_flags; /* mount flags */
unsigned long s_magic; /* filesystem's magic number */
struct dentry *s_root; /* directory mount point */
struct rw_semaphore s_umount; /* unmount semaphore */
struct semaphore s_lock; /* superblock semaphore */
int s_count; /* superblock ref count */
int s_syncing; /* filesystem syncing flag */
int s_need_sync_fs; /* not-yet-synced flag */
atomic_t s_active; /* active reference count */
void *s_security; /* security module */
struct list_head s_dirty; /* list of dirty inodes */
struct list_head s_io; /* list of writebacks */
struct hlist_head s_anon; /* anonymous dentries */
struct list_head s_files; /* list of assigned files */
struct block_device *s_bdev; /* associated block device */
struct list_head s_instances; /* instances of this fs */
struct quota_info s_dquot; /* quota-specific options */
char s_id[32]; /* text name */
void *s_fs_info; /* filesystem-specific info */
struct semaphore s_vfs_rename_sem; /* rename semaphore */
};
創(chuàng)建,管理和銷毀超級塊對象的代碼位于文件fs/super.c中,超級塊對象通過alloc_super()函數創(chuàng)建并初始化。在文件系統(tǒng)安裝時,內核會調用該函數以便從磁盤讀取文件系統(tǒng)超級塊,并且將其信息填充到內存中的超級塊對象中。其中最重要的一個是s_op,指向超級塊的操作函數表,由super_operations結構體表示,定義在linux/fs.h中,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct super_operations {
struct inode *(*alloc_inode) (struct super_block *sb);
void (*destroy_inode) (struct inode *);
void (*read_inode) (struct inode *);
void (*dirty_inode) (struct inode *);
void (*write_inode) (struct inode *, int);
void (*put_inode) (struct inode *);
void (*drop_inode) (struct inode *);
void (*delete_inode) (struct inode *);
void (*put_super) (struct super_block *);
void (*write_super) (struct super_block *);
int (*sync_fs) (struct super_block *, int);
void (*write_super_lockfs) (struct super_block *);
void (*unlockfs) (struct super_block *);
int (*statfs) (struct super_block *, struct statfs *);
int (*remount_fs) (struct super_block *, int *, char *);
void (*clear_inode) (struct inode *);
void (*umount_begin) (struct super_block *);
int (*show_options) (struct seq_file *, struct vfsmount *);
};
當文件系統(tǒng)需要對其超級塊執(zhí)行操作時,首先要在超級塊對象中尋找需要的操作方法。比如一個文件系統(tǒng)要寫自己的超級塊,需要調用:sb->s_op->write_super(sb)這里的sb是指向文件系統(tǒng)超級塊的指針,沿著該指針進入超級塊操作函數表,并從表中取得希望得到的write_super()函數,該函數執(zhí)行寫入超級塊的實際操作。
2.索引節(jié)點對象:由inode結構體表示,定義在linux/fs.h中,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
struct inode {
struct hlist_node i_hash; /* hash list */
struct list_head i_list; /* list of inodes */
struct list_head i_dentry; /* list of dentries */
unsigned long i_ino; /* inode number */
atomic_t i_count; /* reference counter */
umode_t i_mode; /* access permissions */
unsigned int i_nlink; /* number of hard links */
uid_t i_uid; /* user id of owner */
gid_t i_gid; /* group id of owner */
kdev_t i_rdev; /* real device node */
loff_t i_size; /* file size in bytes */
struct timespec i_atime; /* last access time */
struct timespec i_mtime; /* last modify time */
struct timespec i_ctime; /* last change time */
unsigned int i_blkbits; /* block size in bits */
unsigned long i_blksize; /* block size in bytes */
unsigned long i_version; /* version number */
unsigned long i_blocks; /* file size in blocks */
unsigned short i_bytes; /* bytes consumed */
spinlock_t i_lock; /* spinlock */
struct rw_semaphore i_alloc_sem; /* nests inside of i_sem */
struct semaphore i_sem; /* inode semaphore */
struct inode_operations *i_op; /* inode ops table */
struct file_operations *i_fop; /* default inode ops */
struct super_block *i_sb; /* associated superblock */
struct file_lock *i_flock; /* file lock list */
struct address_space *i_mapping; /* associated mapping */
struct address_space i_data; /* mapping for device */
struct dquot *i_dquot[MAXQUOTAS]; /* disk quotas for inode */
struct list_head i_devices; /* list of block devices */
struct pipe_inode_info *i_pipe; /* pipe information */
struct block_device *i_bdev; /* block device driver */
unsigned long i_dnotify_mask; /* directory notify mask */
struct dnotify_struct *i_dnotify; /* dnotify */
unsigned long i_state; /* state flags */
unsigned long dirtied_when; /* first dirtying time */
unsigned int i_flags; /* filesystem flags */
unsigned char i_sock; /* is this a socket? */
atomic_t i_writecount; /* count of writers */
void *i_security; /* security module */
__u32 i_generation; /* inode version number */
union {
void *generic_ip; /* filesystem-specific info */
} u;
};
有時,某些文件系統(tǒng)可能并不能完整的包含索引節(jié)點結構體要求的所有信息。舉個例子,有的文件系統(tǒng)可能并不記錄文件的創(chuàng)建時間,這時,該文件系統(tǒng)就可以在實現(xiàn)中選擇任意合適的辦法來解決這個問題,它可以在i_ctime中存儲0,或者讓i_ctime等于i_mtime,甚至任何其他值。索引節(jié)點對象中的inode_operations項存放了操作函數列表,定義在linux/fs.h中,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct inode_operations {
int (*create) (struct inode *, struct dentry *,int);
struct dentry * (*lookup) (struct inode *, struct dentry *);
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 *, int);
int (*rmdir) (struct inode *, struct dentry *);
int (*mknod) (struct inode *, struct dentry *, int, dev_t);
int (*rename) (struct inode *, struct dentry *, struct inode *, struct dentry *);
int (*readlink) (struct dentry *, char *, int);
int (*follow_link) (struct dentry *, struct nameidata *);
int (*put_link) (struct dentry *, struct nameidata *);
void (*truncate) (struct inode *);
int (*permission) (struct inode *, int);
int (*setattr) (struct dentry *, struct iattr *);
int (*getattr) (struct vfsmount *, 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 *);
};
同樣,操作調用時,用以下方式:i->i_op->truncate(i).
由于版面原因,我不得不分兩次說了,下次繼續(xù)后面有關虛擬文件系統(tǒng)的剩余部分.
接著上次的來,我今天講虛擬文件系統(tǒng)剩下的一點知識.
3.目錄項對象.目錄項的概念上節(jié)已經說了,我就不多說.目錄項中也可包括安裝點.在路徑/mnt/cdrom/foo中,/,mnt,cdrom都屬于目錄項對象。目錄項由dentry結構體表示,定義在文件linux/dcache.h中,描述如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct dentry {
atomic_t d_count; /* usage count */
unsigned long d_vfs_flags; /* dentry cache flags */
spinlock_t d_lock; /* per-dentry lock */
struct inode *d_inode; /* associated inode */
struct list_head d_lru; /* unused list */
struct list_head d_child; /* list of dentries within */
struct list_head d_subdirs; /* subdirectories */
struct list_head d_alias; /* list of alias inodes */
unsigned long d_time; /* revalidate time */
struct dentry_operations *d_op; /* dentry operations table */
struct super_block *d_sb; /* superblock of file */
unsigned int d_flags; /* dentry flags */
int d_mounted; /* is this a mount point? */
void *d_fsdata; /* filesystem-specific data */
struct rcu_head d_rcu; /* RCU locking */
struct dcookie_struct *d_cookie; /* cookie */
struct dentry *d_parent; /* dentry object of parent */
struct qstr d_name; /* dentry name */
struct hlist_node d_hash; /* list of hash table entries */
struct hlist_head *d_bucket; /* hash bucket */
unsigned char d_iname[DNAME_INLINE_LEN_MIN]; /* short name */
};
由于目錄項并非真正保存在磁盤上,所有目錄項沒有對應的磁盤數據結構,VFS根據字符串形式的路徑名現(xiàn)場創(chuàng)建它,目錄項結構體也沒有是否被修改的標志。目錄項對象有三種狀態(tài):被使用,未被使用和負狀態(tài)。一個被使用的目錄項對應一個有效的索引節(jié)點(即d_inode指向相應的索引節(jié)點)并且該對象存在一個或多個使用者(即d_count為正值)。一個未被使用的目錄項對應一個有效的索引節(jié)點(d_inode指向一個索引節(jié)點),但是VFS當前并未使用它(d_count為0)。該目錄項對象仍然指向一個有效對象,而且被保留在內存中以便需要時再使用它。顯然這樣要比重新創(chuàng)建要效率高些。一個負狀態(tài)的目錄項沒有對應的有效索引節(jié)點(d_inode為NULL).因為索引節(jié)點已被刪除了,或路徑不再正確了,但是目錄項仍然保留,以便快速解析以后的路徑查詢。雖然負的狀態(tài)目錄項有些用處,但如果需要的話話,還是可以刪除的,可以銷毀它。
結構體dentry_operation指明了VFS操作目錄的所有方法,如下:
1
2
3
4
5
6
7
8
struct dentry_operations {
int (*d_revalidate) (struct dentry *, int);
int (*d_hash) (struct dentry *, struct qstr *);
int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
int (*d_delete) (struct dentry *);
void (*d_release) (struct dentry *);
void (*d_iput) (struct dentry *, struct inode *);
};
其實,如果VFS遍歷路徑名中所有的元素并將它們逐個地解析成目錄項對象,將是一件非常耗時的事情。所以內核將目錄項對象緩存在目錄項緩存(dcache)中,目錄項緩存包括三個主要部分:
1.“被使用的”目錄項鏈表,該鏈表通過索引節(jié)點對象中的i_dentry項連接相關的索引節(jié)點,因為一個給定的索引節(jié)點可能有多個鏈接,所以就可能有多
個目錄項對象,因此用一個鏈表來連接它們。
2.“最近被使用的”雙向鏈表。該鏈表包含未被使用的和負狀態(tài)的目錄項對象。該鏈表是按時間插入的。
3. 哈希表和相應的哈希函數用來快速地將給定路徑解析為相關目錄項對象。
哈希表有數組dentry_hashtable表示,其中每一個元素都是一個指向具有相同鍵值的目錄項對象鏈表的指針。數組的大小取決于系統(tǒng)中物理內存的大小。實際的哈希值由d_hash()計算,它是內核提供給文件系統(tǒng)的唯一的一個哈希函數。查找哈希表要通過d_lookup()函數,如果該函數在dcache中發(fā)現(xiàn)了與其相匹配的目錄項對像,則匹配對象被返回;否則,返回NULL指針。dcache在一定意義上也提供了對索引節(jié)點的緩存。和目錄項對象相關的索引節(jié)點對象不會被釋放,因為目錄項會讓相關索引節(jié)點的使用計數為正,這樣就可以確保索引節(jié)點留在內存中。只要目錄項被緩存,其相應的索引節(jié)點也就被緩存了。
4.文件對象:文件對象表示進程以打開的文件。文件對象僅僅在進程觀點上代表已打開文件,它反過來指向目錄項對象(反過來指向索引節(jié)點),其實只有目錄項對象才表示已打開的實際文件。雖然一個文件對應的文件對象不是唯一的,但對應的索引節(jié)點和目錄項對象無疑是唯一的。文件對象由file結構表示,定義在文件linux/fs.h中,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct file {
struct list_head f_list; /* list of file objects */
struct dentry *f_dentry; /* associated dentry object */
struct vfsmount *f_vfsmnt; /* associated mounted fs */
struct file_operations *f_op; /* file operations table */
atomic_t f_count; /* file object's usage count */
unsigned int f_flags; /* flags specified on open */
mode_t f_mode; /* file access mode */
loff_t f_pos; /* file offset (file pointer) */
struct fown_struct f_owner; /* owner data for signals */
unsigned int f_uid; /* user's UID */
unsigned int f_gid; /* user's GID */
int f_error; /* error code */
struct file_ra_state f_ra; /* read-ahead state */
unsigned long f_version; /* version number */
void *f_security; /* security module */
void *private_data; /* tty driver hook */
struct list_head f_ep_links; /* list of eventpoll links */
spinlock_t f_ep_lock; /* eventpoll lock */
struct address_space *f_mapping; /* page cache mapping */
};
文件對象的操作有file_operations結構表示,在linux/fs.h中,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, char *, size_t, loff_t);
ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
ssize_t (*aio_write) (struct kiocb *, const char *, size_t, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int);
int (*aio_fsync) (struct kiocb *, int);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*readv) (struct file *, const struct iovec *,
unsigned long, loff_t *);
ssize_t (*writev) (struct file *, const struct iovec *,
unsigned long, loff_t *);
ssize_t (*sendfile) (struct file *, loff_t *, size_t,
read_actor_t, void *);
ssize_t (*sendpage) (struct file *, struct page *, int,
size_t, loff_t *, int);
unsigned long (*get_unmapped_area) (struct file *, unsigned long,
unsigned long, unsigned long,
unsigned long);
int (*check_flags) (int flags);
int (*dir_notify) (struct file *filp, unsigned long arg);
int (*flock) (struct file *filp, int cmd, struct file_lock *fl);
};
最后,除了以上幾種VFS基礎對象外,內核還使用了另外一些數據結構來管理文件系統(tǒng)的其它相關數據,如下:
1.file_system_type:因為linux支持眾多的文件系統(tǒng),所以內核必有由一個特殊的結構來描述每種文件系統(tǒng)的功能和行為:
1
2
3
4
5
6
7
8
9
10
11
12
struct file_system_type {
const char *name; /* filesystem's name */
struct subsystem subsys; /* sysfs subsystem object */
int fs_flags; /* filesystem type flags */
/* the following is used to read the superblock off the disk */
struct super_block *(*get_sb) (struct file_system_type *, int,char *, void *);
/* the following is used to terminate access to the superblock */
void (*kill_sb) (struct super_block *);
struct module *owner; /* module owning the filesystem */
struct file_system_type *next; /* next file_system_type in list */
struct list_head fs_supers; /* list of superblock objects */
};
其中,get_sb()函數從磁盤上讀取超級塊,并且在文件系統(tǒng)被安裝時,在內存中組裝超級塊對象,剩余的函數描述文件系統(tǒng)的屬性。每種文件系統(tǒng),不管有多少個實力安裝到系統(tǒng)中,還是根本就沒有安裝到系統(tǒng)中,都只有一個file_system_type結構。更有趣的是,當文件系統(tǒng)被實際安裝時,將有一個vfsmount結構體在安裝點被創(chuàng)建。該結構體被用來代表文件系統(tǒng)的實例----換句話說,代表一個安裝點.
2.vfsmount結構被定義在linux/mount.h中,下面是具體結構:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct vfsmount {
struct list_head mnt_hash; /* hash table list */
struct vfsmount *mnt_parent; /* parent filesystem */
struct dentry *mnt_mountpoint; /* dentry of this mount point */
struct dentry *mnt_root; /* dentry of root of this fs */
struct super_block *mnt_sb; /* superblock of this filesystem */
struct list_head mnt_mounts; /* list of children */
struct list_head mnt_child; /* list of children */
atomic_t mnt_count; /* usage count */
int mnt_flags; /* mount flags */
char *mnt_devname; /* device file name */
struct list_head mnt_list; /* list of descriptors */
struct list_head mnt_fslink; /* fs-specific expiry list */
struct namespace *mnt_namespace /* associated namespace */
};
vfs中維護的各種鏈表是為了跟蹤文件系統(tǒng)和所有其他安裝點的關系,mnt_flags保存了安裝時指定的標志信息,下表給出了標準的安裝標志:
安裝那些管理不充分信任的移動設備時,這些標志很有用處。
系統(tǒng)中每一個進程都有自己的一組打開的文件,有三個數據結構將VFS層和文件的進程緊密聯(lián)系在一起,它們分別是file_struct,fs_struct和namespace.
1.file_struct:該結構體有進程描述符中的files域指向,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct files_struct {
atomic_t count; /* structure's usage count */
spinlock_t file_lock; /* lock protecting this structure */
int max_fds; /* maximum number of file objects */
int max_fdset; /* maximum number of file descriptors */
int next_fd; /* next file descriptor number */
struct file **fd; /* array of all file objects */
fd_set *close_on_exec; /* file descriptors to close on exec() */
fd_set *open_fds; /* pointer to open file descriptors */
fd_set close_on_exec_init; /* initial files to close on exec() */
fd_set open_fds_init; /* initial set of file descriptors */
struct file *fd_array[NR_OPEN_DEFAULT]; /* default array of file objects */
};
fd數組指針指向以打開的文件對象鏈表,默認情況下,指向fd_arrar數組。NR_OPEN_DEFAULT默認是32,所以該數組可以容納32個文件對象。如果一個進程所打開的文件對象超過32個,內核將分配一個新數組,并且將fd指針指向它。這個值也是可以調整的。
2.第二個結構體是fs_struct:由進程描述符的fs域指向。它包含文件系統(tǒng)和進程相關的信息,在linux/fs_struct.h中,如下:
1
2
3
4
5
6
7
8
9
10
11
struct fs_struct {
atomic_t count; /* structure usage count */
rwlock_t lock; /* lock protecting structure */
int umask; /* default file permissions*/
struct dentry *root; /* dentry of the root directory */
struct dentry *pwd; /* dentry of the current directory */
struct dentry *altroot; /* dentry of the alternative root */
struct vfsmount *rootmnt; /* mount object of the root directory */
struct vfsmount *pwdmnt; /* mount object of the current directory */
struct vfsmount *altrootmnt; /* mount object of the alternative root */
};
該結構包含了當前進程的當前工作目錄和根目錄。
3.最后一個是namespace:由進程描述符namespace域指向,定義在linux/namespace.h中,如下:
1
2
3
4
5
6
struct namespace {
atomic_t count; /* structure usage count */
struct vfsmount *root; /* mount object of root directory */
struct list_head list; /* list of mount points */
struct rw_semaphore sem; /* semaphore protecting the namespace */
};
list域是連接已安裝文件系統(tǒng)的雙向鏈表,它包含的元素組成了全體命令空間。 上述這些數據結構都是通過進程描述符連接起來的。對多數進程來說,它們的描述符都指向唯一的files_struct和fs_struct結構體。但是,對于那些使用克隆標志CLONE_FILES或CLONE_FS創(chuàng)建的進程,會共享這兩個結構體。所以多個進程描述符可能指向同一個files_struct或fs_struct結構體。每個結構體都維護一個count域作為引用計數,它防止進程正使用該結構時,該結構被銷毀。而namespace卻不是這樣,默認情況下,所有的進程共享同樣的命名空間,也就是說,它們都看到同一個文件層層結構。只有在進行clone()操作時使用CLONE_NEWS標志,才會給進程一個另外的命名空間結構體的拷貝。