acl.c 6.4 KB
Newer Older
1
/*
2
 * linux/fs/ext4/acl.c
3 4 5 6
 *
 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
 */

7
#include <linux/quotaops.h>
8 9
#include "ext4_jbd2.h"
#include "ext4.h"
10 11 12 13 14 15 16
#include "xattr.h"
#include "acl.h"

/*
 * Convert from filesystem to in-memory representation.
 */
static struct posix_acl *
17
ext4_acl_from_disk(const void *value, size_t size)
18 19 20 21 22 23 24
{
	const char *end = (char *)value + size;
	int n, count;
	struct posix_acl *acl;

	if (!value)
		return NULL;
25
	if (size < sizeof(ext4_acl_header))
26
		 return ERR_PTR(-EINVAL);
27 28
	if (((ext4_acl_header *)value)->a_version !=
	    cpu_to_le32(EXT4_ACL_VERSION))
29
		return ERR_PTR(-EINVAL);
30 31
	value = (char *)value + sizeof(ext4_acl_header);
	count = ext4_acl_count(size);
32 33 34 35
	if (count < 0)
		return ERR_PTR(-EINVAL);
	if (count == 0)
		return NULL;
36
	acl = posix_acl_alloc(count, GFP_NOFS);
37 38
	if (!acl)
		return ERR_PTR(-ENOMEM);
39
	for (n = 0; n < count; n++) {
40 41 42
		ext4_acl_entry *entry =
			(ext4_acl_entry *)value;
		if ((char *)value + sizeof(ext4_acl_entry_short) > end)
43 44 45
			goto fail;
		acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
		acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
46 47 48 49 50 51 52 53 54 55 56

		switch (acl->a_entries[n].e_tag) {
		case ACL_USER_OBJ:
		case ACL_GROUP_OBJ:
		case ACL_MASK:
		case ACL_OTHER:
			value = (char *)value +
				sizeof(ext4_acl_entry_short);
			break;

		case ACL_USER:
57 58 59 60 61 62 63
			value = (char *)value + sizeof(ext4_acl_entry);
			if ((char *)value > end)
				goto fail;
			acl->a_entries[n].e_uid =
				make_kuid(&init_user_ns,
					  le32_to_cpu(entry->e_id));
			break;
64 65 66
		case ACL_GROUP:
			value = (char *)value + sizeof(ext4_acl_entry);
			if ((char *)value > end)
67
				goto fail;
68 69 70
			acl->a_entries[n].e_gid =
				make_kgid(&init_user_ns,
					  le32_to_cpu(entry->e_id));
71 72 73 74
			break;

		default:
			goto fail;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
		}
	}
	if (value != end)
		goto fail;
	return acl;

fail:
	posix_acl_release(acl);
	return ERR_PTR(-EINVAL);
}

/*
 * Convert from in-memory to filesystem representation.
 */
static void *
90
ext4_acl_to_disk(const struct posix_acl *acl, size_t *size)
91
{
92
	ext4_acl_header *ext_acl;
93 94 95
	char *e;
	size_t n;

96 97
	*size = ext4_acl_size(acl->a_count);
	ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count *
98
			sizeof(ext4_acl_entry), GFP_NOFS);
99 100
	if (!ext_acl)
		return ERR_PTR(-ENOMEM);
101 102
	ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION);
	e = (char *)ext_acl + sizeof(ext4_acl_header);
103
	for (n = 0; n < acl->a_count; n++) {
104
		const struct posix_acl_entry *acl_e = &acl->a_entries[n];
105
		ext4_acl_entry *entry = (ext4_acl_entry *)e;
106 107 108
		entry->e_tag  = cpu_to_le16(acl_e->e_tag);
		entry->e_perm = cpu_to_le16(acl_e->e_perm);
		switch (acl_e->e_tag) {
109
		case ACL_USER:
110 111 112 113
			entry->e_id = cpu_to_le32(
				from_kuid(&init_user_ns, acl_e->e_uid));
			e += sizeof(ext4_acl_entry);
			break;
114
		case ACL_GROUP:
115 116
			entry->e_id = cpu_to_le32(
				from_kgid(&init_user_ns, acl_e->e_gid));
117 118 119 120 121 122 123 124 125 126 127 128
			e += sizeof(ext4_acl_entry);
			break;

		case ACL_USER_OBJ:
		case ACL_GROUP_OBJ:
		case ACL_MASK:
		case ACL_OTHER:
			e += sizeof(ext4_acl_entry_short);
			break;

		default:
			goto fail;
129 130 131 132 133 134 135 136 137 138 139 140 141 142
		}
	}
	return (char *)ext_acl;

fail:
	kfree(ext_acl);
	return ERR_PTR(-EINVAL);
}

/*
 * Inode operation get_posix_acl().
 *
 * inode->i_mutex: don't care
 */
143
struct posix_acl *
144
ext4_get_acl(struct inode *inode, int type)
145 146 147 148 149 150
{
	int name_index;
	char *value = NULL;
	struct posix_acl *acl;
	int retval;

151 152 153 154 155 156 157 158
	switch (type) {
	case ACL_TYPE_ACCESS:
		name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
		break;
	case ACL_TYPE_DEFAULT:
		name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
		break;
	default:
159
		BUG();
160
	}
161
	retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
162
	if (retval > 0) {
163
		value = kmalloc(retval, GFP_NOFS);
164 165
		if (!value)
			return ERR_PTR(-ENOMEM);
166
		retval = ext4_xattr_get(inode, name_index, "", value, retval);
167 168
	}
	if (retval > 0)
169
		acl = ext4_acl_from_disk(value, retval);
170 171 172 173 174 175 176 177 178 179 180 181
	else if (retval == -ENODATA || retval == -ENOSYS)
		acl = NULL;
	else
		acl = ERR_PTR(retval);
	kfree(value);

	return acl;
}

/*
 * Set the access or default ACL of an inode.
 *
182
 * inode->i_mutex: down unless called from ext4_new_inode
183 184
 */
static int
185
__ext4_set_acl(handle_t *handle, struct inode *inode, int type,
186
	     struct posix_acl *acl, int xattr_flags)
187 188 189 190 191
{
	int name_index;
	void *value = NULL;
	size_t size = 0;
	int error;
192 193
	int update_mode = 0;
	umode_t mode = inode->i_mode;
194

195 196 197 198
	switch (type) {
	case ACL_TYPE_ACCESS:
		name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
		if (acl) {
199
			error = posix_acl_update_mode(inode, &mode, &acl);
200
			if (error)
201
				return error;
202
			update_mode = 1;
203 204
		}
		break;
205

206 207 208 209 210
	case ACL_TYPE_DEFAULT:
		name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
		if (!S_ISDIR(inode->i_mode))
			return acl ? -EACCES : 0;
		break;
211

212 213
	default:
		return -EINVAL;
214 215
	}
	if (acl) {
216
		value = ext4_acl_to_disk(acl, &size);
217 218 219 220
		if (IS_ERR(value))
			return (int)PTR_ERR(value);
	}

221
	error = ext4_xattr_set_handle(handle, inode, name_index, "",
222
				      value, size, xattr_flags);
223 224

	kfree(value);
225
	if (!error) {
226
		set_cached_acl(inode, type, acl);
227 228 229 230 231 232
		if (update_mode) {
			inode->i_mode = mode;
			inode->i_ctime = current_time(inode);
			ext4_mark_inode_dirty(handle, inode);
		}
	}
233

234 235 236 237
	return error;
}

int
238
ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)
239
{
240
	handle_t *handle;
241 242
	int error, credits, retries = 0;
	size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0;
243

244 245 246
	error = dquot_initialize(inode);
	if (error)
		return error;
247
retry:
248 249
	error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
				       &credits);
T
Tahsin Erdogan 已提交
250 251 252
	if (error)
		return error;

253
	handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
254 255 256
	if (IS_ERR(handle))
		return PTR_ERR(handle);

257
	error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */);
258
	ext4_journal_stop(handle);
259
	if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
260
		goto retry;
261 262 263 264
	return error;
}

/*
265 266 267 268
 * Initialize the ACLs of a new inode. Called from ext4_new_inode.
 *
 * dir->i_mutex: down
 * inode->i_mutex: up (access to inode is still exclusive)
269
 */
270 271
int
ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
272
{
273
	struct posix_acl *default_acl, *acl;
274 275
	int error;

276 277 278
	error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
	if (error)
		return error;
279

280 281
	if (default_acl) {
		error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
282
				       default_acl, XATTR_CREATE);
283 284 285 286 287
		posix_acl_release(default_acl);
	}
	if (acl) {
		if (!error)
			error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
288
					       acl, XATTR_CREATE);
289
		posix_acl_release(acl);
290
	}
291 292
	return error;
}