[S390] qdio: Sanitize do_QDIO sanity checks
[linux-2.6/kvm.git] / fs / ext3 / acl.c
blobe0c7454517158dde2ee69433526c78eeb5398cd2
1 /*
2 * linux/fs/ext3/acl.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 */
7 #include <linux/init.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include <linux/capability.h>
11 #include <linux/fs.h>
12 #include <linux/ext3_jbd.h>
13 #include <linux/ext3_fs.h>
14 #include "xattr.h"
15 #include "acl.h"
18 * Convert from filesystem to in-memory representation.
20 static struct posix_acl *
21 ext3_acl_from_disk(const void *value, size_t size)
23 const char *end = (char *)value + size;
24 int n, count;
25 struct posix_acl *acl;
27 if (!value)
28 return NULL;
29 if (size < sizeof(ext3_acl_header))
30 return ERR_PTR(-EINVAL);
31 if (((ext3_acl_header *)value)->a_version !=
32 cpu_to_le32(EXT3_ACL_VERSION))
33 return ERR_PTR(-EINVAL);
34 value = (char *)value + sizeof(ext3_acl_header);
35 count = ext3_acl_count(size);
36 if (count < 0)
37 return ERR_PTR(-EINVAL);
38 if (count == 0)
39 return NULL;
40 acl = posix_acl_alloc(count, GFP_NOFS);
41 if (!acl)
42 return ERR_PTR(-ENOMEM);
43 for (n=0; n < count; n++) {
44 ext3_acl_entry *entry =
45 (ext3_acl_entry *)value;
46 if ((char *)value + sizeof(ext3_acl_entry_short) > end)
47 goto fail;
48 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
49 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
50 switch(acl->a_entries[n].e_tag) {
51 case ACL_USER_OBJ:
52 case ACL_GROUP_OBJ:
53 case ACL_MASK:
54 case ACL_OTHER:
55 value = (char *)value +
56 sizeof(ext3_acl_entry_short);
57 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
58 break;
60 case ACL_USER:
61 case ACL_GROUP:
62 value = (char *)value + sizeof(ext3_acl_entry);
63 if ((char *)value > end)
64 goto fail;
65 acl->a_entries[n].e_id =
66 le32_to_cpu(entry->e_id);
67 break;
69 default:
70 goto fail;
73 if (value != end)
74 goto fail;
75 return acl;
77 fail:
78 posix_acl_release(acl);
79 return ERR_PTR(-EINVAL);
83 * Convert from in-memory to filesystem representation.
85 static void *
86 ext3_acl_to_disk(const struct posix_acl *acl, size_t *size)
88 ext3_acl_header *ext_acl;
89 char *e;
90 size_t n;
92 *size = ext3_acl_size(acl->a_count);
93 ext_acl = kmalloc(sizeof(ext3_acl_header) + acl->a_count *
94 sizeof(ext3_acl_entry), GFP_NOFS);
95 if (!ext_acl)
96 return ERR_PTR(-ENOMEM);
97 ext_acl->a_version = cpu_to_le32(EXT3_ACL_VERSION);
98 e = (char *)ext_acl + sizeof(ext3_acl_header);
99 for (n=0; n < acl->a_count; n++) {
100 ext3_acl_entry *entry = (ext3_acl_entry *)e;
101 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
102 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
103 switch(acl->a_entries[n].e_tag) {
104 case ACL_USER:
105 case ACL_GROUP:
106 entry->e_id =
107 cpu_to_le32(acl->a_entries[n].e_id);
108 e += sizeof(ext3_acl_entry);
109 break;
111 case ACL_USER_OBJ:
112 case ACL_GROUP_OBJ:
113 case ACL_MASK:
114 case ACL_OTHER:
115 e += sizeof(ext3_acl_entry_short);
116 break;
118 default:
119 goto fail;
122 return (char *)ext_acl;
124 fail:
125 kfree(ext_acl);
126 return ERR_PTR(-EINVAL);
129 static inline struct posix_acl *
130 ext3_iget_acl(struct inode *inode, struct posix_acl **i_acl)
132 struct posix_acl *acl = ACCESS_ONCE(*i_acl);
134 if (acl) {
135 spin_lock(&inode->i_lock);
136 acl = *i_acl;
137 if (acl != EXT3_ACL_NOT_CACHED)
138 acl = posix_acl_dup(acl);
139 spin_unlock(&inode->i_lock);
142 return acl;
145 static inline void
146 ext3_iset_acl(struct inode *inode, struct posix_acl **i_acl,
147 struct posix_acl *acl)
149 spin_lock(&inode->i_lock);
150 if (*i_acl != EXT3_ACL_NOT_CACHED)
151 posix_acl_release(*i_acl);
152 *i_acl = posix_acl_dup(acl);
153 spin_unlock(&inode->i_lock);
157 * Inode operation get_posix_acl().
159 * inode->i_mutex: don't care
161 static struct posix_acl *
162 ext3_get_acl(struct inode *inode, int type)
164 struct ext3_inode_info *ei = EXT3_I(inode);
165 int name_index;
166 char *value = NULL;
167 struct posix_acl *acl;
168 int retval;
170 if (!test_opt(inode->i_sb, POSIX_ACL))
171 return NULL;
173 switch(type) {
174 case ACL_TYPE_ACCESS:
175 acl = ext3_iget_acl(inode, &ei->i_acl);
176 if (acl != EXT3_ACL_NOT_CACHED)
177 return acl;
178 name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
179 break;
181 case ACL_TYPE_DEFAULT:
182 acl = ext3_iget_acl(inode, &ei->i_default_acl);
183 if (acl != EXT3_ACL_NOT_CACHED)
184 return acl;
185 name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
186 break;
188 default:
189 return ERR_PTR(-EINVAL);
191 retval = ext3_xattr_get(inode, name_index, "", NULL, 0);
192 if (retval > 0) {
193 value = kmalloc(retval, GFP_NOFS);
194 if (!value)
195 return ERR_PTR(-ENOMEM);
196 retval = ext3_xattr_get(inode, name_index, "", value, retval);
198 if (retval > 0)
199 acl = ext3_acl_from_disk(value, retval);
200 else if (retval == -ENODATA || retval == -ENOSYS)
201 acl = NULL;
202 else
203 acl = ERR_PTR(retval);
204 kfree(value);
206 if (!IS_ERR(acl)) {
207 switch(type) {
208 case ACL_TYPE_ACCESS:
209 ext3_iset_acl(inode, &ei->i_acl, acl);
210 break;
212 case ACL_TYPE_DEFAULT:
213 ext3_iset_acl(inode, &ei->i_default_acl, acl);
214 break;
217 return acl;
221 * Set the access or default ACL of an inode.
223 * inode->i_mutex: down unless called from ext3_new_inode
225 static int
226 ext3_set_acl(handle_t *handle, struct inode *inode, int type,
227 struct posix_acl *acl)
229 struct ext3_inode_info *ei = EXT3_I(inode);
230 int name_index;
231 void *value = NULL;
232 size_t size = 0;
233 int error;
235 if (S_ISLNK(inode->i_mode))
236 return -EOPNOTSUPP;
238 switch(type) {
239 case ACL_TYPE_ACCESS:
240 name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
241 if (acl) {
242 mode_t mode = inode->i_mode;
243 error = posix_acl_equiv_mode(acl, &mode);
244 if (error < 0)
245 return error;
246 else {
247 inode->i_mode = mode;
248 ext3_mark_inode_dirty(handle, inode);
249 if (error == 0)
250 acl = NULL;
253 break;
255 case ACL_TYPE_DEFAULT:
256 name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
257 if (!S_ISDIR(inode->i_mode))
258 return acl ? -EACCES : 0;
259 break;
261 default:
262 return -EINVAL;
264 if (acl) {
265 value = ext3_acl_to_disk(acl, &size);
266 if (IS_ERR(value))
267 return (int)PTR_ERR(value);
270 error = ext3_xattr_set_handle(handle, inode, name_index, "",
271 value, size, 0);
273 kfree(value);
274 if (!error) {
275 switch(type) {
276 case ACL_TYPE_ACCESS:
277 ext3_iset_acl(inode, &ei->i_acl, acl);
278 break;
280 case ACL_TYPE_DEFAULT:
281 ext3_iset_acl(inode, &ei->i_default_acl, acl);
282 break;
285 return error;
288 static int
289 ext3_check_acl(struct inode *inode, int mask)
291 struct posix_acl *acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
293 if (IS_ERR(acl))
294 return PTR_ERR(acl);
295 if (acl) {
296 int error = posix_acl_permission(inode, acl, mask);
297 posix_acl_release(acl);
298 return error;
301 return -EAGAIN;
305 ext3_permission(struct inode *inode, int mask)
307 return generic_permission(inode, mask, ext3_check_acl);
311 * Initialize the ACLs of a new inode. Called from ext3_new_inode.
313 * dir->i_mutex: down
314 * inode->i_mutex: up (access to inode is still exclusive)
317 ext3_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
319 struct posix_acl *acl = NULL;
320 int error = 0;
322 if (!S_ISLNK(inode->i_mode)) {
323 if (test_opt(dir->i_sb, POSIX_ACL)) {
324 acl = ext3_get_acl(dir, ACL_TYPE_DEFAULT);
325 if (IS_ERR(acl))
326 return PTR_ERR(acl);
328 if (!acl)
329 inode->i_mode &= ~current_umask();
331 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
332 struct posix_acl *clone;
333 mode_t mode;
335 if (S_ISDIR(inode->i_mode)) {
336 error = ext3_set_acl(handle, inode,
337 ACL_TYPE_DEFAULT, acl);
338 if (error)
339 goto cleanup;
341 clone = posix_acl_clone(acl, GFP_NOFS);
342 error = -ENOMEM;
343 if (!clone)
344 goto cleanup;
346 mode = inode->i_mode;
347 error = posix_acl_create_masq(clone, &mode);
348 if (error >= 0) {
349 inode->i_mode = mode;
350 if (error > 0) {
351 /* This is an extended ACL */
352 error = ext3_set_acl(handle, inode,
353 ACL_TYPE_ACCESS, clone);
356 posix_acl_release(clone);
358 cleanup:
359 posix_acl_release(acl);
360 return error;
364 * Does chmod for an inode that may have an Access Control List. The
365 * inode->i_mode field must be updated to the desired value by the caller
366 * before calling this function.
367 * Returns 0 on success, or a negative error number.
369 * We change the ACL rather than storing some ACL entries in the file
370 * mode permission bits (which would be more efficient), because that
371 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
372 * for directories) are added. There are no more bits available in the
373 * file mode.
375 * inode->i_mutex: down
378 ext3_acl_chmod(struct inode *inode)
380 struct posix_acl *acl, *clone;
381 int error;
383 if (S_ISLNK(inode->i_mode))
384 return -EOPNOTSUPP;
385 if (!test_opt(inode->i_sb, POSIX_ACL))
386 return 0;
387 acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
388 if (IS_ERR(acl) || !acl)
389 return PTR_ERR(acl);
390 clone = posix_acl_clone(acl, GFP_KERNEL);
391 posix_acl_release(acl);
392 if (!clone)
393 return -ENOMEM;
394 error = posix_acl_chmod_masq(clone, inode->i_mode);
395 if (!error) {
396 handle_t *handle;
397 int retries = 0;
399 retry:
400 handle = ext3_journal_start(inode,
401 EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
402 if (IS_ERR(handle)) {
403 error = PTR_ERR(handle);
404 ext3_std_error(inode->i_sb, error);
405 goto out;
407 error = ext3_set_acl(handle, inode, ACL_TYPE_ACCESS, clone);
408 ext3_journal_stop(handle);
409 if (error == -ENOSPC &&
410 ext3_should_retry_alloc(inode->i_sb, &retries))
411 goto retry;
413 out:
414 posix_acl_release(clone);
415 return error;
419 * Extended attribute handlers
421 static size_t
422 ext3_xattr_list_acl_access(struct inode *inode, char *list, size_t list_len,
423 const char *name, size_t name_len)
425 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
427 if (!test_opt(inode->i_sb, POSIX_ACL))
428 return 0;
429 if (list && size <= list_len)
430 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
431 return size;
434 static size_t
435 ext3_xattr_list_acl_default(struct inode *inode, char *list, size_t list_len,
436 const char *name, size_t name_len)
438 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
440 if (!test_opt(inode->i_sb, POSIX_ACL))
441 return 0;
442 if (list && size <= list_len)
443 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
444 return size;
447 static int
448 ext3_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
450 struct posix_acl *acl;
451 int error;
453 if (!test_opt(inode->i_sb, POSIX_ACL))
454 return -EOPNOTSUPP;
456 acl = ext3_get_acl(inode, type);
457 if (IS_ERR(acl))
458 return PTR_ERR(acl);
459 if (acl == NULL)
460 return -ENODATA;
461 error = posix_acl_to_xattr(acl, buffer, size);
462 posix_acl_release(acl);
464 return error;
467 static int
468 ext3_xattr_get_acl_access(struct inode *inode, const char *name,
469 void *buffer, size_t size)
471 if (strcmp(name, "") != 0)
472 return -EINVAL;
473 return ext3_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
476 static int
477 ext3_xattr_get_acl_default(struct inode *inode, const char *name,
478 void *buffer, size_t size)
480 if (strcmp(name, "") != 0)
481 return -EINVAL;
482 return ext3_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
485 static int
486 ext3_xattr_set_acl(struct inode *inode, int type, const void *value,
487 size_t size)
489 handle_t *handle;
490 struct posix_acl *acl;
491 int error, retries = 0;
493 if (!test_opt(inode->i_sb, POSIX_ACL))
494 return -EOPNOTSUPP;
495 if (!is_owner_or_cap(inode))
496 return -EPERM;
498 if (value) {
499 acl = posix_acl_from_xattr(value, size);
500 if (IS_ERR(acl))
501 return PTR_ERR(acl);
502 else if (acl) {
503 error = posix_acl_valid(acl);
504 if (error)
505 goto release_and_out;
507 } else
508 acl = NULL;
510 retry:
511 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
512 if (IS_ERR(handle))
513 return PTR_ERR(handle);
514 error = ext3_set_acl(handle, inode, type, acl);
515 ext3_journal_stop(handle);
516 if (error == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
517 goto retry;
519 release_and_out:
520 posix_acl_release(acl);
521 return error;
524 static int
525 ext3_xattr_set_acl_access(struct inode *inode, const char *name,
526 const void *value, size_t size, int flags)
528 if (strcmp(name, "") != 0)
529 return -EINVAL;
530 return ext3_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
533 static int
534 ext3_xattr_set_acl_default(struct inode *inode, const char *name,
535 const void *value, size_t size, int flags)
537 if (strcmp(name, "") != 0)
538 return -EINVAL;
539 return ext3_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
542 struct xattr_handler ext3_xattr_acl_access_handler = {
543 .prefix = POSIX_ACL_XATTR_ACCESS,
544 .list = ext3_xattr_list_acl_access,
545 .get = ext3_xattr_get_acl_access,
546 .set = ext3_xattr_set_acl_access,
549 struct xattr_handler ext3_xattr_acl_default_handler = {
550 .prefix = POSIX_ACL_XATTR_DEFAULT,
551 .list = ext3_xattr_list_acl_default,
552 .get = ext3_xattr_get_acl_default,
553 .set = ext3_xattr_set_acl_default,