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/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
;
39 struct posix_acl
*acl
;
45 struct nfsacl_simple_acl
{
47 struct posix_acl_entry ace
[4];
51 xdr_nfsace_encode(struct xdr_array2_desc
*desc
, void *elem
)
53 struct nfsacl_encode_desc
*nfsacl_desc
=
54 (struct nfsacl_encode_desc
*) desc
;
57 struct posix_acl_entry
*entry
=
58 &nfsacl_desc
->acl
->a_entries
[nfsacl_desc
->count
++];
60 *p
++ = htonl(entry
->e_tag
| nfsacl_desc
->typeflag
);
61 switch(entry
->e_tag
) {
63 *p
++ = htonl(from_kuid(&init_user_ns
, nfsacl_desc
->uid
));
66 *p
++ = htonl(from_kgid(&init_user_ns
, nfsacl_desc
->gid
));
69 *p
++ = htonl(from_kuid(&init_user_ns
, entry
->e_uid
));
72 *p
++ = htonl(from_kgid(&init_user_ns
, entry
->e_gid
));
74 default: /* Solaris depends on that! */
78 *p
++ = htonl(entry
->e_perm
& S_IRWXO
);
83 * nfsacl_encode - Encode an NFSv3 ACL
85 * @buf: destination xdr_buf to contain XDR encoded ACL
86 * @base: byte offset in xdr_buf where XDR'd ACL begins
87 * @inode: inode of file whose ACL this is
88 * @acl: posix_acl to encode
89 * @encode_entries: whether to encode ACEs as well
90 * @typeflag: ACL type: NFS_ACL_DEFAULT or zero
92 * Returns size of encoded ACL in bytes or a negative errno value.
94 int nfsacl_encode(struct xdr_buf
*buf
, unsigned int base
, struct inode
*inode
,
95 struct posix_acl
*acl
, int encode_entries
, int typeflag
)
97 int entries
= (acl
&& acl
->a_count
) ? max_t(int, acl
->a_count
, 4) : 0;
98 struct nfsacl_encode_desc nfsacl_desc
= {
101 .array_len
= encode_entries
? entries
: 0,
102 .xcode
= xdr_nfsace_encode
,
105 .typeflag
= typeflag
,
109 struct nfsacl_simple_acl aclbuf
;
112 if (entries
> NFS_ACL_MAX_ENTRIES
||
113 xdr_encode_word(buf
, base
, entries
))
115 if (encode_entries
&& acl
&& acl
->a_count
== 3) {
116 struct posix_acl
*acl2
= &aclbuf
.acl
;
118 /* Avoid the use of posix_acl_alloc(). nfsacl_encode() is
119 * invoked in contexts where a memory allocation failure is
120 * fatal. Fortunately this fake ACL is small enough to
121 * construct on the stack. */
122 posix_acl_init(acl2
, 4);
124 /* Insert entries in canonical order: other orders seem
125 to confuse Solaris VxFS. */
126 acl2
->a_entries
[0] = acl
->a_entries
[0]; /* ACL_USER_OBJ */
127 acl2
->a_entries
[1] = acl
->a_entries
[1]; /* ACL_GROUP_OBJ */
128 acl2
->a_entries
[2] = acl
->a_entries
[1]; /* ACL_MASK */
129 acl2
->a_entries
[2].e_tag
= ACL_MASK
;
130 acl2
->a_entries
[3] = acl
->a_entries
[2]; /* ACL_OTHER */
131 nfsacl_desc
.acl
= acl2
;
133 err
= xdr_encode_array2(buf
, base
+ 4, &nfsacl_desc
.desc
);
135 err
= 8 + nfsacl_desc
.desc
.elem_size
*
136 nfsacl_desc
.desc
.array_len
;
140 struct nfsacl_decode_desc
{
141 struct xdr_array2_desc desc
;
143 struct posix_acl
*acl
;
147 xdr_nfsace_decode(struct xdr_array2_desc
*desc
, void *elem
)
149 struct nfsacl_decode_desc
*nfsacl_desc
=
150 (struct nfsacl_decode_desc
*) desc
;
152 struct posix_acl_entry
*entry
;
155 if (!nfsacl_desc
->acl
) {
156 if (desc
->array_len
> NFS_ACL_MAX_ENTRIES
)
158 nfsacl_desc
->acl
= posix_acl_alloc(desc
->array_len
, GFP_KERNEL
);
159 if (!nfsacl_desc
->acl
)
161 nfsacl_desc
->count
= 0;
164 entry
= &nfsacl_desc
->acl
->a_entries
[nfsacl_desc
->count
++];
165 entry
->e_tag
= ntohl(*p
++) & ~NFS_ACL_DEFAULT
;
167 entry
->e_perm
= ntohl(*p
++);
169 switch(entry
->e_tag
) {
171 entry
->e_uid
= make_kuid(&init_user_ns
, id
);
172 if (!uid_valid(entry
->e_uid
))
176 entry
->e_gid
= make_kgid(&init_user_ns
, id
);
177 if (!gid_valid(entry
->e_gid
))
183 if (entry
->e_perm
& ~S_IRWXO
)
187 /* Solaris sometimes sets additional bits in the mask */
188 entry
->e_perm
&= S_IRWXO
;
198 cmp_acl_entry(const void *x
, const void *y
)
200 const struct posix_acl_entry
*a
= x
, *b
= y
;
202 if (a
->e_tag
!= b
->e_tag
)
203 return a
->e_tag
- b
->e_tag
;
204 else if ((a
->e_tag
== ACL_USER
) && uid_gt(a
->e_uid
, b
->e_uid
))
206 else if ((a
->e_tag
== ACL_USER
) && uid_lt(a
->e_uid
, b
->e_uid
))
208 else if ((a
->e_tag
== ACL_GROUP
) && gid_gt(a
->e_gid
, b
->e_gid
))
210 else if ((a
->e_tag
== ACL_GROUP
) && gid_lt(a
->e_gid
, b
->e_gid
))
217 * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
220 posix_acl_from_nfsacl(struct posix_acl
*acl
)
222 struct posix_acl_entry
*pa
, *pe
,
223 *group_obj
= NULL
, *mask
= NULL
;
228 sort(acl
->a_entries
, acl
->a_count
, sizeof(struct posix_acl_entry
),
229 cmp_acl_entry
, NULL
);
231 /* Find the ACL_GROUP_OBJ and ACL_MASK entries. */
232 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
246 if (acl
->a_count
== 4 && group_obj
&& mask
&&
247 mask
->e_perm
== group_obj
->e_perm
) {
248 /* remove bogus ACL_MASK entry */
249 memmove(mask
, mask
+1, (3 - (mask
- acl
->a_entries
)) *
250 sizeof(struct posix_acl_entry
));
257 * nfsacl_decode - Decode an NFSv3 ACL
259 * @buf: xdr_buf containing XDR'd ACL data to decode
260 * @base: byte offset in xdr_buf where XDR'd ACL begins
261 * @aclcnt: count of ACEs in decoded posix_acl
262 * @pacl: buffer in which to place decoded posix_acl
264 * Returns the length of the decoded ACL in bytes, or a negative errno value.
266 int nfsacl_decode(struct xdr_buf
*buf
, unsigned int base
, unsigned int *aclcnt
,
267 struct posix_acl
**pacl
)
269 struct nfsacl_decode_desc nfsacl_desc
= {
272 .xcode
= pacl
? xdr_nfsace_decode
: NULL
,
278 if (xdr_decode_word(buf
, base
, &entries
) ||
279 entries
> NFS_ACL_MAX_ENTRIES
)
281 nfsacl_desc
.desc
.array_maxlen
= entries
;
282 err
= xdr_decode_array2(buf
, base
+ 4, &nfsacl_desc
.desc
);
286 if (entries
!= nfsacl_desc
.desc
.array_len
||
287 posix_acl_from_nfsacl(nfsacl_desc
.acl
) != 0) {
288 posix_acl_release(nfsacl_desc
.acl
);
291 *pacl
= nfsacl_desc
.acl
;
295 return 8 + nfsacl_desc
.desc
.elem_size
*
296 nfsacl_desc
.desc
.array_len
;