如果你認爲本系列文章對你有所幫助,請大家有錢的捧個錢場,點擊此處贊助,贊助額0.1元起步,多少隨意
聲明:本文只用於個人學習交流,若不慎造成侵權,請及時聯繫我,立即予以改正
鋒影
email:[email protected]
POSIX-Layer Data Structures
這篇文章主要講述和POSIX-Layer例程支持相關的幾個關鍵數據結構。
1. Introduction
資源管理器定義了三個與POSIX-Layer相關的數據結構:
iofunc_ocb_t
,包含了每次打開的數據,比如打開文件的偏移等;iofunc_attr_t
,資源管理器有時可能得管理多個設備,比如devc-ser*
對應到/dev/ser1
,/dev/ser2
等,這個結構包含一個名字對應的數據;iofunc_mount_t
,主要用於文件系統,設備通常不需要這個結構;
三者之間的關係如下:A resource manager is responsible for three data structures
Multiple clients with multiple OCBs, all linked to one mount structure
2. iofunc_ocb_t結構
OCB
(Open Control Block)結構,用於維護客戶端和資源管理器之間特定會話的狀態信息,在open()
時創建,在close()
時退出。這個數據結構用於iofunc layer
中的幫助函數。該結構至少包含以下內容:
typedef struct _iofunc_ocb {
IOFUNC_ATTR_T *attr;
int32_t ioflag;
off_t offset;
uint16_t sflag;
uint16_t flags;
} iofunc_ocb_t;
attr
,指向屬性結構體;ioflag
,包含了資源打開模式:O_RDONLY
/O_RDWR
/O_WRONLY
,對應的ioflag
值爲_IO_FLAG_RD
/_IO_FLAG_RD | _IO_FLAG_WR
/_IO_FLAG_WR
,這些信息從io_connect_t
結構體中繼承得來;offset
,讀寫資源的偏移,資源管理器可以修改這個成員;sflag
,定義共享模式,從io_connect_t
結構中繼承得來;flags
,當設置IOFUNC_OCB_PRIVILEGED
位時,特權進程來執行open()
操作,此外也可以使用IOFUNC_OCB_FLAGS_PRIVATE
範圍之內的flag。資源管理器可以修改這個成員;
3. iofunc_attr_t結構
這個結構爲資源管理器提供了設備的特徵,與OCB
結構結合使用。
typedef struct _iofunc_attr {
IOFUNC_MOUNT_T *mount;
uint32_t flags;
int32_t lock_tid;
uint16_t lock_count;
uint16_t count;
uint16_t rcount;
uint16_t wcount;
uint16_t rlocks;
uint16_t wlocks;
struct _iofunc_mmap_list *mmap_list;
struct _iofunc_lock_list *lock_list;
void *list;
uint32_t list_size;
off_t nbytes;
ino_t inode;
uid_t uid;
gid_t gid;
time_t mtime;
time_t atime;
time_t ctime;
mode_t mode;
nlink_t nlink;
dev_t rdev;
} iofunc_attr_t;
mount
,指向掛載結構體;flags
,可以是不同比特位的組合,包括:IOFUNC_ATTR_ATIME/IOFUNC_ATTR_CTIME/IOFUNC_ATTR_DIRTY_NLINK/IOFUNC_ATTR_DIRTY_MODE/IOFUNC_ATTR_DIRTY_OWNER/IOFUNC_ATTR_DIRTY_RDEV/IOFUNC_ATTR_DIRTY_SIZE/IOFUNC_ATTR_DIRTY_TIME/IOFUNC_ATTR_MTIME
,用於標識修改記錄。lock_tid
和lock_count
,用於多線程的加鎖和統計;count, rcount, wcount, rlocks and wlocks
,計數值及鎖;mmap_list and lock_list
,mmap_list
用於iofunc_mmap()
和iofunc_mmap_default()
函數,lock_list
用於iofunc_lock_default()
函數,通常用戶不需要修改會檢查這兩個成員;list and list_size
,保留字段;nbytes
,資源的字節數,比如對於文件來說,放置的就是文件的大小;inode
,特定掛載點的inode,每個掛載點都必須是唯一的;uid and gid
,資源的用戶ID和組ID,通常由chown()
等函數來更新;mtime, atime, and ctime
,修改時間、訪問時間,以及狀態改變時間,是三個POSIX時間成員;mode
,資源的模式,定義在<sys/stat.h>
中,以S_*
開頭;nlink
,鏈接數量;rdev
,包含字符特殊設備的設備號,以及指定專用設備的rdev
號;
4. iofunc_mount_t結構(可選)
這個結構中的成員,尤其是conf
和flags
,可以修改某些iofunc
層函數的行爲。這個結構中至少包含以下內容:
typedef struct _iofunc_mount {
uint32_t flags;
uint32_t conf;
dev_t dev;
int32_t blocksize;
iofunc_funcs_t *funcs;
} iofunc_mount_t;
flags
,包含一個相關的位用於標識資源管理器使用的偏移量是32-bit的(與擴展的64-bit偏移相反);conf
,包含以下位:IOFUNC_PC_CHOWN_RESTRICTED/IOFUNC_PC_NO_TRUNC/IOFUNC_PC_SYNC_IO/IOFUNC_PC_LINK_DIR/IOFUNC_PC_ACL
,這些選項是由iofunc
層_IO_PATHCONF
默認處理程序返回的;dev
,包含文件系統對應的設備號,當客戶端調用stat()
函數時,會將該值填充到struct stat st_dev
成員中;blocksize
,包含了設備的塊大小;funcs
,這是一個struct _iofunc_funcs
結構,用於擴展OCB;