Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / fs / nfsd / nfs4acl.c
blob9c51aff02ae28f7b77d2da214a9057da8dc06518
1 /*
2 * Common NFSv4 ACL handling code.
4 * Copyright (c) 2002, 2003 The Regents of the University of Michigan.
5 * All rights reserved.
7 * Marius Aamodt Eriksen <marius@umich.edu>
8 * Jeff Sedlak <jsedlak@umich.edu>
9 * J. Bruce Fields <bfields@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/slab.h>
38 #include <linux/nfs_fs.h>
39 #include <linux/export.h>
40 #include "acl.h"
43 /* mode bit translations: */
44 #define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
45 #define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
46 #define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
47 #define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
48 #define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
50 /* We don't support these bits; insist they be neither allowed nor denied */
51 #define NFS4_MASK_UNSUPP (NFS4_ACE_DELETE | NFS4_ACE_WRITE_OWNER \
52 | NFS4_ACE_READ_NAMED_ATTRS | NFS4_ACE_WRITE_NAMED_ATTRS)
54 /* flags used to simulate posix default ACLs */
55 #define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
56 | NFS4_ACE_DIRECTORY_INHERIT_ACE)
58 #define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
59 | NFS4_ACE_INHERIT_ONLY_ACE \
60 | NFS4_ACE_IDENTIFIER_GROUP)
62 #define MASK_EQUAL(mask1, mask2) \
63 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
65 static u32
66 mask_from_posix(unsigned short perm, unsigned int flags)
68 int mask = NFS4_ANYONE_MODE;
70 if (flags & NFS4_ACL_OWNER)
71 mask |= NFS4_OWNER_MODE;
72 if (perm & ACL_READ)
73 mask |= NFS4_READ_MODE;
74 if (perm & ACL_WRITE)
75 mask |= NFS4_WRITE_MODE;
76 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
77 mask |= NFS4_ACE_DELETE_CHILD;
78 if (perm & ACL_EXECUTE)
79 mask |= NFS4_EXECUTE_MODE;
80 return mask;
83 static u32
84 deny_mask_from_posix(unsigned short perm, u32 flags)
86 u32 mask = 0;
88 if (perm & ACL_READ)
89 mask |= NFS4_READ_MODE;
90 if (perm & ACL_WRITE)
91 mask |= NFS4_WRITE_MODE;
92 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
93 mask |= NFS4_ACE_DELETE_CHILD;
94 if (perm & ACL_EXECUTE)
95 mask |= NFS4_EXECUTE_MODE;
96 return mask;
99 /* XXX: modify functions to return NFS errors; they're only ever
100 * used by nfs code, after all.... */
102 /* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
103 * side of being more restrictive, so the mode bit mapping below is
104 * pessimistic. An optimistic version would be needed to handle DENY's,
105 * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
106 * bits. */
108 static void
109 low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
111 u32 write_mode = NFS4_WRITE_MODE;
113 if (flags & NFS4_ACL_DIR)
114 write_mode |= NFS4_ACE_DELETE_CHILD;
115 *mode = 0;
116 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
117 *mode |= ACL_READ;
118 if ((perm & write_mode) == write_mode)
119 *mode |= ACL_WRITE;
120 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
121 *mode |= ACL_EXECUTE;
124 struct ace_container {
125 struct nfs4_ace *ace;
126 struct list_head ace_l;
129 static short ace2type(struct nfs4_ace *);
130 static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
131 unsigned int);
133 struct nfs4_acl *
134 nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
135 unsigned int flags)
137 struct nfs4_acl *acl;
138 int size = 0;
140 if (pacl) {
141 if (posix_acl_valid(pacl) < 0)
142 return ERR_PTR(-EINVAL);
143 size += 2*pacl->a_count;
145 if (dpacl) {
146 if (posix_acl_valid(dpacl) < 0)
147 return ERR_PTR(-EINVAL);
148 size += 2*dpacl->a_count;
151 /* Allocate for worst case: one (deny, allow) pair each: */
152 acl = nfs4_acl_new(size);
153 if (acl == NULL)
154 return ERR_PTR(-ENOMEM);
156 if (pacl)
157 _posix_to_nfsv4_one(pacl, acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
159 if (dpacl)
160 _posix_to_nfsv4_one(dpacl, acl, flags | NFS4_ACL_TYPE_DEFAULT);
162 return acl;
165 struct posix_acl_summary {
166 unsigned short owner;
167 unsigned short users;
168 unsigned short group;
169 unsigned short groups;
170 unsigned short other;
171 unsigned short mask;
174 static void
175 summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
177 struct posix_acl_entry *pa, *pe;
180 * Only pas.users and pas.groups need initialization; previous
181 * posix_acl_valid() calls ensure that the other fields will be
182 * initialized in the following loop. But, just to placate gcc:
184 memset(pas, 0, sizeof(*pas));
185 pas->mask = 07;
187 pe = acl->a_entries + acl->a_count;
189 FOREACH_ACL_ENTRY(pa, acl, pe) {
190 switch (pa->e_tag) {
191 case ACL_USER_OBJ:
192 pas->owner = pa->e_perm;
193 break;
194 case ACL_GROUP_OBJ:
195 pas->group = pa->e_perm;
196 break;
197 case ACL_USER:
198 pas->users |= pa->e_perm;
199 break;
200 case ACL_GROUP:
201 pas->groups |= pa->e_perm;
202 break;
203 case ACL_OTHER:
204 pas->other = pa->e_perm;
205 break;
206 case ACL_MASK:
207 pas->mask = pa->e_perm;
208 break;
211 /* We'll only care about effective permissions: */
212 pas->users &= pas->mask;
213 pas->group &= pas->mask;
214 pas->groups &= pas->mask;
217 /* We assume the acl has been verified with posix_acl_valid. */
218 static void
219 _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
220 unsigned int flags)
222 struct posix_acl_entry *pa, *group_owner_entry;
223 struct nfs4_ace *ace;
224 struct posix_acl_summary pas;
225 unsigned short deny;
226 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
227 NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
229 BUG_ON(pacl->a_count < 3);
230 summarize_posix_acl(pacl, &pas);
232 pa = pacl->a_entries;
233 ace = acl->aces + acl->naces;
235 /* We could deny everything not granted by the owner: */
236 deny = ~pas.owner;
238 * but it is equivalent (and simpler) to deny only what is not
239 * granted by later entries:
241 deny &= pas.users | pas.group | pas.groups | pas.other;
242 if (deny) {
243 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
244 ace->flag = eflag;
245 ace->access_mask = deny_mask_from_posix(deny, flags);
246 ace->whotype = NFS4_ACL_WHO_OWNER;
247 ace++;
248 acl->naces++;
251 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
252 ace->flag = eflag;
253 ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
254 ace->whotype = NFS4_ACL_WHO_OWNER;
255 ace++;
256 acl->naces++;
257 pa++;
259 while (pa->e_tag == ACL_USER) {
260 deny = ~(pa->e_perm & pas.mask);
261 deny &= pas.groups | pas.group | pas.other;
262 if (deny) {
263 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
264 ace->flag = eflag;
265 ace->access_mask = deny_mask_from_posix(deny, flags);
266 ace->whotype = NFS4_ACL_WHO_NAMED;
267 ace->who = pa->e_id;
268 ace++;
269 acl->naces++;
271 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
272 ace->flag = eflag;
273 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
274 flags);
275 ace->whotype = NFS4_ACL_WHO_NAMED;
276 ace->who = pa->e_id;
277 ace++;
278 acl->naces++;
279 pa++;
282 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
283 * since a user can be in more than one group. */
285 /* allow ACEs */
287 group_owner_entry = pa;
289 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
290 ace->flag = eflag;
291 ace->access_mask = mask_from_posix(pas.group, flags);
292 ace->whotype = NFS4_ACL_WHO_GROUP;
293 ace++;
294 acl->naces++;
295 pa++;
297 while (pa->e_tag == ACL_GROUP) {
298 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
299 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
300 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
301 flags);
302 ace->whotype = NFS4_ACL_WHO_NAMED;
303 ace->who = pa->e_id;
304 ace++;
305 acl->naces++;
306 pa++;
309 /* deny ACEs */
311 pa = group_owner_entry;
313 deny = ~pas.group & pas.other;
314 if (deny) {
315 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
316 ace->flag = eflag;
317 ace->access_mask = deny_mask_from_posix(deny, flags);
318 ace->whotype = NFS4_ACL_WHO_GROUP;
319 ace++;
320 acl->naces++;
322 pa++;
324 while (pa->e_tag == ACL_GROUP) {
325 deny = ~(pa->e_perm & pas.mask);
326 deny &= pas.other;
327 if (deny) {
328 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
329 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
330 ace->access_mask = deny_mask_from_posix(deny, flags);
331 ace->whotype = NFS4_ACL_WHO_NAMED;
332 ace->who = pa->e_id;
333 ace++;
334 acl->naces++;
336 pa++;
339 if (pa->e_tag == ACL_MASK)
340 pa++;
341 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
342 ace->flag = eflag;
343 ace->access_mask = mask_from_posix(pa->e_perm, flags);
344 ace->whotype = NFS4_ACL_WHO_EVERYONE;
345 acl->naces++;
348 static void
349 sort_pacl_range(struct posix_acl *pacl, int start, int end) {
350 int sorted = 0, i;
351 struct posix_acl_entry tmp;
353 /* We just do a bubble sort; easy to do in place, and we're not
354 * expecting acl's to be long enough to justify anything more. */
355 while (!sorted) {
356 sorted = 1;
357 for (i = start; i < end; i++) {
358 if (pacl->a_entries[i].e_id
359 > pacl->a_entries[i+1].e_id) {
360 sorted = 0;
361 tmp = pacl->a_entries[i];
362 pacl->a_entries[i] = pacl->a_entries[i+1];
363 pacl->a_entries[i+1] = tmp;
369 static void
370 sort_pacl(struct posix_acl *pacl)
372 /* posix_acl_valid requires that users and groups be in order
373 * by uid/gid. */
374 int i, j;
376 if (pacl->a_count <= 4)
377 return; /* no users or groups */
378 i = 1;
379 while (pacl->a_entries[i].e_tag == ACL_USER)
380 i++;
381 sort_pacl_range(pacl, 1, i-1);
383 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
384 j = ++i;
385 while (pacl->a_entries[j].e_tag == ACL_GROUP)
386 j++;
387 sort_pacl_range(pacl, i, j-1);
388 return;
392 * While processing the NFSv4 ACE, this maintains bitmasks representing
393 * which permission bits have been allowed and which denied to a given
394 * entity: */
395 struct posix_ace_state {
396 u32 allow;
397 u32 deny;
400 struct posix_user_ace_state {
401 uid_t uid;
402 struct posix_ace_state perms;
405 struct posix_ace_state_array {
406 int n;
407 struct posix_user_ace_state aces[];
411 * While processing the NFSv4 ACE, this maintains the partial permissions
412 * calculated so far: */
414 struct posix_acl_state {
415 int empty;
416 struct posix_ace_state owner;
417 struct posix_ace_state group;
418 struct posix_ace_state other;
419 struct posix_ace_state everyone;
420 struct posix_ace_state mask; /* Deny unused in this case */
421 struct posix_ace_state_array *users;
422 struct posix_ace_state_array *groups;
425 static int
426 init_state(struct posix_acl_state *state, int cnt)
428 int alloc;
430 memset(state, 0, sizeof(struct posix_acl_state));
431 state->empty = 1;
433 * In the worst case, each individual acl could be for a distinct
434 * named user or group, but we don't no which, so we allocate
435 * enough space for either:
437 alloc = sizeof(struct posix_ace_state_array)
438 + cnt*sizeof(struct posix_user_ace_state);
439 state->users = kzalloc(alloc, GFP_KERNEL);
440 if (!state->users)
441 return -ENOMEM;
442 state->groups = kzalloc(alloc, GFP_KERNEL);
443 if (!state->groups) {
444 kfree(state->users);
445 return -ENOMEM;
447 return 0;
450 static void
451 free_state(struct posix_acl_state *state) {
452 kfree(state->users);
453 kfree(state->groups);
456 static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
458 state->mask.allow |= astate->allow;
462 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
463 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
464 * to traditional read/write/execute permissions.
466 * It's problematic to reject acls that use certain mode bits, because it
467 * places the burden on users to learn the rules about which bits one
468 * particular server sets, without giving the user a lot of help--we return an
469 * error that could mean any number of different things. To make matters
470 * worse, the problematic bits might be introduced by some application that's
471 * automatically mapping from some other acl model.
473 * So wherever possible we accept anything, possibly erring on the side of
474 * denying more permissions than necessary.
476 * However we do reject *explicit* DENY's of a few bits representing
477 * permissions we could never deny:
480 static inline int check_deny(u32 mask, int isowner)
482 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
483 return -EINVAL;
484 if (!isowner)
485 return 0;
486 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
487 return -EINVAL;
488 return 0;
491 static struct posix_acl *
492 posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
494 struct posix_acl_entry *pace;
495 struct posix_acl *pacl;
496 int nace;
497 int i, error = 0;
500 * ACLs with no ACEs are treated differently in the inheritable
501 * and effective cases: when there are no inheritable ACEs, we
502 * set a zero-length default posix acl:
504 if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT)) {
505 pacl = posix_acl_alloc(0, GFP_KERNEL);
506 return pacl ? pacl : ERR_PTR(-ENOMEM);
509 * When there are no effective ACEs, the following will end
510 * up setting a 3-element effective posix ACL with all
511 * permissions zero.
513 nace = 4 + state->users->n + state->groups->n;
514 pacl = posix_acl_alloc(nace, GFP_KERNEL);
515 if (!pacl)
516 return ERR_PTR(-ENOMEM);
518 pace = pacl->a_entries;
519 pace->e_tag = ACL_USER_OBJ;
520 error = check_deny(state->owner.deny, 1);
521 if (error)
522 goto out_err;
523 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
524 pace->e_id = ACL_UNDEFINED_ID;
526 for (i=0; i < state->users->n; i++) {
527 pace++;
528 pace->e_tag = ACL_USER;
529 error = check_deny(state->users->aces[i].perms.deny, 0);
530 if (error)
531 goto out_err;
532 low_mode_from_nfs4(state->users->aces[i].perms.allow,
533 &pace->e_perm, flags);
534 pace->e_id = state->users->aces[i].uid;
535 add_to_mask(state, &state->users->aces[i].perms);
538 pace++;
539 pace->e_tag = ACL_GROUP_OBJ;
540 error = check_deny(state->group.deny, 0);
541 if (error)
542 goto out_err;
543 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
544 pace->e_id = ACL_UNDEFINED_ID;
545 add_to_mask(state, &state->group);
547 for (i=0; i < state->groups->n; i++) {
548 pace++;
549 pace->e_tag = ACL_GROUP;
550 error = check_deny(state->groups->aces[i].perms.deny, 0);
551 if (error)
552 goto out_err;
553 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
554 &pace->e_perm, flags);
555 pace->e_id = state->groups->aces[i].uid;
556 add_to_mask(state, &state->groups->aces[i].perms);
559 pace++;
560 pace->e_tag = ACL_MASK;
561 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
562 pace->e_id = ACL_UNDEFINED_ID;
564 pace++;
565 pace->e_tag = ACL_OTHER;
566 error = check_deny(state->other.deny, 0);
567 if (error)
568 goto out_err;
569 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
570 pace->e_id = ACL_UNDEFINED_ID;
572 return pacl;
573 out_err:
574 posix_acl_release(pacl);
575 return ERR_PTR(error);
578 static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
580 /* Allow all bits in the mask not already denied: */
581 astate->allow |= mask & ~astate->deny;
584 static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
586 /* Deny all bits in the mask not already allowed: */
587 astate->deny |= mask & ~astate->allow;
590 static int find_uid(struct posix_acl_state *state, struct posix_ace_state_array *a, uid_t uid)
592 int i;
594 for (i = 0; i < a->n; i++)
595 if (a->aces[i].uid == uid)
596 return i;
597 /* Not found: */
598 a->n++;
599 a->aces[i].uid = uid;
600 a->aces[i].perms.allow = state->everyone.allow;
601 a->aces[i].perms.deny = state->everyone.deny;
603 return i;
606 static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
608 int i;
610 for (i=0; i < a->n; i++)
611 deny_bits(&a->aces[i].perms, mask);
614 static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
616 int i;
618 for (i=0; i < a->n; i++)
619 allow_bits(&a->aces[i].perms, mask);
622 static void process_one_v4_ace(struct posix_acl_state *state,
623 struct nfs4_ace *ace)
625 u32 mask = ace->access_mask;
626 int i;
628 state->empty = 0;
630 switch (ace2type(ace)) {
631 case ACL_USER_OBJ:
632 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
633 allow_bits(&state->owner, mask);
634 } else {
635 deny_bits(&state->owner, mask);
637 break;
638 case ACL_USER:
639 i = find_uid(state, state->users, ace->who);
640 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
641 allow_bits(&state->users->aces[i].perms, mask);
642 } else {
643 deny_bits(&state->users->aces[i].perms, mask);
644 mask = state->users->aces[i].perms.deny;
645 deny_bits(&state->owner, mask);
647 break;
648 case ACL_GROUP_OBJ:
649 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
650 allow_bits(&state->group, mask);
651 } else {
652 deny_bits(&state->group, mask);
653 mask = state->group.deny;
654 deny_bits(&state->owner, mask);
655 deny_bits(&state->everyone, mask);
656 deny_bits_array(state->users, mask);
657 deny_bits_array(state->groups, mask);
659 break;
660 case ACL_GROUP:
661 i = find_uid(state, state->groups, ace->who);
662 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
663 allow_bits(&state->groups->aces[i].perms, mask);
664 } else {
665 deny_bits(&state->groups->aces[i].perms, mask);
666 mask = state->groups->aces[i].perms.deny;
667 deny_bits(&state->owner, mask);
668 deny_bits(&state->group, mask);
669 deny_bits(&state->everyone, mask);
670 deny_bits_array(state->users, mask);
671 deny_bits_array(state->groups, mask);
673 break;
674 case ACL_OTHER:
675 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
676 allow_bits(&state->owner, mask);
677 allow_bits(&state->group, mask);
678 allow_bits(&state->other, mask);
679 allow_bits(&state->everyone, mask);
680 allow_bits_array(state->users, mask);
681 allow_bits_array(state->groups, mask);
682 } else {
683 deny_bits(&state->owner, mask);
684 deny_bits(&state->group, mask);
685 deny_bits(&state->other, mask);
686 deny_bits(&state->everyone, mask);
687 deny_bits_array(state->users, mask);
688 deny_bits_array(state->groups, mask);
693 int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
694 struct posix_acl **dpacl, unsigned int flags)
696 struct posix_acl_state effective_acl_state, default_acl_state;
697 struct nfs4_ace *ace;
698 int ret;
700 ret = init_state(&effective_acl_state, acl->naces);
701 if (ret)
702 return ret;
703 ret = init_state(&default_acl_state, acl->naces);
704 if (ret)
705 goto out_estate;
706 ret = -EINVAL;
707 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
708 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
709 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
710 goto out_dstate;
711 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
712 goto out_dstate;
713 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
714 process_one_v4_ace(&effective_acl_state, ace);
715 continue;
717 if (!(flags & NFS4_ACL_DIR))
718 goto out_dstate;
720 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
721 * is set, we're effectively turning on the other. That's OK,
722 * according to rfc 3530.
724 process_one_v4_ace(&default_acl_state, ace);
726 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
727 process_one_v4_ace(&effective_acl_state, ace);
729 *pacl = posix_state_to_acl(&effective_acl_state, flags);
730 if (IS_ERR(*pacl)) {
731 ret = PTR_ERR(*pacl);
732 *pacl = NULL;
733 goto out_dstate;
735 *dpacl = posix_state_to_acl(&default_acl_state,
736 flags | NFS4_ACL_TYPE_DEFAULT);
737 if (IS_ERR(*dpacl)) {
738 ret = PTR_ERR(*dpacl);
739 *dpacl = NULL;
740 posix_acl_release(*pacl);
741 *pacl = NULL;
742 goto out_dstate;
744 sort_pacl(*pacl);
745 sort_pacl(*dpacl);
746 ret = 0;
747 out_dstate:
748 free_state(&default_acl_state);
749 out_estate:
750 free_state(&effective_acl_state);
751 return ret;
754 static short
755 ace2type(struct nfs4_ace *ace)
757 switch (ace->whotype) {
758 case NFS4_ACL_WHO_NAMED:
759 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
760 ACL_GROUP : ACL_USER);
761 case NFS4_ACL_WHO_OWNER:
762 return ACL_USER_OBJ;
763 case NFS4_ACL_WHO_GROUP:
764 return ACL_GROUP_OBJ;
765 case NFS4_ACL_WHO_EVERYONE:
766 return ACL_OTHER;
768 BUG();
769 return -1;
772 EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
773 EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
775 struct nfs4_acl *
776 nfs4_acl_new(int n)
778 struct nfs4_acl *acl;
780 acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL);
781 if (acl == NULL)
782 return NULL;
783 acl->naces = 0;
784 return acl;
787 static struct {
788 char *string;
789 int stringlen;
790 int type;
791 } s2t_map[] = {
793 .string = "OWNER@",
794 .stringlen = sizeof("OWNER@") - 1,
795 .type = NFS4_ACL_WHO_OWNER,
798 .string = "GROUP@",
799 .stringlen = sizeof("GROUP@") - 1,
800 .type = NFS4_ACL_WHO_GROUP,
803 .string = "EVERYONE@",
804 .stringlen = sizeof("EVERYONE@") - 1,
805 .type = NFS4_ACL_WHO_EVERYONE,
810 nfs4_acl_get_whotype(char *p, u32 len)
812 int i;
814 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
815 if (s2t_map[i].stringlen == len &&
816 0 == memcmp(s2t_map[i].string, p, len))
817 return s2t_map[i].type;
819 return NFS4_ACL_WHO_NAMED;
823 nfs4_acl_write_who(int who, char *p)
825 int i;
827 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
828 if (s2t_map[i].type == who) {
829 memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
830 return s2t_map[i].stringlen;
833 BUG();
834 return -1;
837 EXPORT_SYMBOL(nfs4_acl_new);
838 EXPORT_SYMBOL(nfs4_acl_get_whotype);
839 EXPORT_SYMBOL(nfs4_acl_write_who);