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