第1章 FCB数据结构(文件控制块)
1. 目录项
相当于FCB次部,包括两个内容: 即文件名和inode编号, 不同的文件名可能对应同一个inode编号, 这样便对应同一个FCB主部, 即一个文件可以有多个名字.
2. inode
相当于FCB主部, 包括文件主、共享说明、地址信息等, 称为inode, inode与文件具有一对一的关系. 在UNIX文件系统中, 有一个固定的区域, 用于保存所有文件的inode.每个inode有一个唯一的编号,称为i_number。
inode定义在include/linux/fs.h中,具体如下:
struct inode {
/* RCU path lookup touches following: */
umode_t i_mode;
uid_t i_uid;
gid_t i_gid;
const struct inode_operations *i_op;
struct super_block *i_sb;
spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
unsigned int i_flags;
unsigned long i_state;
#ifdef CONFIG_SECURITY
void *i_security;
#endif
struct mutex i_mutex;
unsigned long dirtied_when; /* jiffies of first dirtying */
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 list_head i_dentry;
struct rcu_head i_rcu;
};
unsigned long i_ino;
atomic_t i_count;
unsigned int i_nlink;
dev_t i_rdev;
unsigned int i_blkbits;
u64 i_version;
loff_t i_size;
#ifdef __NEED_I_SIZE_ORDERED
seqcount_t i_size_seqcount;
#endif
struct timespec i_atime;
struct timespec i_mtime;
struct timespec i_ctime;
blkcnt_t i_blocks;
unsigned short i_bytes;
struct rw_semaphore i_alloc_sem;
const struct file_operations *i_fop; /* former ->i_op->default_file_ops */
struct file_lock *i_flock;
struct address_space *i_mapping;
struct address_space i_data;
#ifdef CONFIG_QUOTA
struct dquot *i_dquot[MAXQUOTAS];
#endif
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
#ifdef CONFIG_IMA
atomic_t i_readcount; /* struct files open RO */
#endif
atomic_t i_writecount;
#ifdef CONFIG_FS_POSIX_ACL
struct posix_acl *i_acl;
struct posix_acl *i_default_acl;
#endif
void *i_private; /* fs or device private pointer */
};
第2章 UGO与ACL的数据结构
其中,i_mode为UGO权限(User, Group, Other),其类型umode_t在include/linux/types.h中定义:
typedef unsigned short umode_t;
可见i_mode本质上就是一个16bit的空间,只有右边10bit被使用。UGO访问控制方式将文件的权限用三组3位的bit描述,即9bit,并且在最前面加上一位作为文件的类型标志。表示是文件还是目录,每类用户占3位,读、写、执行权限各用1位描述,具有权限时,该位就设置为1。读、写、执行权限分别用r、w、x三个字符表示。
i_acl和i_default_acl为ACL数据结构的指针,分别代表当前ACL和默认ACL,默认ACL只有目录具有,当目录中新建文件或目录时,新建文件会沿用父目录的默认ACL。
posix_acl等数据结构定义在/include/linux/posix_acl.h文件中
struct posix_acl_entry {
short e_tag;
unsigned short e_perm;
unsigned int e_id;
};
struct posix_acl {
atomic_t a_refcount;
unsigned int a_count;
struct posix_acl_entry a_entries[0];
};
每一个 ACL 实体由三部分组成: e_tag 、 e_id 、 e_perm 。 e_tag 表示 ACL 实体的标志,如 user:tux:rwx 中 user 就是一个 e_tag 。 e_id 是 ACL 实体限制的用户或组 id, 如 user:tux:rwx中的 tux, 在某些实体中这个项可以为空。 e_perm 说明的是具体的访问权限,主要有 rwx 三种,这和传统的 u/g/o 模式是一致的。在 Posix 标准中规定一共有 6 种 e_tag ,分别是 ACL_USER_OBJ, ACL_USER,ACL_GROUP_OBJ, ,ACL_GROUP, ACL_MASK, ACL_OTHER 。 ACL_USER_OBJ 是文件属主的 ACL 实体,ACL_GROUP_OBJ 是文件属组的 ACL 实体, ACL_MASK 是掩码 ACL 实体, ACL_OTHER 是其他用户的 ACL 实体。这四种 ( 个 )ACL 实体的 id 都为空,其他类型的 ACL 实体的 e_id 都不能为空。
第3章 相关操作
3.1 include/linux/posix_acl.c中的相关函数
posix_acl_init(struct posix_acl *acl, int count)
初始化一个ACL
struct posix_acl *posix_acl_alloc(int count, gfp_t flags)
分配一个ACL的空间
struct posix_acl *posix_acl_clone(const struct posix_acl *acl, gfp_tflags)
复制一个ACL
Int posix_acl_valid(const struct posix_acl *acl)
检查一个ACL的合法性
Int posix_acl_equiv_mode(const struct posix_acl *acl, mode_t*mode_p)
比较一个ACL与一个UGO权限
struct posix_acl *posix_acl_from_mode(mode_t mode, gfp_t flags)
从UGO权限创建一个ACL
posix_acl_permission(struct inode *inode, const struct posix_acl*acl, int want)
检查当前进程是否有访问inode的want权限
3.2 include/fs/ext4/acl.c中的相关函数
static struct posix_acl *ext4_acl_from_disk(const void *value,size_t size)
从磁盘中取出ACL
static void *ext4_acl_to_disk(const struct posix_acl *acl, size_t*size)
将ACL存入磁盘
static struct posix_acl *ext4_get_acl(struct inode *inode, int type)
从inode中取出ACL
static int ext4_set_acl(handle_t *handle, struct inode *inode, inttype, struct posix_acl *acl)
将ACL存入inode
Int ext4_check_acl(struct inode *inode, int mask, unsigned intflags)
检查当前进程是否有访问inode的mask权限,内部调用了posix_acl_permission函数。
第4章 参考资料
在Linux 内核源码中找到了相关的数据结构:
/usr/src/linux-3.0/fs/ext4/acl.h
/usr/src/linux-3.0/fs/ext4/acl.c
/usr/src/linux-3.0/include/linux/posix_acl.h
/usr/src/linux-3.0/fs/posix_acl.c