QNX之编写资源管理器(三)
2019-01-09 本文已影响0人
Loyen
QNX相关历史文章:
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;