2021SC@SDUSC
接上一篇的acl.h文件,今天来看ext4源码的acl.c文件部分,主要是一些acl权限控制的具体实现。
首先需要说一下里面两个比较重要的结构体:acl结构体和posix结构体。鉴于acl结构体在上一篇已经讲过,所以以下部分只说一下posix结构体的实现:
struct posix_acl {
/*引用计数*/
atomic_t a_refcount;
/*内部有几个acl项*/
unsigned int a_count;
/*实际数据区*/
struct posix_acl_entry a_entries[0];
};
/*acl项结构体*/
struct posix_acl_entry {
short e_tag;
unsigned short e_perm;
unsigned int e_id;
};
注意到这里的一个细节:定义了 a_entries[0] 。在Linux内核中,posix结构的开头是 posix_acl结构体,后面有几个控制项,就连接几个posix_acl_entry结构体,这里提前定义好的a_entries[0]既不占用空间,又方便了访问后面的acl项(比如想访问第一个:p->a_entries[0],后面的acl项在内存上是连续的)。
下面直接上acl.c的源码:
/*
* linux/fs/ext4/acl.c
*
* Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
*/
#include <linux/quotaops.h>
#include "ext4_jbd2.h"
#include "ext4.h"
#include "xattr.h"
#include "acl.h"
/*把硬盘上的ext4的acl数据转成内存上的posix标准的acl数据
ext4文件系统的acl格式数据是开头是一个ext4_acl_header结构体的ext4头部,然后接下来是ext4_acl_entry_short或者是ext4_acl_entry结构体的acl实体
内存存储的posix标准的acl数据是开头是一个posix_acl结构体,然后随后就是count个posix_acl_entry结构体
*/
static struct posix_acl*
ext4_acl_from_disk(const void* value, size_t size)
{
const char* end = (char*)value + size;
int n, count;
struct posix_acl* acl;
/*如果参数是NULL,直接返回NULL*/
if (!value)
return NULL;
/*如果大小小于ext4_acl_header大小,说明有错误*/
if (size <