Merge branch 'core-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / fs / nfsd / nfs4acl.c
blobd190e33d0ec2fdeb845eec70ab3c610ab551758d
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 "nfsfh.h"
41 #include "nfsd.h"
42 #include "acl.h"
43 #include "vfs.h"
45 #define NFS4_ACL_TYPE_DEFAULT 0x01
46 #define NFS4_ACL_DIR 0x02
47 #define NFS4_ACL_OWNER 0x04
49 /* mode bit translations: */
50 #define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
51 #define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
52 #define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
53 #define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
54 #define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
56 /* We don't support these bits; insist they be neither allowed nor denied */
57 #define NFS4_MASK_UNSUPP (NFS4_ACE_DELETE | NFS4_ACE_WRITE_OWNER \
58 | NFS4_ACE_READ_NAMED_ATTRS | NFS4_ACE_WRITE_NAMED_ATTRS)
60 /* flags used to simulate posix default ACLs */
61 #define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
62 | NFS4_ACE_DIRECTORY_INHERIT_ACE)
64 #define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
65 | NFS4_ACE_INHERIT_ONLY_ACE \
66 | NFS4_ACE_IDENTIFIER_GROUP)
68 #define MASK_EQUAL(mask1, mask2) \
69 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
71 static u32
72 mask_from_posix(unsigned short perm, unsigned int flags)
74 int mask = NFS4_ANYONE_MODE;
76 if (flags & NFS4_ACL_OWNER)
77 mask |= NFS4_OWNER_MODE;
78 if (perm & ACL_READ)
79 mask |= NFS4_READ_MODE;
80 if (perm & ACL_WRITE)
81 mask |= NFS4_WRITE_MODE;
82 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
83 mask |= NFS4_ACE_DELETE_CHILD;
84 if (perm & ACL_EXECUTE)
85 mask |= NFS4_EXECUTE_MODE;
86 return mask;
89 static u32
90 deny_mask_from_posix(unsigned short perm, u32 flags)
92 u32 mask = 0;
94 if (perm & ACL_READ)
95 mask |= NFS4_READ_MODE;
96 if (perm & ACL_WRITE)
97 mask |= NFS4_WRITE_MODE;
98 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
99 mask |= NFS4_ACE_DELETE_CHILD;
100 if (perm & ACL_EXECUTE)
101 mask |= NFS4_EXECUTE_MODE;
102 return mask;
105 /* XXX: modify functions to return NFS errors; they're only ever
106 * used by nfs code, after all.... */
108 /* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
109 * side of being more restrictive, so the mode bit mapping below is
110 * pessimistic. An optimistic version would be needed to handle DENY's,
111 * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
112 * bits. */
114 static void
115 low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
117 u32 write_mode = NFS4_WRITE_MODE;
119 if (flags & NFS4_ACL_DIR)
120 write_mode |= NFS4_ACE_DELETE_CHILD;
121 *mode = 0;
122 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
123 *mode |= ACL_READ;
124 if ((perm & write_mode) == write_mode)
125 *mode |= ACL_WRITE;
126 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
127 *mode |= ACL_EXECUTE;
130 struct ace_container {
131 struct nfs4_ace *ace;
132 struct list_head ace_l;
135 static short ace2type(struct nfs4_ace *);
136 static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
137 unsigned int);
140 nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry,
141 struct nfs4_acl **acl)
143 struct inode *inode = dentry->d_inode;
144 int error = 0;
145 struct posix_acl *pacl = NULL, *dpacl = NULL;
146 unsigned int flags = 0;
147 int size = 0;
149 pacl = get_acl(inode, ACL_TYPE_ACCESS);
150 if (!pacl) {
151 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
152 if (IS_ERR(pacl))
153 return PTR_ERR(pacl);
155 /* allocate for worst case: one (deny, allow) pair each: */
156 size += 2 * pacl->a_count;
158 if (S_ISDIR(inode->i_mode)) {
159 flags = NFS4_ACL_DIR;
160 dpacl = get_acl(inode, ACL_TYPE_DEFAULT);
161 if (dpacl)
162 size += 2 * dpacl->a_count;
165 *acl = nfs4_acl_new(size);
166 if (*acl == NULL) {
167 error = -ENOMEM;
168 goto out;
171 _posix_to_nfsv4_one(pacl, *acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
173 if (dpacl)
174 _posix_to_nfsv4_one(dpacl, *acl, flags | NFS4_ACL_TYPE_DEFAULT);
176 out:
177 posix_acl_release(pacl);
178 posix_acl_release(dpacl);
179 return error;
182 struct posix_acl_summary {
183 unsigned short owner;
184 unsigned short users;
185 unsigned short group;
186 unsigned short groups;
187 unsigned short other;
188 unsigned short mask;
191 static void
192 summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
194 struct posix_acl_entry *pa, *pe;
197 * Only pas.users and pas.groups need initialization; previous
198 * posix_acl_valid() calls ensure that the other fields will be
199 * initialized in the following loop. But, just to placate gcc:
201 memset(pas, 0, sizeof(*pas));
202 pas->mask = 07;
204 pe = acl->a_entries + acl->a_count;
206 FOREACH_ACL_ENTRY(pa, acl, pe) {
207 switch (pa->e_tag) {
208 case ACL_USER_OBJ:
209 pas->owner = pa->e_perm;
210 break;
211 case ACL_GROUP_OBJ:
212 pas->group = pa->e_perm;
213 break;
214 case ACL_USER:
215 pas->users |= pa->e_perm;
216 break;
217 case ACL_GROUP:
218 pas->groups |= pa->e_perm;
219 break;
220 case ACL_OTHER:
221 pas->other = pa->e_perm;
222 break;
223 case ACL_MASK:
224 pas->mask = pa->e_perm;
225 break;
228 /* We'll only care about effective permissions: */
229 pas->users &= pas->mask;
230 pas->group &= pas->mask;
231 pas->groups &= pas->mask;
234 /* We assume the acl has been verified with posix_acl_valid. */
235 static void
236 _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
237 unsigned int flags)
239 struct posix_acl_entry *pa, *group_owner_entry;
240 struct nfs4_ace *ace;
241 struct posix_acl_summary pas;
242 unsigned short deny;
243 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
244 NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
246 BUG_ON(pacl->a_count < 3);
247 summarize_posix_acl(pacl, &pas);
249 pa = pacl->a_entries;
250 ace = acl->aces + acl->naces;
252 /* We could deny everything not granted by the owner: */
253 deny = ~pas.owner;
255 * but it is equivalent (and simpler) to deny only what is not
256 * granted by later entries:
258 deny &= pas.users | pas.group | pas.groups | pas.other;
259 if (deny) {
260 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
261 ace->flag = eflag;
262 ace->access_mask = deny_mask_from_posix(deny, flags);
263 ace->whotype = NFS4_ACL_WHO_OWNER;
264 ace++;
265 acl->naces++;
268 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
269 ace->flag = eflag;
270 ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
271 ace->whotype = NFS4_ACL_WHO_OWNER;
272 ace++;
273 acl->naces++;
274 pa++;
276 while (pa->e_tag == ACL_USER) {
277 deny = ~(pa->e_perm & pas.mask);
278 deny &= pas.groups | pas.group | pas.other;
279 if (deny) {
280 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
281 ace->flag = eflag;
282 ace->access_mask = deny_mask_from_posix(deny, flags);
283 ace->whotype = NFS4_ACL_WHO_NAMED;
284 ace->who_uid = pa->e_uid;
285 ace++;
286 acl->naces++;
288 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
289 ace->flag = eflag;
290 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
291 flags);
292 ace->whotype = NFS4_ACL_WHO_NAMED;
293 ace->who_uid = pa->e_uid;
294 ace++;
295 acl->naces++;
296 pa++;
299 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
300 * since a user can be in more than one group. */
302 /* allow ACEs */
304 group_owner_entry = pa;
306 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
307 ace->flag = eflag;
308 ace->access_mask = mask_from_posix(pas.group, flags);
309 ace->whotype = NFS4_ACL_WHO_GROUP;
310 ace++;
311 acl->naces++;
312 pa++;
314 while (pa->e_tag == ACL_GROUP) {
315 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
316 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
317 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
318 flags);
319 ace->whotype = NFS4_ACL_WHO_NAMED;
320 ace->who_gid = pa->e_gid;
321 ace++;
322 acl->naces++;
323 pa++;
326 /* deny ACEs */
328 pa = group_owner_entry;
330 deny = ~pas.group & pas.other;
331 if (deny) {
332 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
333 ace->flag = eflag;
334 ace->access_mask = deny_mask_from_posix(deny, flags);
335 ace->whotype = NFS4_ACL_WHO_GROUP;
336 ace++;
337 acl->naces++;
339 pa++;
341 while (pa->e_tag == ACL_GROUP) {
342 deny = ~(pa->e_perm & pas.mask);
343 deny &= pas.other;
344 if (deny) {
345 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
346 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
347 ace->access_mask = deny_mask_from_posix(deny, flags);
348 ace->whotype = NFS4_ACL_WHO_NAMED;
349 ace->who_gid = pa->e_gid;
350 ace++;
351 acl->naces++;
353 pa++;
356 if (pa->e_tag == ACL_MASK)
357 pa++;
358 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
359 ace->flag = eflag;
360 ace->access_mask = mask_from_posix(pa->e_perm, flags);
361 ace->whotype = NFS4_ACL_WHO_EVERYONE;
362 acl->naces++;
365 static bool
366 pace_gt(struct posix_acl_entry *pace1, struct posix_acl_entry *pace2)
368 if (pace1->e_tag != pace2->e_tag)
369 return pace1->e_tag > pace2->e_tag;
370 if (pace1->e_tag == ACL_USER)
371 return uid_gt(pace1->e_uid, pace2->e_uid);
372 if (pace1->e_tag == ACL_GROUP)
373 return gid_gt(pace1->e_gid, pace2->e_gid);
374 return false;
377 static void
378 sort_pacl_range(struct posix_acl *pacl, int start, int end) {
379 int sorted = 0, i;
380 struct posix_acl_entry tmp;
382 /* We just do a bubble sort; easy to do in place, and we're not
383 * expecting acl's to be long enough to justify anything more. */
384 while (!sorted) {
385 sorted = 1;
386 for (i = start; i < end; i++) {
387 if (pace_gt(&pacl->a_entries[i],
388 &pacl->a_entries[i+1])) {
389 sorted = 0;
390 tmp = pacl->a_entries[i];
391 pacl->a_entries[i] = pacl->a_entries[i+1];
392 pacl->a_entries[i+1] = tmp;
398 static void
399 sort_pacl(struct posix_acl *pacl)
401 /* posix_acl_valid requires that users and groups be in order
402 * by uid/gid. */
403 int i, j;
405 if (pacl->a_count <= 4)
406 return; /* no users or groups */
407 i = 1;
408 while (pacl->a_entries[i].e_tag == ACL_USER)
409 i++;
410 sort_pacl_range(pacl, 1, i-1);
412 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
413 j = ++i;
414 while (pacl->a_entries[j].e_tag == ACL_GROUP)
415 j++;
416 sort_pacl_range(pacl, i, j-1);
417 return;
421 * While processing the NFSv4 ACE, this maintains bitmasks representing
422 * which permission bits have been allowed and which denied to a given
423 * entity: */
424 struct posix_ace_state {
425 u32 allow;
426 u32 deny;
429 struct posix_user_ace_state {
430 union {
431 kuid_t uid;
432 kgid_t gid;
434 struct posix_ace_state perms;
437 struct posix_ace_state_array {
438 int n;
439 struct posix_user_ace_state aces[];
443 * While processing the NFSv4 ACE, this maintains the partial permissions
444 * calculated so far: */
446 struct posix_acl_state {
447 int empty;
448 struct posix_ace_state owner;
449 struct posix_ace_state group;
450 struct posix_ace_state other;
451 struct posix_ace_state everyone;
452 struct posix_ace_state mask; /* Deny unused in this case */
453 struct posix_ace_state_array *users;
454 struct posix_ace_state_array *groups;
457 static int
458 init_state(struct posix_acl_state *state, int cnt)
460 int alloc;
462 memset(state, 0, sizeof(struct posix_acl_state));
463 state->empty = 1;
465 * In the worst case, each individual acl could be for a distinct
466 * named user or group, but we don't no which, so we allocate
467 * enough space for either:
469 alloc = sizeof(struct posix_ace_state_array)
470 + cnt*sizeof(struct posix_user_ace_state);
471 state->users = kzalloc(alloc, GFP_KERNEL);
472 if (!state->users)
473 return -ENOMEM;
474 state->groups = kzalloc(alloc, GFP_KERNEL);
475 if (!state->groups) {
476 kfree(state->users);
477 return -ENOMEM;
479 return 0;
482 static void
483 free_state(struct posix_acl_state *state) {
484 kfree(state->users);
485 kfree(state->groups);
488 static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
490 state->mask.allow |= astate->allow;
494 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
495 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
496 * to traditional read/write/execute permissions.
498 * It's problematic to reject acls that use certain mode bits, because it
499 * places the burden on users to learn the rules about which bits one
500 * particular server sets, without giving the user a lot of help--we return an
501 * error that could mean any number of different things. To make matters
502 * worse, the problematic bits might be introduced by some application that's
503 * automatically mapping from some other acl model.
505 * So wherever possible we accept anything, possibly erring on the side of
506 * denying more permissions than necessary.
508 * However we do reject *explicit* DENY's of a few bits representing
509 * permissions we could never deny:
512 static inline int check_deny(u32 mask, int isowner)
514 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
515 return -EINVAL;
516 if (!isowner)
517 return 0;
518 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
519 return -EINVAL;
520 return 0;
523 static struct posix_acl *
524 posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
526 struct posix_acl_entry *pace;
527 struct posix_acl *pacl;
528 int nace;
529 int i, error = 0;
532 * ACLs with no ACEs are treated differently in the inheritable
533 * and effective cases: when there are no inheritable ACEs, we
534 * set a zero-length default posix acl:
536 if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT)) {
537 pacl = posix_acl_alloc(0, GFP_KERNEL);
538 return pacl ? pacl : ERR_PTR(-ENOMEM);
541 * When there are no effective ACEs, the following will end
542 * up setting a 3-element effective posix ACL with all
543 * permissions zero.
545 nace = 4 + state->users->n + state->groups->n;
546 pacl = posix_acl_alloc(nace, GFP_KERNEL);
547 if (!pacl)
548 return ERR_PTR(-ENOMEM);
550 pace = pacl->a_entries;
551 pace->e_tag = ACL_USER_OBJ;
552 error = check_deny(state->owner.deny, 1);
553 if (error)
554 goto out_err;
555 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
557 for (i=0; i < state->users->n; i++) {
558 pace++;
559 pace->e_tag = ACL_USER;
560 error = check_deny(state->users->aces[i].perms.deny, 0);
561 if (error)
562 goto out_err;
563 low_mode_from_nfs4(state->users->aces[i].perms.allow,
564 &pace->e_perm, flags);
565 pace->e_uid = state->users->aces[i].uid;
566 add_to_mask(state, &state->users->aces[i].perms);
569 pace++;
570 pace->e_tag = ACL_GROUP_OBJ;
571 error = check_deny(state->group.deny, 0);
572 if (error)
573 goto out_err;
574 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
575 add_to_mask(state, &state->group);
577 for (i=0; i < state->groups->n; i++) {
578 pace++;
579 pace->e_tag = ACL_GROUP;
580 error = check_deny(state->groups->aces[i].perms.deny, 0);
581 if (error)
582 goto out_err;
583 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
584 &pace->e_perm, flags);
585 pace->e_gid = state->groups->aces[i].gid;
586 add_to_mask(state, &state->groups->aces[i].perms);
589 pace++;
590 pace->e_tag = ACL_MASK;
591 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
593 pace++;
594 pace->e_tag = ACL_OTHER;
595 error = check_deny(state->other.deny, 0);
596 if (error)
597 goto out_err;
598 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
600 return pacl;
601 out_err:
602 posix_acl_release(pacl);
603 return ERR_PTR(error);
606 static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
608 /* Allow all bits in the mask not already denied: */
609 astate->allow |= mask & ~astate->deny;
612 static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
614 /* Deny all bits in the mask not already allowed: */
615 astate->deny |= mask & ~astate->allow;
618 static int find_uid(struct posix_acl_state *state, kuid_t uid)
620 struct posix_ace_state_array *a = state->users;
621 int i;
623 for (i = 0; i < a->n; i++)
624 if (uid_eq(a->aces[i].uid, uid))
625 return i;
626 /* Not found: */
627 a->n++;
628 a->aces[i].uid = uid;
629 a->aces[i].perms.allow = state->everyone.allow;
630 a->aces[i].perms.deny = state->everyone.deny;
632 return i;
635 static int find_gid(struct posix_acl_state *state, kgid_t gid)
637 struct posix_ace_state_array *a = state->groups;
638 int i;
640 for (i = 0; i < a->n; i++)
641 if (gid_eq(a->aces[i].gid, gid))
642 return i;
643 /* Not found: */
644 a->n++;
645 a->aces[i].gid = gid;
646 a->aces[i].perms.allow = state->everyone.allow;
647 a->aces[i].perms.deny = state->everyone.deny;
649 return i;
652 static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
654 int i;
656 for (i=0; i < a->n; i++)
657 deny_bits(&a->aces[i].perms, mask);
660 static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
662 int i;
664 for (i=0; i < a->n; i++)
665 allow_bits(&a->aces[i].perms, mask);
668 static void process_one_v4_ace(struct posix_acl_state *state,
669 struct nfs4_ace *ace)
671 u32 mask = ace->access_mask;
672 int i;
674 state->empty = 0;
676 switch (ace2type(ace)) {
677 case ACL_USER_OBJ:
678 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
679 allow_bits(&state->owner, mask);
680 } else {
681 deny_bits(&state->owner, mask);
683 break;
684 case ACL_USER:
685 i = find_uid(state, ace->who_uid);
686 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
687 allow_bits(&state->users->aces[i].perms, mask);
688 } else {
689 deny_bits(&state->users->aces[i].perms, mask);
690 mask = state->users->aces[i].perms.deny;
691 deny_bits(&state->owner, mask);
693 break;
694 case ACL_GROUP_OBJ:
695 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
696 allow_bits(&state->group, mask);
697 } else {
698 deny_bits(&state->group, mask);
699 mask = state->group.deny;
700 deny_bits(&state->owner, mask);
701 deny_bits(&state->everyone, mask);
702 deny_bits_array(state->users, mask);
703 deny_bits_array(state->groups, mask);
705 break;
706 case ACL_GROUP:
707 i = find_gid(state, ace->who_gid);
708 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
709 allow_bits(&state->groups->aces[i].perms, mask);
710 } else {
711 deny_bits(&state->groups->aces[i].perms, mask);
712 mask = state->groups->aces[i].perms.deny;
713 deny_bits(&state->owner, mask);
714 deny_bits(&state->group, mask);
715 deny_bits(&state->everyone, mask);
716 deny_bits_array(state->users, mask);
717 deny_bits_array(state->groups, mask);
719 break;
720 case ACL_OTHER:
721 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
722 allow_bits(&state->owner, mask);
723 allow_bits(&state->group, mask);
724 allow_bits(&state->other, mask);
725 allow_bits(&state->everyone, mask);
726 allow_bits_array(state->users, mask);
727 allow_bits_array(state->groups, mask);
728 } else {
729 deny_bits(&state->owner, mask);
730 deny_bits(&state->group, mask);
731 deny_bits(&state->other, mask);
732 deny_bits(&state->everyone, mask);
733 deny_bits_array(state->users, mask);
734 deny_bits_array(state->groups, mask);
739 static int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl,
740 struct posix_acl **pacl, struct posix_acl **dpacl,
741 unsigned int flags)
743 struct posix_acl_state effective_acl_state, default_acl_state;
744 struct nfs4_ace *ace;
745 int ret;
747 ret = init_state(&effective_acl_state, acl->naces);
748 if (ret)
749 return ret;
750 ret = init_state(&default_acl_state, acl->naces);
751 if (ret)
752 goto out_estate;
753 ret = -EINVAL;
754 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
755 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
756 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
757 goto out_dstate;
758 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
759 goto out_dstate;
760 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
761 process_one_v4_ace(&effective_acl_state, ace);
762 continue;
764 if (!(flags & NFS4_ACL_DIR))
765 goto out_dstate;
767 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
768 * is set, we're effectively turning on the other. That's OK,
769 * according to rfc 3530.
771 process_one_v4_ace(&default_acl_state, ace);
773 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
774 process_one_v4_ace(&effective_acl_state, ace);
776 *pacl = posix_state_to_acl(&effective_acl_state, flags);
777 if (IS_ERR(*pacl)) {
778 ret = PTR_ERR(*pacl);
779 *pacl = NULL;
780 goto out_dstate;
782 *dpacl = posix_state_to_acl(&default_acl_state,
783 flags | NFS4_ACL_TYPE_DEFAULT);
784 if (IS_ERR(*dpacl)) {
785 ret = PTR_ERR(*dpacl);
786 *dpacl = NULL;
787 posix_acl_release(*pacl);
788 *pacl = NULL;
789 goto out_dstate;
791 sort_pacl(*pacl);
792 sort_pacl(*dpacl);
793 ret = 0;
794 out_dstate:
795 free_state(&default_acl_state);
796 out_estate:
797 free_state(&effective_acl_state);
798 return ret;
801 __be32
802 nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
803 struct nfs4_acl *acl)
805 __be32 error;
806 int host_error;
807 struct dentry *dentry;
808 struct inode *inode;
809 struct posix_acl *pacl = NULL, *dpacl = NULL;
810 unsigned int flags = 0;
812 /* Get inode */
813 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
814 if (error)
815 return error;
817 dentry = fhp->fh_dentry;
818 inode = dentry->d_inode;
820 if (!inode->i_op->set_acl || !IS_POSIXACL(inode))
821 return nfserr_attrnotsupp;
823 if (S_ISDIR(inode->i_mode))
824 flags = NFS4_ACL_DIR;
826 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
827 if (host_error == -EINVAL)
828 return nfserr_attrnotsupp;
829 if (host_error < 0)
830 goto out_nfserr;
832 host_error = inode->i_op->set_acl(inode, pacl, ACL_TYPE_ACCESS);
833 if (host_error < 0)
834 goto out_release;
836 if (S_ISDIR(inode->i_mode)) {
837 host_error = inode->i_op->set_acl(inode, dpacl,
838 ACL_TYPE_DEFAULT);
841 out_release:
842 posix_acl_release(pacl);
843 posix_acl_release(dpacl);
844 out_nfserr:
845 if (host_error == -EOPNOTSUPP)
846 return nfserr_attrnotsupp;
847 else
848 return nfserrno(host_error);
852 static short
853 ace2type(struct nfs4_ace *ace)
855 switch (ace->whotype) {
856 case NFS4_ACL_WHO_NAMED:
857 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
858 ACL_GROUP : ACL_USER);
859 case NFS4_ACL_WHO_OWNER:
860 return ACL_USER_OBJ;
861 case NFS4_ACL_WHO_GROUP:
862 return ACL_GROUP_OBJ;
863 case NFS4_ACL_WHO_EVERYONE:
864 return ACL_OTHER;
866 BUG();
867 return -1;
870 struct nfs4_acl *
871 nfs4_acl_new(int n)
873 struct nfs4_acl *acl;
875 acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL);
876 if (acl == NULL)
877 return NULL;
878 acl->naces = 0;
879 return acl;
882 static struct {
883 char *string;
884 int stringlen;
885 int type;
886 } s2t_map[] = {
888 .string = "OWNER@",
889 .stringlen = sizeof("OWNER@") - 1,
890 .type = NFS4_ACL_WHO_OWNER,
893 .string = "GROUP@",
894 .stringlen = sizeof("GROUP@") - 1,
895 .type = NFS4_ACL_WHO_GROUP,
898 .string = "EVERYONE@",
899 .stringlen = sizeof("EVERYONE@") - 1,
900 .type = NFS4_ACL_WHO_EVERYONE,
905 nfs4_acl_get_whotype(char *p, u32 len)
907 int i;
909 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
910 if (s2t_map[i].stringlen == len &&
911 0 == memcmp(s2t_map[i].string, p, len))
912 return s2t_map[i].type;
914 return NFS4_ACL_WHO_NAMED;
917 __be32 nfs4_acl_write_who(int who, __be32 **p, int *len)
919 int i;
920 int bytes;
922 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
923 if (s2t_map[i].type != who)
924 continue;
925 bytes = 4 + (XDR_QUADLEN(s2t_map[i].stringlen) << 2);
926 if (bytes > *len)
927 return nfserr_resource;
928 *p = xdr_encode_opaque(*p, s2t_map[i].string,
929 s2t_map[i].stringlen);
930 *len -= bytes;
931 return 0;
933 WARN_ON_ONCE(1);
934 return -1;