2 * fs/nfs_common/nfsacl.c
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;
46 xdr_nfsace_encode(struct xdr_array2_desc *desc, void *elem)
48 struct nfsacl_encode_desc *nfsacl_desc =
49 (struct nfsacl_encode_desc *) desc;
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);
76 nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode,
77 struct posix_acl *acl, int encode_entries, int typeflag)
79 int entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0;
80 struct nfsacl_encode_desc nfsacl_desc = {
83 .array_len = encode_entries ? entries : 0,
84 .xcode = xdr_nfsace_encode,
92 struct posix_acl *acl2 = NULL;
94 if (entries > NFS_ACL_MAX_ENTRIES ||
95 xdr_encode_word(buf, base, entries))
97 if (encode_entries && acl && acl->a_count == 3) {
98 /* Fake up an ACL_MASK entry. */
99 acl2 = posix_acl_alloc(4, GFP_KERNEL);
102 /* Insert entries in canonical order: other orders seem
103 to confuse Solaris VxFS. */
104 acl2->a_entries[0] = acl->a_entries[0]; /* ACL_USER_OBJ */
105 acl2->a_entries[1] = acl->a_entries[1]; /* ACL_GROUP_OBJ */
106 acl2->a_entries[2] = acl->a_entries[1]; /* ACL_MASK */
107 acl2->a_entries[2].e_tag = ACL_MASK;
108 acl2->a_entries[3] = acl->a_entries[2]; /* ACL_OTHER */
109 nfsacl_desc.acl = acl2;
111 err = xdr_encode_array2(buf, base + 4, &nfsacl_desc.desc);
113 posix_acl_release(acl2);
115 err = 8 + nfsacl_desc.desc.elem_size *
116 nfsacl_desc.desc.array_len;
120 struct nfsacl_decode_desc {
121 struct xdr_array2_desc desc;
123 struct posix_acl *acl;
127 xdr_nfsace_decode(struct xdr_array2_desc *desc, void *elem)
129 struct nfsacl_decode_desc *nfsacl_desc =
130 (struct nfsacl_decode_desc *) desc;
132 struct posix_acl_entry *entry;
134 if (!nfsacl_desc->acl) {
135 if (desc->array_len > NFS_ACL_MAX_ENTRIES)
137 nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL);
138 if (!nfsacl_desc->acl)
140 nfsacl_desc->count = 0;
143 entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
144 entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT;
145 entry->e_id = ntohl(*p++);
146 entry->e_perm = ntohl(*p++);
148 switch(entry->e_tag) {
154 if (entry->e_perm & ~S_IRWXO)
158 /* Solaris sometimes sets additonal bits in the mask */
159 entry->e_perm &= S_IRWXO;
169 cmp_acl_entry(const void *x, const void *y)
171 const struct posix_acl_entry *a = x, *b = y;
173 if (a->e_tag != b->e_tag)
174 return a->e_tag - b->e_tag;
175 else if (a->e_id > b->e_id)
177 else if (a->e_id < b->e_id)
184 * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
187 posix_acl_from_nfsacl(struct posix_acl *acl)
189 struct posix_acl_entry *pa, *pe,
190 *group_obj = NULL, *mask = NULL;
195 sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry),
196 cmp_acl_entry, NULL);
198 /* Clear undefined identifier fields and find the ACL_GROUP_OBJ
199 and ACL_MASK entries. */
200 FOREACH_ACL_ENTRY(pa, acl, pe) {
203 pa->e_id = ACL_UNDEFINED_ID;
206 pa->e_id = ACL_UNDEFINED_ID;
213 pa->e_id = ACL_UNDEFINED_ID;
217 if (acl->a_count == 4 && group_obj && mask &&
218 mask->e_perm == group_obj->e_perm) {
219 /* remove bogus ACL_MASK entry */
220 memmove(mask, mask+1, (3 - (mask - acl->a_entries)) *
221 sizeof(struct posix_acl_entry));
228 nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt,
229 struct posix_acl **pacl)
231 struct nfsacl_decode_desc nfsacl_desc = {
234 .xcode = pacl ? xdr_nfsace_decode : NULL,
240 if (xdr_decode_word(buf, base, &entries) ||
241 entries > NFS_ACL_MAX_ENTRIES)
243 nfsacl_desc.desc.array_maxlen = entries;
244 err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc);
248 if (entries != nfsacl_desc.desc.array_len ||
249 posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
250 posix_acl_release(nfsacl_desc.acl);
253 *pacl = nfsacl_desc.acl;
257 return 8 + nfsacl_desc.desc.elem_size *
258 nfsacl_desc.desc.array_len;