[PATCH] knfsd: nfsd4: acls: relax the nfsv4->posix mapping
[linux-2.6/sactl.git] / fs / nfsd / nfs4acl.c
blobf2f66b3da7ac1d3b9ff3ac505d84c407e0cba1c9
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 | NFS4_ACE_INHERIT_ONLY_ACE)
66 #define MASK_EQUAL(mask1, mask2) \
67 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
69 static u32
70 mask_from_posix(unsigned short perm, unsigned int flags)
72 int mask = NFS4_ANYONE_MODE;
74 if (flags & NFS4_ACL_OWNER)
75 mask |= NFS4_OWNER_MODE;
76 if (perm & ACL_READ)
77 mask |= NFS4_READ_MODE;
78 if (perm & ACL_WRITE)
79 mask |= NFS4_WRITE_MODE;
80 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
81 mask |= NFS4_ACE_DELETE_CHILD;
82 if (perm & ACL_EXECUTE)
83 mask |= NFS4_EXECUTE_MODE;
84 return mask;
87 static u32
88 deny_mask(u32 allow_mask, unsigned int flags)
90 u32 ret = ~allow_mask & ~NFS4_MASK_UNSUPP;
91 if (!(flags & NFS4_ACL_DIR))
92 ret &= ~NFS4_ACE_DELETE_CHILD;
93 return ret;
96 /* XXX: modify functions to return NFS errors; they're only ever
97 * used by nfs code, after all.... */
99 /* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
100 * side of being more restrictive, so the mode bit mapping below is
101 * pessimistic. An optimistic version would be needed to handle DENY's,
102 * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
103 * bits. */
105 static void
106 low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
108 u32 write_mode = NFS4_WRITE_MODE;
110 if (flags & NFS4_ACL_DIR)
111 write_mode |= NFS4_ACE_DELETE_CHILD;
112 *mode = 0;
113 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
114 *mode |= ACL_READ;
115 if ((perm & write_mode) == write_mode)
116 *mode |= ACL_WRITE;
117 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
118 *mode |= ACL_EXECUTE;
121 struct ace_container {
122 struct nfs4_ace *ace;
123 struct list_head ace_l;
126 static short ace2type(struct nfs4_ace *);
127 static int _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *, unsigned int);
128 static struct posix_acl *_nfsv4_to_posix_one(struct nfs4_acl *, unsigned int);
129 int nfs4_acl_add_ace(struct nfs4_acl *, u32, u32, u32, int, uid_t);
130 static int nfs4_acl_split(struct nfs4_acl *, struct nfs4_acl *);
132 struct nfs4_acl *
133 nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
134 unsigned int flags)
136 struct nfs4_acl *acl;
137 int error = -EINVAL;
139 if ((pacl != NULL &&
140 (posix_acl_valid(pacl) < 0 || pacl->a_count == 0)) ||
141 (dpacl != NULL &&
142 (posix_acl_valid(dpacl) < 0 || dpacl->a_count == 0)))
143 goto out_err;
145 acl = nfs4_acl_new();
146 if (acl == NULL) {
147 error = -ENOMEM;
148 goto out_err;
151 if (pacl != NULL) {
152 error = _posix_to_nfsv4_one(pacl, acl,
153 flags & ~NFS4_ACL_TYPE_DEFAULT);
154 if (error < 0)
155 goto out_acl;
158 if (dpacl != NULL) {
159 error = _posix_to_nfsv4_one(dpacl, acl,
160 flags | NFS4_ACL_TYPE_DEFAULT);
161 if (error < 0)
162 goto out_acl;
165 return acl;
167 out_acl:
168 nfs4_acl_free(acl);
169 out_err:
170 acl = ERR_PTR(error);
172 return acl;
175 static int
176 nfs4_acl_add_pair(struct nfs4_acl *acl, int eflag, u32 mask, int whotype,
177 uid_t owner, unsigned int flags)
179 int error;
181 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
182 eflag, mask, whotype, owner);
183 if (error < 0)
184 return error;
185 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
186 eflag, deny_mask(mask, flags), whotype, owner);
187 return error;
190 /* We assume the acl has been verified with posix_acl_valid. */
191 static int
192 _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
193 unsigned int flags)
195 struct posix_acl_entry *pa, *pe, *group_owner_entry;
196 int error = -EINVAL;
197 u32 mask, mask_mask;
198 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
199 NFS4_INHERITANCE_FLAGS : 0);
201 BUG_ON(pacl->a_count < 3);
202 pe = pacl->a_entries + pacl->a_count;
203 pa = pe - 2; /* if mask entry exists, it's second from the last. */
204 if (pa->e_tag == ACL_MASK)
205 mask_mask = deny_mask(mask_from_posix(pa->e_perm, flags), flags);
206 else
207 mask_mask = 0;
209 pa = pacl->a_entries;
210 BUG_ON(pa->e_tag != ACL_USER_OBJ);
211 mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
212 error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_OWNER, 0, flags);
213 if (error < 0)
214 goto out;
215 pa++;
217 while (pa->e_tag == ACL_USER) {
218 mask = mask_from_posix(pa->e_perm, flags);
219 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
220 eflag, mask_mask, NFS4_ACL_WHO_NAMED, pa->e_id);
221 if (error < 0)
222 goto out;
225 error = nfs4_acl_add_pair(acl, eflag, mask,
226 NFS4_ACL_WHO_NAMED, pa->e_id, flags);
227 if (error < 0)
228 goto out;
229 pa++;
232 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
233 * since a user can be in more than one group. */
235 /* allow ACEs */
237 if (pacl->a_count > 3) {
238 BUG_ON(pa->e_tag != ACL_GROUP_OBJ);
239 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
240 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,
241 NFS4_ACL_WHO_GROUP, 0);
242 if (error < 0)
243 goto out;
245 group_owner_entry = pa;
246 mask = mask_from_posix(pa->e_perm, flags);
247 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
248 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,
249 NFS4_ACL_WHO_GROUP, 0);
250 if (error < 0)
251 goto out;
252 pa++;
254 while (pa->e_tag == ACL_GROUP) {
255 mask = mask_from_posix(pa->e_perm, flags);
256 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
257 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,
258 NFS4_ACL_WHO_NAMED, pa->e_id);
259 if (error < 0)
260 goto out;
262 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
263 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,
264 NFS4_ACL_WHO_NAMED, pa->e_id);
265 if (error < 0)
266 goto out;
267 pa++;
270 /* deny ACEs */
272 pa = group_owner_entry;
273 mask = mask_from_posix(pa->e_perm, flags);
274 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
275 NFS4_ACE_IDENTIFIER_GROUP | eflag,
276 deny_mask(mask, flags), NFS4_ACL_WHO_GROUP, 0);
277 if (error < 0)
278 goto out;
279 pa++;
280 while (pa->e_tag == ACL_GROUP) {
281 mask = mask_from_posix(pa->e_perm, flags);
282 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
283 NFS4_ACE_IDENTIFIER_GROUP | eflag,
284 deny_mask(mask, flags), NFS4_ACL_WHO_NAMED, pa->e_id);
285 if (error < 0)
286 goto out;
287 pa++;
290 if (pa->e_tag == ACL_MASK)
291 pa++;
292 BUG_ON(pa->e_tag != ACL_OTHER);
293 mask = mask_from_posix(pa->e_perm, flags);
294 error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_EVERYONE, 0, flags);
296 out:
297 return error;
300 static void
301 sort_pacl_range(struct posix_acl *pacl, int start, int end) {
302 int sorted = 0, i;
303 struct posix_acl_entry tmp;
305 /* We just do a bubble sort; easy to do in place, and we're not
306 * expecting acl's to be long enough to justify anything more. */
307 while (!sorted) {
308 sorted = 1;
309 for (i = start; i < end; i++) {
310 if (pacl->a_entries[i].e_id
311 > pacl->a_entries[i+1].e_id) {
312 sorted = 0;
313 tmp = pacl->a_entries[i];
314 pacl->a_entries[i] = pacl->a_entries[i+1];
315 pacl->a_entries[i+1] = tmp;
321 static void
322 sort_pacl(struct posix_acl *pacl)
324 /* posix_acl_valid requires that users and groups be in order
325 * by uid/gid. */
326 int i, j;
328 if (pacl->a_count <= 4)
329 return; /* no users or groups */
330 i = 1;
331 while (pacl->a_entries[i].e_tag == ACL_USER)
332 i++;
333 sort_pacl_range(pacl, 1, i-1);
335 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
336 j = i++;
337 while (pacl->a_entries[j].e_tag == ACL_GROUP)
338 j++;
339 sort_pacl_range(pacl, i, j-1);
340 return;
344 nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
345 struct posix_acl **dpacl, unsigned int flags)
347 struct nfs4_acl *dacl;
348 int error = -ENOMEM;
350 *pacl = NULL;
351 *dpacl = NULL;
353 dacl = nfs4_acl_new();
354 if (dacl == NULL)
355 goto out;
357 error = nfs4_acl_split(acl, dacl);
358 if (error < 0)
359 goto out_acl;
361 if (pacl != NULL) {
362 if (acl->naces == 0) {
363 error = -ENODATA;
364 goto try_dpacl;
367 *pacl = _nfsv4_to_posix_one(acl, flags);
368 if (IS_ERR(*pacl)) {
369 error = PTR_ERR(*pacl);
370 *pacl = NULL;
371 goto out_acl;
375 try_dpacl:
376 if (dpacl != NULL) {
377 if (dacl->naces == 0) {
378 if (pacl == NULL || *pacl == NULL)
379 error = -ENODATA;
380 goto out_acl;
383 error = 0;
384 *dpacl = _nfsv4_to_posix_one(dacl, flags);
385 if (IS_ERR(*dpacl)) {
386 error = PTR_ERR(*dpacl);
387 *dpacl = NULL;
388 goto out_acl;
392 out_acl:
393 if (error && pacl) {
394 posix_acl_release(*pacl);
395 *pacl = NULL;
397 nfs4_acl_free(dacl);
398 out:
399 return error;
403 * While processing the NFSv4 ACE, this maintains bitmasks representing
404 * which permission bits have been allowed and which denied to a given
405 * entity: */
406 struct posix_ace_state {
407 u32 allow;
408 u32 deny;
411 struct posix_user_ace_state {
412 uid_t uid;
413 struct posix_ace_state perms;
416 struct posix_ace_state_array {
417 int n;
418 struct posix_user_ace_state aces[];
422 * While processing the NFSv4 ACE, this maintains the partial permissions
423 * calculated so far: */
425 struct posix_acl_state {
426 struct posix_ace_state owner;
427 struct posix_ace_state group;
428 struct posix_ace_state other;
429 struct posix_ace_state everyone;
430 struct posix_ace_state mask; /* Deny unused in this case */
431 struct posix_ace_state_array *users;
432 struct posix_ace_state_array *groups;
435 static int
436 init_state(struct posix_acl_state *state, int cnt)
438 int alloc;
440 memset(state, 0, sizeof(struct posix_acl_state));
442 * In the worst case, each individual acl could be for a distinct
443 * named user or group, but we don't no which, so we allocate
444 * enough space for either:
446 alloc = sizeof(struct posix_ace_state_array)
447 + cnt*sizeof(struct posix_ace_state);
448 state->users = kzalloc(alloc, GFP_KERNEL);
449 if (!state->users)
450 return -ENOMEM;
451 state->groups = kzalloc(alloc, GFP_KERNEL);
452 if (!state->groups) {
453 kfree(state->users);
454 return -ENOMEM;
456 return 0;
459 static void
460 free_state(struct posix_acl_state *state) {
461 kfree(state->users);
462 kfree(state->groups);
465 static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
467 state->mask.allow |= astate->allow;
471 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
472 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
473 * to traditional read/write/execute permissions.
475 * It's problematic to reject acls that use certain mode bits, because it
476 * places the burden on users to learn the rules about which bits one
477 * particular server sets, without giving the user a lot of help--we return an
478 * error that could mean any number of different things. To make matters
479 * worse, the problematic bits might be introduced by some application that's
480 * automatically mapping from some other acl model.
482 * So wherever possible we accept anything, possibly erring on the side of
483 * denying more permissions than necessary.
485 * However we do reject *explicit* DENY's of a few bits representing
486 * permissions we could never deny:
489 static inline int check_deny(u32 mask, int isowner)
491 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
492 return -EINVAL;
493 if (!isowner)
494 return 0;
495 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
496 return -EINVAL;
497 return 0;
500 static struct posix_acl *
501 posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
503 struct posix_acl_entry *pace;
504 struct posix_acl *pacl;
505 int nace;
506 int i, error = 0;
508 nace = 4 + state->users->n + state->groups->n;
509 pacl = posix_acl_alloc(nace, GFP_KERNEL);
510 if (!pacl)
511 return ERR_PTR(-ENOMEM);
513 pace = pacl->a_entries;
514 pace->e_tag = ACL_USER_OBJ;
515 error = check_deny(state->owner.deny, 1);
516 if (error)
517 goto out_err;
518 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
519 pace->e_id = ACL_UNDEFINED_ID;
521 for (i=0; i < state->users->n; i++) {
522 pace++;
523 pace->e_tag = ACL_USER;
524 error = check_deny(state->users->aces[i].perms.deny, 0);
525 if (error)
526 goto out_err;
527 low_mode_from_nfs4(state->users->aces[i].perms.allow,
528 &pace->e_perm, flags);
529 pace->e_id = state->users->aces[i].uid;
530 add_to_mask(state, &state->users->aces[i].perms);
533 pace++;
534 pace->e_tag = ACL_GROUP_OBJ;
535 error = check_deny(state->group.deny, 0);
536 if (error)
537 goto out_err;
538 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
539 pace->e_id = ACL_UNDEFINED_ID;
540 add_to_mask(state, &state->group);
542 for (i=0; i < state->groups->n; i++) {
543 pace++;
544 pace->e_tag = ACL_GROUP;
545 error = check_deny(state->groups->aces[i].perms.deny, 0);
546 if (error)
547 goto out_err;
548 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
549 &pace->e_perm, flags);
550 pace->e_id = state->groups->aces[i].uid;
551 add_to_mask(state, &state->groups->aces[i].perms);
554 pace++;
555 pace->e_tag = ACL_MASK;
556 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
557 pace->e_id = ACL_UNDEFINED_ID;
559 pace++;
560 pace->e_tag = ACL_OTHER;
561 error = check_deny(state->other.deny, 0);
562 if (error)
563 goto out_err;
564 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
565 pace->e_id = ACL_UNDEFINED_ID;
567 return pacl;
568 out_err:
569 posix_acl_release(pacl);
570 return ERR_PTR(error);
573 static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
575 /* Allow all bits in the mask not already denied: */
576 astate->allow |= mask & ~astate->deny;
579 static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
581 /* Deny all bits in the mask not already allowed: */
582 astate->deny |= mask & ~astate->allow;
585 static int find_uid(struct posix_acl_state *state, struct posix_ace_state_array *a, uid_t uid)
587 int i;
589 for (i = 0; i < a->n; i++)
590 if (a->aces[i].uid == uid)
591 return i;
592 /* Not found: */
593 a->n++;
594 a->aces[i].uid = uid;
595 a->aces[i].perms.allow = state->everyone.allow;
596 a->aces[i].perms.deny = state->everyone.deny;
598 return i;
601 static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
603 int i;
605 for (i=0; i < a->n; i++)
606 deny_bits(&a->aces[i].perms, mask);
609 static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
611 int i;
613 for (i=0; i < a->n; i++)
614 allow_bits(&a->aces[i].perms, mask);
617 static void process_one_v4_ace(struct posix_acl_state *state,
618 struct nfs4_ace *ace)
620 u32 mask = ace->access_mask;
621 int i;
623 switch (ace2type(ace)) {
624 case ACL_USER_OBJ:
625 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
626 allow_bits(&state->owner, mask);
627 } else {
628 deny_bits(&state->owner, mask);
630 break;
631 case ACL_USER:
632 i = find_uid(state, state->users, ace->who);
633 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
634 allow_bits(&state->users->aces[i].perms, mask);
635 } else {
636 deny_bits(&state->users->aces[i].perms, mask);
637 mask = state->users->aces[i].perms.deny;
638 deny_bits(&state->owner, mask);
640 break;
641 case ACL_GROUP_OBJ:
642 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
643 allow_bits(&state->group, mask);
644 } else {
645 deny_bits(&state->group, mask);
646 mask = state->group.deny;
647 deny_bits(&state->owner, mask);
648 deny_bits(&state->everyone, mask);
649 deny_bits_array(state->users, mask);
650 deny_bits_array(state->groups, mask);
652 break;
653 case ACL_GROUP:
654 i = find_uid(state, state->groups, ace->who);
655 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
656 allow_bits(&state->groups->aces[i].perms, mask);
657 } else {
658 deny_bits(&state->groups->aces[i].perms, mask);
659 mask = state->groups->aces[i].perms.deny;
660 deny_bits(&state->owner, mask);
661 deny_bits(&state->group, mask);
662 deny_bits(&state->everyone, mask);
663 deny_bits_array(state->users, mask);
664 deny_bits_array(state->groups, mask);
666 break;
667 case ACL_OTHER:
668 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
669 allow_bits(&state->owner, mask);
670 allow_bits(&state->group, mask);
671 allow_bits(&state->other, mask);
672 allow_bits(&state->everyone, mask);
673 allow_bits_array(state->users, mask);
674 allow_bits_array(state->groups, mask);
675 } else {
676 deny_bits(&state->owner, mask);
677 deny_bits(&state->group, mask);
678 deny_bits(&state->other, mask);
679 deny_bits(&state->everyone, mask);
680 deny_bits_array(state->users, mask);
681 deny_bits_array(state->groups, mask);
686 static struct posix_acl *
687 _nfsv4_to_posix_one(struct nfs4_acl *n4acl, unsigned int flags)
689 struct posix_acl_state state;
690 struct posix_acl *pacl;
691 struct nfs4_ace *ace;
692 int ret;
694 ret = init_state(&state, n4acl->naces);
695 if (ret)
696 return ERR_PTR(ret);
698 list_for_each_entry(ace, &n4acl->ace_head, l_ace)
699 process_one_v4_ace(&state, ace);
701 pacl = posix_state_to_acl(&state, flags);
703 free_state(&state);
705 if (!IS_ERR(pacl))
706 sort_pacl(pacl);
707 return pacl;
710 static int
711 nfs4_acl_split(struct nfs4_acl *acl, struct nfs4_acl *dacl)
713 struct list_head *h, *n;
714 struct nfs4_ace *ace;
715 int error = 0;
717 list_for_each_safe(h, n, &acl->ace_head) {
718 ace = list_entry(h, struct nfs4_ace, l_ace);
720 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
721 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
722 return -EINVAL;
724 if ((ace->flag & NFS4_INHERITANCE_FLAGS)
725 != NFS4_INHERITANCE_FLAGS)
726 continue;
728 error = nfs4_acl_add_ace(dacl, ace->type, ace->flag,
729 ace->access_mask, ace->whotype, ace->who);
730 if (error < 0)
731 goto out;
733 list_del(h);
734 kfree(ace);
735 acl->naces--;
738 out:
739 return error;
742 static short
743 ace2type(struct nfs4_ace *ace)
745 switch (ace->whotype) {
746 case NFS4_ACL_WHO_NAMED:
747 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
748 ACL_GROUP : ACL_USER);
749 case NFS4_ACL_WHO_OWNER:
750 return ACL_USER_OBJ;
751 case NFS4_ACL_WHO_GROUP:
752 return ACL_GROUP_OBJ;
753 case NFS4_ACL_WHO_EVERYONE:
754 return ACL_OTHER;
756 BUG();
757 return -1;
760 EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
761 EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
763 struct nfs4_acl *
764 nfs4_acl_new(void)
766 struct nfs4_acl *acl;
768 if ((acl = kmalloc(sizeof(*acl), GFP_KERNEL)) == NULL)
769 return NULL;
771 acl->naces = 0;
772 INIT_LIST_HEAD(&acl->ace_head);
774 return acl;
777 void
778 nfs4_acl_free(struct nfs4_acl *acl)
780 struct list_head *h;
781 struct nfs4_ace *ace;
783 if (!acl)
784 return;
786 while (!list_empty(&acl->ace_head)) {
787 h = acl->ace_head.next;
788 list_del(h);
789 ace = list_entry(h, struct nfs4_ace, l_ace);
790 kfree(ace);
793 kfree(acl);
795 return;
799 nfs4_acl_add_ace(struct nfs4_acl *acl, u32 type, u32 flag, u32 access_mask,
800 int whotype, uid_t who)
802 struct nfs4_ace *ace;
804 if ((ace = kmalloc(sizeof(*ace), GFP_KERNEL)) == NULL)
805 return -ENOMEM;
807 ace->type = type;
808 ace->flag = flag;
809 ace->access_mask = access_mask;
810 ace->whotype = whotype;
811 ace->who = who;
813 list_add_tail(&ace->l_ace, &acl->ace_head);
814 acl->naces++;
816 return 0;
819 static struct {
820 char *string;
821 int stringlen;
822 int type;
823 } s2t_map[] = {
825 .string = "OWNER@",
826 .stringlen = sizeof("OWNER@") - 1,
827 .type = NFS4_ACL_WHO_OWNER,
830 .string = "GROUP@",
831 .stringlen = sizeof("GROUP@") - 1,
832 .type = NFS4_ACL_WHO_GROUP,
835 .string = "EVERYONE@",
836 .stringlen = sizeof("EVERYONE@") - 1,
837 .type = NFS4_ACL_WHO_EVERYONE,
842 nfs4_acl_get_whotype(char *p, u32 len)
844 int i;
846 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
847 if (s2t_map[i].stringlen == len &&
848 0 == memcmp(s2t_map[i].string, p, len))
849 return s2t_map[i].type;
851 return NFS4_ACL_WHO_NAMED;
855 nfs4_acl_write_who(int who, char *p)
857 int i;
859 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
860 if (s2t_map[i].type == who) {
861 memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
862 return s2t_map[i].stringlen;
865 BUG();
866 return -1;
869 EXPORT_SYMBOL(nfs4_acl_new);
870 EXPORT_SYMBOL(nfs4_acl_free);
871 EXPORT_SYMBOL(nfs4_acl_add_ace);
872 EXPORT_SYMBOL(nfs4_acl_get_whotype);
873 EXPORT_SYMBOL(nfs4_acl_write_who);