2 * fs/nfs_common/nfsacl.c
4 * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
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>
25 #include <linux/sunrpc/xdr.h>
26 #include <linux/nfsacl.h>
27 #include <linux/nfs3.h>
28 #include <linux/sort.h>
30 MODULE_LICENSE("GPL");
32 EXPORT_SYMBOL(nfsacl_encode
);
33 EXPORT_SYMBOL(nfsacl_decode
);
35 struct nfsacl_encode_desc
{
36 struct xdr_array2_desc desc
;
38 struct posix_acl
*acl
;
45 xdr_nfsace_encode(struct xdr_array2_desc
*desc
, void *elem
)
47 struct nfsacl_encode_desc
*nfsacl_desc
=
48 (struct nfsacl_encode_desc
*) desc
;
49 u32
*p
= (u32
*) elem
;
51 if (nfsacl_desc
->count
< nfsacl_desc
->acl
->a_count
) {
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
) {
58 *p
++ = htonl(nfsacl_desc
->uid
);
61 *p
++ = htonl(nfsacl_desc
->gid
);
65 *p
++ = htonl(entry
->e_id
);
67 default: /* Solaris depends on that! */
71 *p
++ = htonl(entry
->e_perm
& S_IRWXO
);
73 const struct posix_acl_entry
*pa
, *pe
;
74 int group_obj_perm
= ACL_READ
|ACL_WRITE
|ACL_EXECUTE
;
76 FOREACH_ACL_ENTRY(pa
, nfsacl_desc
->acl
, pe
) {
77 if (pa
->e_tag
== ACL_GROUP_OBJ
) {
78 group_obj_perm
= pa
->e_perm
& S_IRWXO
;
82 /* fake up ACL_MASK entry */
83 *p
++ = htonl(ACL_MASK
| nfsacl_desc
->typeflag
);
85 *p
++ = htonl(group_obj_perm
);
92 nfsacl_encode(struct xdr_buf
*buf
, unsigned int base
, struct inode
*inode
,
93 struct posix_acl
*acl
, int encode_entries
, int typeflag
)
95 int entries
= (acl
&& acl
->a_count
) ? max_t(int, acl
->a_count
, 4) : 0;
96 struct nfsacl_encode_desc nfsacl_desc
= {
99 .array_len
= encode_entries
? entries
: 0,
100 .xcode
= xdr_nfsace_encode
,
103 .typeflag
= typeflag
,
109 if (entries
> NFS_ACL_MAX_ENTRIES
||
110 xdr_encode_word(buf
, base
, entries
))
112 err
= xdr_encode_array2(buf
, base
+ 4, &nfsacl_desc
.desc
);
114 err
= 8 + nfsacl_desc
.desc
.elem_size
*
115 nfsacl_desc
.desc
.array_len
;
119 struct nfsacl_decode_desc
{
120 struct xdr_array2_desc desc
;
122 struct posix_acl
*acl
;
126 xdr_nfsace_decode(struct xdr_array2_desc
*desc
, void *elem
)
128 struct nfsacl_decode_desc
*nfsacl_desc
=
129 (struct nfsacl_decode_desc
*) desc
;
130 u32
*p
= (u32
*) elem
;
131 struct posix_acl_entry
*entry
;
133 if (!nfsacl_desc
->acl
) {
134 if (desc
->array_len
> NFS_ACL_MAX_ENTRIES
)
136 nfsacl_desc
->acl
= posix_acl_alloc(desc
->array_len
, GFP_KERNEL
);
137 if (!nfsacl_desc
->acl
)
139 nfsacl_desc
->count
= 0;
142 entry
= &nfsacl_desc
->acl
->a_entries
[nfsacl_desc
->count
++];
143 entry
->e_tag
= ntohl(*p
++) & ~NFS_ACL_DEFAULT
;
144 entry
->e_id
= ntohl(*p
++);
145 entry
->e_perm
= ntohl(*p
++);
147 switch(entry
->e_tag
) {
153 if (entry
->e_perm
& ~S_IRWXO
)
157 /* Solaris sometimes sets additonal bits in the mask */
158 entry
->e_perm
&= S_IRWXO
;
168 cmp_acl_entry(const void *x
, const void *y
)
170 const struct posix_acl_entry
*a
= x
, *b
= y
;
172 if (a
->e_tag
!= b
->e_tag
)
173 return a
->e_tag
- b
->e_tag
;
174 else if (a
->e_id
> b
->e_id
)
176 else if (a
->e_id
< b
->e_id
)
183 * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
186 posix_acl_from_nfsacl(struct posix_acl
*acl
)
188 struct posix_acl_entry
*pa
, *pe
,
189 *group_obj
= NULL
, *mask
= NULL
;
194 sort(acl
->a_entries
, acl
->a_count
, sizeof(struct posix_acl_entry
),
195 cmp_acl_entry
, NULL
);
197 /* Clear undefined identifier fields and find the ACL_GROUP_OBJ
198 and ACL_MASK entries. */
199 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
202 pa
->e_id
= ACL_UNDEFINED_ID
;
205 pa
->e_id
= ACL_UNDEFINED_ID
;
212 pa
->e_id
= ACL_UNDEFINED_ID
;
216 if (acl
->a_count
== 4 && group_obj
&& mask
&&
217 mask
->e_perm
== group_obj
->e_perm
) {
218 /* remove bogus ACL_MASK entry */
219 memmove(mask
, mask
+1, (3 - (mask
- acl
->a_entries
)) *
220 sizeof(struct posix_acl_entry
));
227 nfsacl_decode(struct xdr_buf
*buf
, unsigned int base
, unsigned int *aclcnt
,
228 struct posix_acl
**pacl
)
230 struct nfsacl_decode_desc nfsacl_desc
= {
233 .xcode
= pacl
? xdr_nfsace_decode
: NULL
,
239 if (xdr_decode_word(buf
, base
, &entries
) ||
240 entries
> NFS_ACL_MAX_ENTRIES
)
242 nfsacl_desc
.desc
.array_maxlen
= entries
;
243 err
= xdr_decode_array2(buf
, base
+ 4, &nfsacl_desc
.desc
);
247 if (entries
!= nfsacl_desc
.desc
.array_len
||
248 posix_acl_from_nfsacl(nfsacl_desc
.acl
) != 0) {
249 posix_acl_release(nfsacl_desc
.acl
);
252 *pacl
= nfsacl_desc
.acl
;
256 return 8 + nfsacl_desc
.desc
.elem_size
*
257 nfsacl_desc
.desc
.array_len
;