Merge branch 'for-linus' of git://repo.or.cz/cris-mirror
[linux-2.6/mini2440.git] / fs / xattr_acl.c
blobc6ad7c7e3ee9eaa58c4a7994c316f4f4a24fd45a
1 /*
2 * linux/fs/xattr_acl.c
4 * Almost all from linux/fs/ext2/acl.c:
5 * Copyright (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
6 */
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/fs.h>
11 #include <linux/posix_acl_xattr.h>
15 * Convert from extended attribute to in-memory representation.
17 struct posix_acl *
18 posix_acl_from_xattr(const void *value, size_t size)
20 posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
21 posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
22 int count;
23 struct posix_acl *acl;
24 struct posix_acl_entry *acl_e;
26 if (!value)
27 return NULL;
28 if (size < sizeof(posix_acl_xattr_header))
29 return ERR_PTR(-EINVAL);
30 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
31 return ERR_PTR(-EOPNOTSUPP);
33 count = posix_acl_xattr_count(size);
34 if (count < 0)
35 return ERR_PTR(-EINVAL);
36 if (count == 0)
37 return NULL;
39 acl = posix_acl_alloc(count, GFP_KERNEL);
40 if (!acl)
41 return ERR_PTR(-ENOMEM);
42 acl_e = acl->a_entries;
44 for (end = entry + count; entry != end; acl_e++, entry++) {
45 acl_e->e_tag = le16_to_cpu(entry->e_tag);
46 acl_e->e_perm = le16_to_cpu(entry->e_perm);
48 switch(acl_e->e_tag) {
49 case ACL_USER_OBJ:
50 case ACL_GROUP_OBJ:
51 case ACL_MASK:
52 case ACL_OTHER:
53 acl_e->e_id = ACL_UNDEFINED_ID;
54 break;
56 case ACL_USER:
57 case ACL_GROUP:
58 acl_e->e_id = le32_to_cpu(entry->e_id);
59 break;
61 default:
62 goto fail;
65 return acl;
67 fail:
68 posix_acl_release(acl);
69 return ERR_PTR(-EINVAL);
71 EXPORT_SYMBOL (posix_acl_from_xattr);
74 * Convert from in-memory to extended attribute representation.
76 int
77 posix_acl_to_xattr(const struct posix_acl *acl, void *buffer, size_t size)
79 posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer;
80 posix_acl_xattr_entry *ext_entry = ext_acl->a_entries;
81 int real_size, n;
83 real_size = posix_acl_xattr_size(acl->a_count);
84 if (!buffer)
85 return real_size;
86 if (real_size > size)
87 return -ERANGE;
89 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
91 for (n=0; n < acl->a_count; n++, ext_entry++) {
92 ext_entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
93 ext_entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
94 ext_entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
96 return real_size;
98 EXPORT_SYMBOL (posix_acl_to_xattr);