Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / nfsd / nfs4acl.c
blobb6ed38380ab805179f8354ab9454fc533e50da53
1 /*
2 * fs/nfs4acl/acl.c
4 * Common NFSv4 ACL handling code.
6 * Copyright (c) 2002, 2003 The Regents of the University of Michigan.
7 * All rights reserved.
9 * Marius Aamodt Eriksen <marius@umich.edu>
10 * Jeff Sedlak <jsedlak@umich.edu>
11 * J. Bruce Fields <bfields@umich.edu>
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <linux/list.h>
42 #include <linux/types.h>
43 #include <linux/fs.h>
44 #include <linux/module.h>
45 #include <linux/nfs_fs.h>
46 #include <linux/posix_acl.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs4_acl.h>
51 /* mode bit translations: */
52 #define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
53 #define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
54 #define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
55 #define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
56 #define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
58 /* We don't support these bits; insist they be neither allowed nor denied */
59 #define NFS4_MASK_UNSUPP (NFS4_ACE_DELETE | NFS4_ACE_WRITE_OWNER \
60 | NFS4_ACE_READ_NAMED_ATTRS | NFS4_ACE_WRITE_NAMED_ATTRS)
62 /* flags used to simulate posix default ACLs */
63 #define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
64 | NFS4_ACE_DIRECTORY_INHERIT_ACE)
66 #define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
67 | NFS4_ACE_INHERIT_ONLY_ACE \
68 | NFS4_ACE_IDENTIFIER_GROUP)
70 #define MASK_EQUAL(mask1, mask2) \
71 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
73 static u32
74 mask_from_posix(unsigned short perm, unsigned int flags)
76 int mask = NFS4_ANYONE_MODE;
78 if (flags & NFS4_ACL_OWNER)
79 mask |= NFS4_OWNER_MODE;
80 if (perm & ACL_READ)
81 mask |= NFS4_READ_MODE;
82 if (perm & ACL_WRITE)
83 mask |= NFS4_WRITE_MODE;
84 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
85 mask |= NFS4_ACE_DELETE_CHILD;
86 if (perm & ACL_EXECUTE)
87 mask |= NFS4_EXECUTE_MODE;
88 return mask;
91 static u32
92 deny_mask_from_posix(unsigned short perm, u32 flags)
94 u32 mask = 0;
96 if (perm & ACL_READ)
97 mask |= NFS4_READ_MODE;
98 if (perm & ACL_WRITE)
99 mask |= NFS4_WRITE_MODE;
100 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
101 mask |= NFS4_ACE_DELETE_CHILD;
102 if (perm & ACL_EXECUTE)
103 mask |= NFS4_EXECUTE_MODE;
104 return mask;
107 /* XXX: modify functions to return NFS errors; they're only ever
108 * used by nfs code, after all.... */
110 /* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
111 * side of being more restrictive, so the mode bit mapping below is
112 * pessimistic. An optimistic version would be needed to handle DENY's,
113 * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
114 * bits. */
116 static void
117 low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
119 u32 write_mode = NFS4_WRITE_MODE;
121 if (flags & NFS4_ACL_DIR)
122 write_mode |= NFS4_ACE_DELETE_CHILD;
123 *mode = 0;
124 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
125 *mode |= ACL_READ;
126 if ((perm & write_mode) == write_mode)
127 *mode |= ACL_WRITE;
128 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
129 *mode |= ACL_EXECUTE;
132 struct ace_container {
133 struct nfs4_ace *ace;
134 struct list_head ace_l;
137 static short ace2type(struct nfs4_ace *);
138 static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
139 unsigned int);
141 struct nfs4_acl *
142 nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
143 unsigned int flags)
145 struct nfs4_acl *acl;
146 int size = 0;
148 if (pacl) {
149 if (posix_acl_valid(pacl) < 0)
150 return ERR_PTR(-EINVAL);
151 size += 2*pacl->a_count;
153 if (dpacl) {
154 if (posix_acl_valid(dpacl) < 0)
155 return ERR_PTR(-EINVAL);
156 size += 2*dpacl->a_count;
159 /* Allocate for worst case: one (deny, allow) pair each: */
160 acl = nfs4_acl_new(size);
161 if (acl == NULL)
162 return ERR_PTR(-ENOMEM);
164 if (pacl)
165 _posix_to_nfsv4_one(pacl, acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
167 if (dpacl)
168 _posix_to_nfsv4_one(dpacl, acl, flags | NFS4_ACL_TYPE_DEFAULT);
170 return acl;
173 struct posix_acl_summary {
174 unsigned short owner;
175 unsigned short users;
176 unsigned short group;
177 unsigned short groups;
178 unsigned short other;
179 unsigned short mask;
182 static void
183 summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
185 struct posix_acl_entry *pa, *pe;
188 * Only pas.users and pas.groups need initialization; previous
189 * posix_acl_valid() calls ensure that the other fields will be
190 * initialized in the following loop. But, just to placate gcc:
192 memset(pas, 0, sizeof(*pas));
193 pas->mask = 07;
195 pe = acl->a_entries + acl->a_count;
197 FOREACH_ACL_ENTRY(pa, acl, pe) {
198 switch (pa->e_tag) {
199 case ACL_USER_OBJ:
200 pas->owner = pa->e_perm;
201 break;
202 case ACL_GROUP_OBJ:
203 pas->group = pa->e_perm;
204 break;
205 case ACL_USER:
206 pas->users |= pa->e_perm;
207 break;
208 case ACL_GROUP:
209 pas->groups |= pa->e_perm;
210 break;
211 case ACL_OTHER:
212 pas->other = pa->e_perm;
213 break;
214 case ACL_MASK:
215 pas->mask = pa->e_perm;
216 break;
219 /* We'll only care about effective permissions: */
220 pas->users &= pas->mask;
221 pas->group &= pas->mask;
222 pas->groups &= pas->mask;
225 /* We assume the acl has been verified with posix_acl_valid. */
226 static void
227 _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
228 unsigned int flags)
230 struct posix_acl_entry *pa, *group_owner_entry;
231 struct nfs4_ace *ace;
232 struct posix_acl_summary pas;
233 unsigned short deny;
234 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
235 NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
237 BUG_ON(pacl->a_count < 3);
238 summarize_posix_acl(pacl, &pas);
240 pa = pacl->a_entries;
241 ace = acl->aces + acl->naces;
243 /* We could deny everything not granted by the owner: */
244 deny = ~pas.owner;
246 * but it is equivalent (and simpler) to deny only what is not
247 * granted by later entries:
249 deny &= pas.users | pas.group | pas.groups | pas.other;
250 if (deny) {
251 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
252 ace->flag = eflag;
253 ace->access_mask = deny_mask_from_posix(deny, flags);
254 ace->whotype = NFS4_ACL_WHO_OWNER;
255 ace++;
256 acl->naces++;
259 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
260 ace->flag = eflag;
261 ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
262 ace->whotype = NFS4_ACL_WHO_OWNER;
263 ace++;
264 acl->naces++;
265 pa++;
267 while (pa->e_tag == ACL_USER) {
268 deny = ~(pa->e_perm & pas.mask);
269 deny &= pas.groups | pas.group | pas.other;
270 if (deny) {
271 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
272 ace->flag = eflag;
273 ace->access_mask = deny_mask_from_posix(deny, flags);
274 ace->whotype = NFS4_ACL_WHO_NAMED;
275 ace->who = pa->e_id;
276 ace++;
277 acl->naces++;
279 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
280 ace->flag = eflag;
281 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
282 flags);
283 ace->whotype = NFS4_ACL_WHO_NAMED;
284 ace->who = pa->e_id;
285 ace++;
286 acl->naces++;
287 pa++;
290 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
291 * since a user can be in more than one group. */
293 /* allow ACEs */
295 group_owner_entry = pa;
297 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
298 ace->flag = eflag;
299 ace->access_mask = mask_from_posix(pas.group, flags);
300 ace->whotype = NFS4_ACL_WHO_GROUP;
301 ace++;
302 acl->naces++;
303 pa++;
305 while (pa->e_tag == ACL_GROUP) {
306 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
307 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
308 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
309 flags);
310 ace->whotype = NFS4_ACL_WHO_NAMED;
311 ace->who = pa->e_id;
312 ace++;
313 acl->naces++;
314 pa++;
317 /* deny ACEs */
319 pa = group_owner_entry;
321 deny = ~pas.group & pas.other;
322 if (deny) {
323 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
324 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
325 ace->access_mask = deny_mask_from_posix(deny, flags);
326 ace->whotype = NFS4_ACL_WHO_GROUP;
327 ace++;
328 acl->naces++;
330 pa++;
332 while (pa->e_tag == ACL_GROUP) {
333 deny = ~(pa->e_perm & pas.mask);
334 deny &= pas.other;
335 if (deny) {
336 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
337 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
338 ace->access_mask = mask_from_posix(deny, flags);
339 ace->whotype = NFS4_ACL_WHO_NAMED;
340 ace->who = pa->e_id;
341 ace++;
342 acl->naces++;
344 pa++;
347 if (pa->e_tag == ACL_MASK)
348 pa++;
349 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
350 ace->flag = eflag;
351 ace->access_mask = mask_from_posix(pa->e_perm, flags);
352 ace->whotype = NFS4_ACL_WHO_EVERYONE;
353 acl->naces++;
356 static void
357 sort_pacl_range(struct posix_acl *pacl, int start, int end) {
358 int sorted = 0, i;
359 struct posix_acl_entry tmp;
361 /* We just do a bubble sort; easy to do in place, and we're not
362 * expecting acl's to be long enough to justify anything more. */
363 while (!sorted) {
364 sorted = 1;
365 for (i = start; i < end; i++) {
366 if (pacl->a_entries[i].e_id
367 > pacl->a_entries[i+1].e_id) {
368 sorted = 0;
369 tmp = pacl->a_entries[i];
370 pacl->a_entries[i] = pacl->a_entries[i+1];
371 pacl->a_entries[i+1] = tmp;
377 static void
378 sort_pacl(struct posix_acl *pacl)
380 /* posix_acl_valid requires that users and groups be in order
381 * by uid/gid. */
382 int i, j;
384 if (pacl->a_count <= 4)
385 return; /* no users or groups */
386 i = 1;
387 while (pacl->a_entries[i].e_tag == ACL_USER)
388 i++;
389 sort_pacl_range(pacl, 1, i-1);
391 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
392 j = i++;
393 while (pacl->a_entries[j].e_tag == ACL_GROUP)
394 j++;
395 sort_pacl_range(pacl, i, j-1);
396 return;
400 * While processing the NFSv4 ACE, this maintains bitmasks representing
401 * which permission bits have been allowed and which denied to a given
402 * entity: */
403 struct posix_ace_state {
404 u32 allow;
405 u32 deny;
408 struct posix_user_ace_state {
409 uid_t uid;
410 struct posix_ace_state perms;
413 struct posix_ace_state_array {
414 int n;
415 struct posix_user_ace_state aces[];
419 * While processing the NFSv4 ACE, this maintains the partial permissions
420 * calculated so far: */
422 struct posix_acl_state {
423 int empty;
424 struct posix_ace_state owner;
425 struct posix_ace_state group;
426 struct posix_ace_state other;
427 struct posix_ace_state everyone;
428 struct posix_ace_state mask; /* Deny unused in this case */
429 struct posix_ace_state_array *users;
430 struct posix_ace_state_array *groups;
433 static int
434 init_state(struct posix_acl_state *state, int cnt)
436 int alloc;
438 memset(state, 0, sizeof(struct posix_acl_state));
439 state->empty = 1;
441 * In the worst case, each individual acl could be for a distinct
442 * named user or group, but we don't no which, so we allocate
443 * enough space for either:
445 alloc = sizeof(struct posix_ace_state_array)
446 + cnt*sizeof(struct posix_ace_state);
447 state->users = kzalloc(alloc, GFP_KERNEL);
448 if (!state->users)
449 return -ENOMEM;
450 state->groups = kzalloc(alloc, GFP_KERNEL);
451 if (!state->groups) {
452 kfree(state->users);
453 return -ENOMEM;
455 return 0;
458 static void
459 free_state(struct posix_acl_state *state) {
460 kfree(state->users);
461 kfree(state->groups);
464 static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
466 state->mask.allow |= astate->allow;
470 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
471 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
472 * to traditional read/write/execute permissions.
474 * It's problematic to reject acls that use certain mode bits, because it
475 * places the burden on users to learn the rules about which bits one
476 * particular server sets, without giving the user a lot of help--we return an
477 * error that could mean any number of different things. To make matters
478 * worse, the problematic bits might be introduced by some application that's
479 * automatically mapping from some other acl model.
481 * So wherever possible we accept anything, possibly erring on the side of
482 * denying more permissions than necessary.
484 * However we do reject *explicit* DENY's of a few bits representing
485 * permissions we could never deny:
488 static inline int check_deny(u32 mask, int isowner)
490 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
491 return -EINVAL;
492 if (!isowner)
493 return 0;
494 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
495 return -EINVAL;
496 return 0;
499 static struct posix_acl *
500 posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
502 struct posix_acl_entry *pace;
503 struct posix_acl *pacl;
504 int nace;
505 int i, error = 0;
508 * ACLs with no ACEs are treated differently in the inheritable
509 * and effective cases: when there are no inheritable ACEs, we
510 * set a zero-length default posix acl:
512 if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT)) {
513 pacl = posix_acl_alloc(0, GFP_KERNEL);
514 return pacl ? pacl : ERR_PTR(-ENOMEM);
517 * When there are no effective ACEs, the following will end
518 * up setting a 3-element effective posix ACL with all
519 * permissions zero.
521 nace = 4 + state->users->n + state->groups->n;
522 pacl = posix_acl_alloc(nace, GFP_KERNEL);
523 if (!pacl)
524 return ERR_PTR(-ENOMEM);
526 pace = pacl->a_entries;
527 pace->e_tag = ACL_USER_OBJ;
528 error = check_deny(state->owner.deny, 1);
529 if (error)
530 goto out_err;
531 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
532 pace->e_id = ACL_UNDEFINED_ID;
534 for (i=0; i < state->users->n; i++) {
535 pace++;
536 pace->e_tag = ACL_USER;
537 error = check_deny(state->users->aces[i].perms.deny, 0);
538 if (error)
539 goto out_err;
540 low_mode_from_nfs4(state->users->aces[i].perms.allow,
541 &pace->e_perm, flags);
542 pace->e_id = state->users->aces[i].uid;
543 add_to_mask(state, &state->users->aces[i].perms);
546 pace++;
547 pace->e_tag = ACL_GROUP_OBJ;
548 error = check_deny(state->group.deny, 0);
549 if (error)
550 goto out_err;
551 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
552 pace->e_id = ACL_UNDEFINED_ID;
553 add_to_mask(state, &state->group);
555 for (i=0; i < state->groups->n; i++) {
556 pace++;
557 pace->e_tag = ACL_GROUP;
558 error = check_deny(state->groups->aces[i].perms.deny, 0);
559 if (error)
560 goto out_err;
561 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
562 &pace->e_perm, flags);
563 pace->e_id = state->groups->aces[i].uid;
564 add_to_mask(state, &state->groups->aces[i].perms);
567 pace++;
568 pace->e_tag = ACL_MASK;
569 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
570 pace->e_id = ACL_UNDEFINED_ID;
572 pace++;
573 pace->e_tag = ACL_OTHER;
574 error = check_deny(state->other.deny, 0);
575 if (error)
576 goto out_err;
577 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
578 pace->e_id = ACL_UNDEFINED_ID;
580 return pacl;
581 out_err:
582 posix_acl_release(pacl);
583 return ERR_PTR(error);
586 static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
588 /* Allow all bits in the mask not already denied: */
589 astate->allow |= mask & ~astate->deny;
592 static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
594 /* Deny all bits in the mask not already allowed: */
595 astate->deny |= mask & ~astate->allow;
598 static int find_uid(struct posix_acl_state *state, struct posix_ace_state_array *a, uid_t uid)
600 int i;
602 for (i = 0; i < a->n; i++)
603 if (a->aces[i].uid == uid)
604 return i;
605 /* Not found: */
606 a->n++;
607 a->aces[i].uid = uid;
608 a->aces[i].perms.allow = state->everyone.allow;
609 a->aces[i].perms.deny = state->everyone.deny;
611 return i;
614 static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
616 int i;
618 for (i=0; i < a->n; i++)
619 deny_bits(&a->aces[i].perms, mask);
622 static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
624 int i;
626 for (i=0; i < a->n; i++)
627 allow_bits(&a->aces[i].perms, mask);
630 static void process_one_v4_ace(struct posix_acl_state *state,
631 struct nfs4_ace *ace)
633 u32 mask = ace->access_mask;
634 int i;
636 state->empty = 0;
638 switch (ace2type(ace)) {
639 case ACL_USER_OBJ:
640 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
641 allow_bits(&state->owner, mask);
642 } else {
643 deny_bits(&state->owner, mask);
645 break;
646 case ACL_USER:
647 i = find_uid(state, state->users, ace->who);
648 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
649 allow_bits(&state->users->aces[i].perms, mask);
650 } else {
651 deny_bits(&state->users->aces[i].perms, mask);
652 mask = state->users->aces[i].perms.deny;
653 deny_bits(&state->owner, mask);
655 break;
656 case ACL_GROUP_OBJ:
657 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
658 allow_bits(&state->group, mask);
659 } else {
660 deny_bits(&state->group, mask);
661 mask = state->group.deny;
662 deny_bits(&state->owner, mask);
663 deny_bits(&state->everyone, mask);
664 deny_bits_array(state->users, mask);
665 deny_bits_array(state->groups, mask);
667 break;
668 case ACL_GROUP:
669 i = find_uid(state, state->groups, ace->who);
670 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
671 allow_bits(&state->groups->aces[i].perms, mask);
672 } else {
673 deny_bits(&state->groups->aces[i].perms, mask);
674 mask = state->groups->aces[i].perms.deny;
675 deny_bits(&state->owner, mask);
676 deny_bits(&state->group, mask);
677 deny_bits(&state->everyone, mask);
678 deny_bits_array(state->users, mask);
679 deny_bits_array(state->groups, mask);
681 break;
682 case ACL_OTHER:
683 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
684 allow_bits(&state->owner, mask);
685 allow_bits(&state->group, mask);
686 allow_bits(&state->other, mask);
687 allow_bits(&state->everyone, mask);
688 allow_bits_array(state->users, mask);
689 allow_bits_array(state->groups, mask);
690 } else {
691 deny_bits(&state->owner, mask);
692 deny_bits(&state->group, mask);
693 deny_bits(&state->other, mask);
694 deny_bits(&state->everyone, mask);
695 deny_bits_array(state->users, mask);
696 deny_bits_array(state->groups, mask);
701 int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
702 struct posix_acl **dpacl, unsigned int flags)
704 struct posix_acl_state effective_acl_state, default_acl_state;
705 struct nfs4_ace *ace;
706 int ret;
708 ret = init_state(&effective_acl_state, acl->naces);
709 if (ret)
710 return ret;
711 ret = init_state(&default_acl_state, acl->naces);
712 if (ret)
713 goto out_estate;
714 ret = -EINVAL;
715 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
716 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
717 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
718 goto out_dstate;
719 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
720 goto out_dstate;
721 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
722 process_one_v4_ace(&effective_acl_state, ace);
723 continue;
725 if (!(flags & NFS4_ACL_DIR))
726 goto out_dstate;
728 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
729 * is set, we're effectively turning on the other. That's OK,
730 * according to rfc 3530.
732 process_one_v4_ace(&default_acl_state, ace);
734 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
735 process_one_v4_ace(&effective_acl_state, ace);
737 *pacl = posix_state_to_acl(&effective_acl_state, flags);
738 if (IS_ERR(*pacl)) {
739 ret = PTR_ERR(*pacl);
740 *pacl = NULL;
741 goto out_dstate;
743 *dpacl = posix_state_to_acl(&default_acl_state,
744 flags | NFS4_ACL_TYPE_DEFAULT);
745 if (IS_ERR(*dpacl)) {
746 ret = PTR_ERR(*dpacl);
747 *dpacl = NULL;
748 posix_acl_release(*pacl);
749 *pacl = NULL;
750 goto out_dstate;
752 sort_pacl(*pacl);
753 sort_pacl(*dpacl);
754 ret = 0;
755 out_dstate:
756 free_state(&default_acl_state);
757 out_estate:
758 free_state(&effective_acl_state);
759 return ret;
762 static short
763 ace2type(struct nfs4_ace *ace)
765 switch (ace->whotype) {
766 case NFS4_ACL_WHO_NAMED:
767 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
768 ACL_GROUP : ACL_USER);
769 case NFS4_ACL_WHO_OWNER:
770 return ACL_USER_OBJ;
771 case NFS4_ACL_WHO_GROUP:
772 return ACL_GROUP_OBJ;
773 case NFS4_ACL_WHO_EVERYONE:
774 return ACL_OTHER;
776 BUG();
777 return -1;
780 EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
781 EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
783 struct nfs4_acl *
784 nfs4_acl_new(int n)
786 struct nfs4_acl *acl;
788 acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL);
789 if (acl == NULL)
790 return NULL;
791 acl->naces = 0;
792 return acl;
795 static struct {
796 char *string;
797 int stringlen;
798 int type;
799 } s2t_map[] = {
801 .string = "OWNER@",
802 .stringlen = sizeof("OWNER@") - 1,
803 .type = NFS4_ACL_WHO_OWNER,
806 .string = "GROUP@",
807 .stringlen = sizeof("GROUP@") - 1,
808 .type = NFS4_ACL_WHO_GROUP,
811 .string = "EVERYONE@",
812 .stringlen = sizeof("EVERYONE@") - 1,
813 .type = NFS4_ACL_WHO_EVERYONE,
818 nfs4_acl_get_whotype(char *p, u32 len)
820 int i;
822 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
823 if (s2t_map[i].stringlen == len &&
824 0 == memcmp(s2t_map[i].string, p, len))
825 return s2t_map[i].type;
827 return NFS4_ACL_WHO_NAMED;
831 nfs4_acl_write_who(int who, char *p)
833 int i;
835 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
836 if (s2t_map[i].type == who) {
837 memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
838 return s2t_map[i].stringlen;
841 BUG();
842 return -1;
845 EXPORT_SYMBOL(nfs4_acl_new);
846 EXPORT_SYMBOL(nfs4_acl_get_whotype);
847 EXPORT_SYMBOL(nfs4_acl_write_who);