NFS: nfsacl_{encode,decode} should return signed integer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs_common / nfsacl.c
bloba3e78bd186794362d602352aad914d4cb1b5613b
1 /*
2 * fs/nfs_common/nfsacl.c
4 * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
5 */
7 /*
8 * The Solaris nfsacl protocol represents some ACLs slightly differently
9 * than POSIX 1003.1e draft 17 does (and we do):
11 * - Minimal ACLs always have an ACL_MASK entry, so they have
12 * four instead of three entries.
13 * - The ACL_MASK entry in such minimal ACLs always has the same
14 * permissions as the ACL_GROUP_OBJ entry. (In extended ACLs
15 * the ACL_MASK and ACL_GROUP_OBJ entries may differ.)
16 * - The identifier fields of the ACL_USER_OBJ and ACL_GROUP_OBJ
17 * entries contain the identifiers of the owner and owning group.
18 * (In POSIX ACLs we always set them to ACL_UNDEFINED_ID).
19 * - ACL entries in the kernel are kept sorted in ascending order
20 * of (e_tag, e_id). Solaris ACLs are unsorted.
23 #include <linux/module.h>
24 #include <linux/fs.h>
25 #include <linux/gfp.h>
26 #include <linux/sunrpc/xdr.h>
27 #include <linux/nfsacl.h>
28 #include <linux/nfs3.h>
29 #include <linux/sort.h>
31 MODULE_LICENSE("GPL");
33 EXPORT_SYMBOL_GPL(nfsacl_encode);
34 EXPORT_SYMBOL_GPL(nfsacl_decode);
36 struct nfsacl_encode_desc {
37 struct xdr_array2_desc desc;
38 unsigned int count;
39 struct posix_acl *acl;
40 int typeflag;
41 uid_t uid;
42 gid_t gid;
45 static int
46 xdr_nfsace_encode(struct xdr_array2_desc *desc, void *elem)
48 struct nfsacl_encode_desc *nfsacl_desc =
49 (struct nfsacl_encode_desc *) desc;
50 __be32 *p = elem;
52 struct posix_acl_entry *entry =
53 &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
55 *p++ = htonl(entry->e_tag | nfsacl_desc->typeflag);
56 switch(entry->e_tag) {
57 case ACL_USER_OBJ:
58 *p++ = htonl(nfsacl_desc->uid);
59 break;
60 case ACL_GROUP_OBJ:
61 *p++ = htonl(nfsacl_desc->gid);
62 break;
63 case ACL_USER:
64 case ACL_GROUP:
65 *p++ = htonl(entry->e_id);
66 break;
67 default: /* Solaris depends on that! */
68 *p++ = 0;
69 break;
71 *p++ = htonl(entry->e_perm & S_IRWXO);
72 return 0;
75 /**
76 * nfsacl_encode - Encode an NFSv3 ACL
78 * @buf: destination xdr_buf to contain XDR encoded ACL
79 * @base: byte offset in xdr_buf where XDR'd ACL begins
80 * @inode: inode of file whose ACL this is
81 * @acl: posix_acl to encode
82 * @encode_entries: whether to encode ACEs as well
83 * @typeflag: ACL type: NFS_ACL_DEFAULT or zero
85 * Returns size of encoded ACL in bytes or a negative errno value.
87 int nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode,
88 struct posix_acl *acl, int encode_entries, int typeflag)
90 int entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0;
91 struct nfsacl_encode_desc nfsacl_desc = {
92 .desc = {
93 .elem_size = 12,
94 .array_len = encode_entries ? entries : 0,
95 .xcode = xdr_nfsace_encode,
97 .acl = acl,
98 .typeflag = typeflag,
99 .uid = inode->i_uid,
100 .gid = inode->i_gid,
102 int err;
103 struct posix_acl *acl2 = NULL;
105 if (entries > NFS_ACL_MAX_ENTRIES ||
106 xdr_encode_word(buf, base, entries))
107 return -EINVAL;
108 if (encode_entries && acl && acl->a_count == 3) {
109 /* Fake up an ACL_MASK entry. */
110 acl2 = posix_acl_alloc(4, GFP_KERNEL);
111 if (!acl2)
112 return -ENOMEM;
113 /* Insert entries in canonical order: other orders seem
114 to confuse Solaris VxFS. */
115 acl2->a_entries[0] = acl->a_entries[0]; /* ACL_USER_OBJ */
116 acl2->a_entries[1] = acl->a_entries[1]; /* ACL_GROUP_OBJ */
117 acl2->a_entries[2] = acl->a_entries[1]; /* ACL_MASK */
118 acl2->a_entries[2].e_tag = ACL_MASK;
119 acl2->a_entries[3] = acl->a_entries[2]; /* ACL_OTHER */
120 nfsacl_desc.acl = acl2;
122 err = xdr_encode_array2(buf, base + 4, &nfsacl_desc.desc);
123 if (acl2)
124 posix_acl_release(acl2);
125 if (!err)
126 err = 8 + nfsacl_desc.desc.elem_size *
127 nfsacl_desc.desc.array_len;
128 return err;
131 struct nfsacl_decode_desc {
132 struct xdr_array2_desc desc;
133 unsigned int count;
134 struct posix_acl *acl;
137 static int
138 xdr_nfsace_decode(struct xdr_array2_desc *desc, void *elem)
140 struct nfsacl_decode_desc *nfsacl_desc =
141 (struct nfsacl_decode_desc *) desc;
142 __be32 *p = elem;
143 struct posix_acl_entry *entry;
145 if (!nfsacl_desc->acl) {
146 if (desc->array_len > NFS_ACL_MAX_ENTRIES)
147 return -EINVAL;
148 nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL);
149 if (!nfsacl_desc->acl)
150 return -ENOMEM;
151 nfsacl_desc->count = 0;
154 entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
155 entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT;
156 entry->e_id = ntohl(*p++);
157 entry->e_perm = ntohl(*p++);
159 switch(entry->e_tag) {
160 case ACL_USER_OBJ:
161 case ACL_USER:
162 case ACL_GROUP_OBJ:
163 case ACL_GROUP:
164 case ACL_OTHER:
165 if (entry->e_perm & ~S_IRWXO)
166 return -EINVAL;
167 break;
168 case ACL_MASK:
169 /* Solaris sometimes sets additonal bits in the mask */
170 entry->e_perm &= S_IRWXO;
171 break;
172 default:
173 return -EINVAL;
176 return 0;
179 static int
180 cmp_acl_entry(const void *x, const void *y)
182 const struct posix_acl_entry *a = x, *b = y;
184 if (a->e_tag != b->e_tag)
185 return a->e_tag - b->e_tag;
186 else if (a->e_id > b->e_id)
187 return 1;
188 else if (a->e_id < b->e_id)
189 return -1;
190 else
191 return 0;
195 * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
197 static int
198 posix_acl_from_nfsacl(struct posix_acl *acl)
200 struct posix_acl_entry *pa, *pe,
201 *group_obj = NULL, *mask = NULL;
203 if (!acl)
204 return 0;
206 sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry),
207 cmp_acl_entry, NULL);
209 /* Clear undefined identifier fields and find the ACL_GROUP_OBJ
210 and ACL_MASK entries. */
211 FOREACH_ACL_ENTRY(pa, acl, pe) {
212 switch(pa->e_tag) {
213 case ACL_USER_OBJ:
214 pa->e_id = ACL_UNDEFINED_ID;
215 break;
216 case ACL_GROUP_OBJ:
217 pa->e_id = ACL_UNDEFINED_ID;
218 group_obj = pa;
219 break;
220 case ACL_MASK:
221 mask = pa;
222 /* fall through */
223 case ACL_OTHER:
224 pa->e_id = ACL_UNDEFINED_ID;
225 break;
228 if (acl->a_count == 4 && group_obj && mask &&
229 mask->e_perm == group_obj->e_perm) {
230 /* remove bogus ACL_MASK entry */
231 memmove(mask, mask+1, (3 - (mask - acl->a_entries)) *
232 sizeof(struct posix_acl_entry));
233 acl->a_count = 3;
235 return 0;
239 * nfsacl_decode - Decode an NFSv3 ACL
241 * @buf: xdr_buf containing XDR'd ACL data to decode
242 * @base: byte offset in xdr_buf where XDR'd ACL begins
243 * @aclcnt: count of ACEs in decoded posix_acl
244 * @pacl: buffer in which to place decoded posix_acl
246 * Returns the length of the decoded ACL in bytes, or a negative errno value.
248 int nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt,
249 struct posix_acl **pacl)
251 struct nfsacl_decode_desc nfsacl_desc = {
252 .desc = {
253 .elem_size = 12,
254 .xcode = pacl ? xdr_nfsace_decode : NULL,
257 u32 entries;
258 int err;
260 if (xdr_decode_word(buf, base, &entries) ||
261 entries > NFS_ACL_MAX_ENTRIES)
262 return -EINVAL;
263 nfsacl_desc.desc.array_maxlen = entries;
264 err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc);
265 if (err)
266 return err;
267 if (pacl) {
268 if (entries != nfsacl_desc.desc.array_len ||
269 posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
270 posix_acl_release(nfsacl_desc.acl);
271 return -EINVAL;
273 *pacl = nfsacl_desc.acl;
275 if (aclcnt)
276 *aclcnt = entries;
277 return 8 + nfsacl_desc.desc.elem_size *
278 nfsacl_desc.desc.array_len;