Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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/slab.h>
10 #include <linux/fs.h>
11 #include <linux/posix_acl_xattr.h>
12
13
14 /*
15  * Convert from extended attribute to in-memory representation.
16  */
17 struct posix_acl *
18 posix_acl_from_xattr(const void *value, size_t size)
19 {
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;
25
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);
32
33         count = posix_acl_xattr_count(size);
34         if (count < 0)
35                 return ERR_PTR(-EINVAL);
36         if (count == 0)
37                 return NULL;
38         
39         acl = posix_acl_alloc(count, GFP_KERNEL);
40         if (!acl)
41                 return ERR_PTR(-ENOMEM);
42         acl_e = acl->a_entries;
43         
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);
47
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;
55
56                         case ACL_USER:
57                         case ACL_GROUP:
58                                 acl_e->e_id = le32_to_cpu(entry->e_id);
59                                 break;
60
61                         default:
62                                 goto fail;
63                 }
64         }
65         return acl;
66
67 fail:
68         posix_acl_release(acl);
69         return ERR_PTR(-EINVAL);
70 }
71 EXPORT_SYMBOL (posix_acl_from_xattr);
72
73 /*
74  * Convert from in-memory to extended attribute representation.
75  */
76 int
77 posix_acl_to_xattr(const struct posix_acl *acl, void *buffer, size_t size)
78 {
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;
82
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;
88         
89         ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
90
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);
95         }
96         return real_size;
97 }
98 EXPORT_SYMBOL (posix_acl_to_xattr);