generic_acl.c 4.2 KB
Newer Older
1 2 3 4
/*
 * (C) 2005 Andreas Gruenbacher <agruen@suse.de>
 *
 * This file is released under the GPL.
5 6
 *
 * Generic ACL support for in-memory filesystems.
7 8 9
 */

#include <linux/sched.h>
10
#include <linux/gfp.h>
11 12
#include <linux/fs.h>
#include <linux/generic_acl.h>
13 14
#include <linux/posix_acl.h>
#include <linux/posix_acl_xattr.h>
15

16 17 18 19

static size_t
generic_acl_list(struct dentry *dentry, char *list, size_t list_size,
		const char *name, size_t name_len, int type)
20 21
{
	struct posix_acl *acl;
22
	const char *xname;
23 24
	size_t size;

25
	acl = get_cached_acl(dentry->d_inode, type);
26 27 28 29
	if (!acl)
		return 0;
	posix_acl_release(acl);

30 31 32 33 34 35 36 37 38
	switch (type) {
	case ACL_TYPE_ACCESS:
		xname = POSIX_ACL_XATTR_ACCESS;
		break;
	case ACL_TYPE_DEFAULT:
		xname = POSIX_ACL_XATTR_DEFAULT;
		break;
	default:
		return 0;
39
	}
40
	size = strlen(xname) + 1;
41
	if (list && size <= list_size)
42
		memcpy(list, xname, size);
43 44 45
	return size;
}

46 47 48
static int
generic_acl_get(struct dentry *dentry, const char *name, void *buffer,
		     size_t size, int type)
49 50 51 52
{
	struct posix_acl *acl;
	int error;

53 54 55 56
	if (strcmp(name, "") != 0)
		return -EINVAL;

	acl = get_cached_acl(dentry->d_inode, type);
57 58 59 60 61 62 63 64
	if (!acl)
		return -ENODATA;
	error = posix_acl_to_xattr(acl, buffer, size);
	posix_acl_release(acl);

	return error;
}

65 66 67
static int
generic_acl_set(struct dentry *dentry, const char *name, const void *value,
		     size_t size, int flags, int type)
68
{
69
	struct inode *inode = dentry->d_inode;
70 71 72
	struct posix_acl *acl = NULL;
	int error;

73 74
	if (strcmp(name, "") != 0)
		return -EINVAL;
75 76
	if (S_ISLNK(inode->i_mode))
		return -EOPNOTSUPP;
77
	if (!inode_owner_or_capable(inode))
78 79 80 81 82 83 84 85 86 87 88 89
		return -EPERM;
	if (value) {
		acl = posix_acl_from_xattr(value, size);
		if (IS_ERR(acl))
			return PTR_ERR(acl);
	}
	if (acl) {
		mode_t mode;

		error = posix_acl_valid(acl);
		if (error)
			goto failed;
90 91 92 93 94 95 96
		switch (type) {
		case ACL_TYPE_ACCESS:
			mode = inode->i_mode;
			error = posix_acl_equiv_mode(acl, &mode);
			if (error < 0)
				goto failed;
			inode->i_mode = mode;
97
			inode->i_ctime = CURRENT_TIME;
98 99 100 101 102 103 104 105 106 107 108
			if (error == 0) {
				posix_acl_release(acl);
				acl = NULL;
			}
			break;
		case ACL_TYPE_DEFAULT:
			if (!S_ISDIR(inode->i_mode)) {
				error = -EINVAL;
				goto failed;
			}
			break;
109 110
		}
	}
111
	set_cached_acl(inode, type, acl);
112 113 114 115 116 117 118 119 120 121 122 123 124
	error = 0;
failed:
	posix_acl_release(acl);
	return error;
}

/**
 * generic_acl_init  -  Take care of acl inheritance at @inode create time
 *
 * Files created inside a directory with a default ACL inherit the
 * directory's default ACL.
 */
int
125
generic_acl_init(struct inode *inode, struct inode *dir)
126 127 128 129 130
{
	struct posix_acl *acl = NULL;
	mode_t mode = inode->i_mode;
	int error;

A
Al Viro 已提交
131
	inode->i_mode = mode & ~current_umask();
132
	if (!S_ISLNK(inode->i_mode))
133
		acl = get_cached_acl(dir, ACL_TYPE_DEFAULT);
134
	if (acl) {
135 136
		if (S_ISDIR(inode->i_mode))
			set_cached_acl(inode, ACL_TYPE_DEFAULT, acl);
137 138 139 140 141 142
		error = posix_acl_create(&acl, GFP_KERNEL, &mode);
		if (error < 0)
			return error;
		inode->i_mode = mode;
		if (error > 0)
			set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
143 144 145 146 147 148 149 150 151 152 153 154 155 156
	}
	error = 0;

	posix_acl_release(acl);
	return error;
}

/**
 * generic_acl_chmod  -  change the access acl of @inode upon chmod()
 *
 * A chmod also changes the permissions of the owner, group/mask, and
 * other ACL entries.
 */
int
157
generic_acl_chmod(struct inode *inode)
158
{
159
	struct posix_acl *acl;
160 161 162 163
	int error = 0;

	if (S_ISLNK(inode->i_mode))
		return -EOPNOTSUPP;
164
	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
165
	if (acl) {
166 167 168 169
		error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
		if (error)
			return error;
		set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
170 171 172 173
		posix_acl_release(acl);
	}
	return error;
}
174 175

int
176
generic_check_acl(struct inode *inode, int mask)
177
{
178 179 180 181 182 183 184
	struct posix_acl *acl;

	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
	if (acl) {
		int error = posix_acl_permission(inode, acl, mask);
		posix_acl_release(acl);
		return error;
185 186 187 188
	}
	return -EAGAIN;
}

189
const struct xattr_handler generic_acl_access_handler = {
190 191 192 193 194 195 196
	.prefix = POSIX_ACL_XATTR_ACCESS,
	.flags	= ACL_TYPE_ACCESS,
	.list	= generic_acl_list,
	.get	= generic_acl_get,
	.set	= generic_acl_set,
};

197
const struct xattr_handler generic_acl_default_handler = {
198 199 200 201 202 203
	.prefix = POSIX_ACL_XATTR_DEFAULT,
	.flags	= ACL_TYPE_DEFAULT,
	.list	= generic_acl_list,
	.get	= generic_acl_get,
	.set	= generic_acl_set,
};