]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfsd/nfs4acl.c
ocfs2: fix ocfs2_sync_file() if filesystem is readonly
[mirror_ubuntu-bionic-kernel.git] / fs / nfsd / nfs4acl.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Common NFSv4 ACL handling code.
3 *
4 * Copyright (c) 2002, 2003 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Marius Aamodt Eriksen <marius@umich.edu>
8 * Jeff Sedlak <jsedlak@umich.edu>
9 * J. Bruce Fields <bfields@umich.edu>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
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.
23 *
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.
35 */
36
5a0e3ad6 37#include <linux/slab.h>
1da177e4 38#include <linux/nfs_fs.h>
afeacc8c 39#include <linux/export.h>
4ac7249e 40#include "nfsfh.h"
3554116d 41#include "nfsd.h"
2ca72e17 42#include "acl.h"
4ac7249e 43#include "vfs.h"
1da177e4 44
4ac7249e
CH
45#define NFS4_ACL_TYPE_DEFAULT 0x01
46#define NFS4_ACL_DIR 0x02
47#define NFS4_ACL_OWNER 0x04
1da177e4
LT
48
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)
55
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)
59
60/* flags used to simulate posix default ACLs */
61#define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
7bdfa68c 62 | NFS4_ACE_DIRECTORY_INHERIT_ACE)
1da177e4 63
7bdfa68c
BF
64#define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
65 | NFS4_ACE_INHERIT_ONLY_ACE \
66 | NFS4_ACE_IDENTIFIER_GROUP)
b548edc2 67
1da177e4
LT
68#define MASK_EQUAL(mask1, mask2) \
69 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
70
71static u32
72mask_from_posix(unsigned short perm, unsigned int flags)
73{
74 int mask = NFS4_ANYONE_MODE;
75
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;
87}
88
89static u32
bec50c47 90deny_mask_from_posix(unsigned short perm, u32 flags)
1da177e4 91{
bec50c47
BF
92 u32 mask = 0;
93
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;
1da177e4
LT
103}
104
105/* XXX: modify functions to return NFS errors; they're only ever
106 * used by nfs code, after all.... */
107
09229edb
BF
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. */
113
114static void
115low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
1da177e4 116{
09229edb 117 u32 write_mode = NFS4_WRITE_MODE;
1da177e4 118
09229edb
BF
119 if (flags & NFS4_ACL_DIR)
120 write_mode |= NFS4_ACE_DELETE_CHILD;
1da177e4
LT
121 *mode = 0;
122 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
123 *mode |= ACL_READ;
09229edb 124 if ((perm & write_mode) == write_mode)
1da177e4
LT
125 *mode |= ACL_WRITE;
126 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
127 *mode |= ACL_EXECUTE;
1da177e4
LT
128}
129
130struct ace_container {
131 struct nfs4_ace *ace;
132 struct list_head ace_l;
133};
134
135static short ace2type(struct nfs4_ace *);
28e05dd8
BF
136static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
137 unsigned int);
1da177e4 138
4ac7249e
CH
139int
140nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry,
141 struct nfs4_acl **acl)
1da177e4 142{
4ac7249e
CH
143 struct inode *inode = dentry->d_inode;
144 int error = 0;
145 struct posix_acl *pacl = NULL, *dpacl = NULL;
146 unsigned int flags = 0;
28e05dd8 147 int size = 0;
1da177e4 148
4ac7249e
CH
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);
154 /* allocate for worst case: one (deny, allow) pair each: */
155 size += 2 * pacl->a_count;
1da177e4 156 }
4ac7249e
CH
157
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;
163 } else {
164 dpacl = NULL;
1da177e4
LT
165 }
166
4ac7249e
CH
167 *acl = nfs4_acl_new(size);
168 if (*acl == NULL) {
169 error = -ENOMEM;
170 goto out;
171 }
1da177e4 172
28e05dd8 173 if (pacl)
4ac7249e 174 _posix_to_nfsv4_one(pacl, *acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
1da177e4 175
28e05dd8 176 if (dpacl)
4ac7249e 177 _posix_to_nfsv4_one(dpacl, *acl, flags | NFS4_ACL_TYPE_DEFAULT);
1da177e4 178
4ac7249e
CH
179 out:
180 posix_acl_release(pacl);
181 posix_acl_release(dpacl);
182 return error;
1da177e4
LT
183}
184
bec50c47
BF
185struct posix_acl_summary {
186 unsigned short owner;
187 unsigned short users;
188 unsigned short group;
189 unsigned short groups;
190 unsigned short other;
191 unsigned short mask;
192};
193
28e05dd8 194static void
bec50c47 195summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
1da177e4 196{
bec50c47 197 struct posix_acl_entry *pa, *pe;
f7fede4b
BF
198
199 /*
200 * Only pas.users and pas.groups need initialization; previous
201 * posix_acl_valid() calls ensure that the other fields will be
202 * initialized in the following loop. But, just to placate gcc:
203 */
204 memset(pas, 0, sizeof(*pas));
bec50c47
BF
205 pas->mask = 07;
206
207 pe = acl->a_entries + acl->a_count;
208
209 FOREACH_ACL_ENTRY(pa, acl, pe) {
210 switch (pa->e_tag) {
211 case ACL_USER_OBJ:
212 pas->owner = pa->e_perm;
213 break;
214 case ACL_GROUP_OBJ:
215 pas->group = pa->e_perm;
216 break;
217 case ACL_USER:
218 pas->users |= pa->e_perm;
219 break;
220 case ACL_GROUP:
221 pas->groups |= pa->e_perm;
222 break;
223 case ACL_OTHER:
224 pas->other = pa->e_perm;
225 break;
226 case ACL_MASK:
227 pas->mask = pa->e_perm;
228 break;
229 }
230 }
231 /* We'll only care about effective permissions: */
232 pas->users &= pas->mask;
233 pas->group &= pas->mask;
234 pas->groups &= pas->mask;
1da177e4
LT
235}
236
237/* We assume the acl has been verified with posix_acl_valid. */
28e05dd8 238static void
1da177e4
LT
239_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
240 unsigned int flags)
241{
bec50c47
BF
242 struct posix_acl_entry *pa, *group_owner_entry;
243 struct nfs4_ace *ace;
244 struct posix_acl_summary pas;
245 unsigned short deny;
1da177e4 246 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
54c04409 247 NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
1da177e4
LT
248
249 BUG_ON(pacl->a_count < 3);
bec50c47 250 summarize_posix_acl(pacl, &pas);
1da177e4
LT
251
252 pa = pacl->a_entries;
bec50c47
BF
253 ace = acl->aces + acl->naces;
254
255 /* We could deny everything not granted by the owner: */
256 deny = ~pas.owner;
257 /*
258 * but it is equivalent (and simpler) to deny only what is not
259 * granted by later entries:
260 */
261 deny &= pas.users | pas.group | pas.groups | 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_OWNER;
267 ace++;
268 acl->naces++;
269 }
270
271 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
272 ace->flag = eflag;
273 ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
274 ace->whotype = NFS4_ACL_WHO_OWNER;
275 ace++;
276 acl->naces++;
1da177e4
LT
277 pa++;
278
279 while (pa->e_tag == ACL_USER) {
bec50c47
BF
280 deny = ~(pa->e_perm & pas.mask);
281 deny &= pas.groups | pas.group | pas.other;
282 if (deny) {
283 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
284 ace->flag = eflag;
285 ace->access_mask = deny_mask_from_posix(deny, flags);
286 ace->whotype = NFS4_ACL_WHO_NAMED;
ab8e4aee 287 ace->who_uid = pa->e_uid;
bec50c47
BF
288 ace++;
289 acl->naces++;
290 }
291 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
292 ace->flag = eflag;
293 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
294 flags);
295 ace->whotype = NFS4_ACL_WHO_NAMED;
ab8e4aee 296 ace->who_uid = pa->e_uid;
bec50c47
BF
297 ace++;
298 acl->naces++;
1da177e4
LT
299 pa++;
300 }
301
302 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
303 * since a user can be in more than one group. */
304
305 /* allow ACEs */
306
1da177e4 307 group_owner_entry = pa;
bec50c47
BF
308
309 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
310 ace->flag = eflag;
311 ace->access_mask = mask_from_posix(pas.group, flags);
312 ace->whotype = NFS4_ACL_WHO_GROUP;
313 ace++;
314 acl->naces++;
1da177e4
LT
315 pa++;
316
317 while (pa->e_tag == ACL_GROUP) {
bec50c47
BF
318 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
319 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
320 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
321 flags);
322 ace->whotype = NFS4_ACL_WHO_NAMED;
ab8e4aee 323 ace->who_gid = pa->e_gid;
bec50c47
BF
324 ace++;
325 acl->naces++;
1da177e4
LT
326 pa++;
327 }
328
329 /* deny ACEs */
330
331 pa = group_owner_entry;
bec50c47
BF
332
333 deny = ~pas.group & pas.other;
334 if (deny) {
335 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
d8d0b85b 336 ace->flag = eflag;
bec50c47
BF
337 ace->access_mask = deny_mask_from_posix(deny, flags);
338 ace->whotype = NFS4_ACL_WHO_GROUP;
339 ace++;
340 acl->naces++;
341 }
1da177e4 342 pa++;
bec50c47 343
1da177e4 344 while (pa->e_tag == ACL_GROUP) {
bec50c47
BF
345 deny = ~(pa->e_perm & pas.mask);
346 deny &= pas.other;
347 if (deny) {
348 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
349 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
55bb55dc 350 ace->access_mask = deny_mask_from_posix(deny, flags);
bec50c47 351 ace->whotype = NFS4_ACL_WHO_NAMED;
ab8e4aee 352 ace->who_gid = pa->e_gid;
bec50c47
BF
353 ace++;
354 acl->naces++;
355 }
1da177e4
LT
356 pa++;
357 }
358
359 if (pa->e_tag == ACL_MASK)
360 pa++;
bec50c47
BF
361 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
362 ace->flag = eflag;
363 ace->access_mask = mask_from_posix(pa->e_perm, flags);
364 ace->whotype = NFS4_ACL_WHO_EVERYONE;
365 acl->naces++;
1da177e4
LT
366}
367
ab8e4aee
EB
368static bool
369pace_gt(struct posix_acl_entry *pace1, struct posix_acl_entry *pace2)
370{
371 if (pace1->e_tag != pace2->e_tag)
372 return pace1->e_tag > pace2->e_tag;
373 if (pace1->e_tag == ACL_USER)
374 return uid_gt(pace1->e_uid, pace2->e_uid);
375 if (pace1->e_tag == ACL_GROUP)
376 return gid_gt(pace1->e_gid, pace2->e_gid);
377 return false;
378}
379
1da177e4
LT
380static void
381sort_pacl_range(struct posix_acl *pacl, int start, int end) {
382 int sorted = 0, i;
383 struct posix_acl_entry tmp;
384
385 /* We just do a bubble sort; easy to do in place, and we're not
386 * expecting acl's to be long enough to justify anything more. */
387 while (!sorted) {
388 sorted = 1;
389 for (i = start; i < end; i++) {
ab8e4aee
EB
390 if (pace_gt(&pacl->a_entries[i],
391 &pacl->a_entries[i+1])) {
1da177e4
LT
392 sorted = 0;
393 tmp = pacl->a_entries[i];
394 pacl->a_entries[i] = pacl->a_entries[i+1];
395 pacl->a_entries[i+1] = tmp;
396 }
397 }
398 }
399}
400
401static void
402sort_pacl(struct posix_acl *pacl)
403{
404 /* posix_acl_valid requires that users and groups be in order
405 * by uid/gid. */
406 int i, j;
407
408 if (pacl->a_count <= 4)
409 return; /* no users or groups */
410 i = 1;
411 while (pacl->a_entries[i].e_tag == ACL_USER)
412 i++;
413 sort_pacl_range(pacl, 1, i-1);
414
415 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
aba24d71 416 j = ++i;
1da177e4
LT
417 while (pacl->a_entries[j].e_tag == ACL_GROUP)
418 j++;
419 sort_pacl_range(pacl, i, j-1);
420 return;
421}
422
09229edb
BF
423/*
424 * While processing the NFSv4 ACE, this maintains bitmasks representing
425 * which permission bits have been allowed and which denied to a given
426 * entity: */
427struct posix_ace_state {
428 u32 allow;
429 u32 deny;
430};
431
432struct posix_user_ace_state {
ab8e4aee
EB
433 union {
434 kuid_t uid;
435 kgid_t gid;
436 };
09229edb
BF
437 struct posix_ace_state perms;
438};
439
440struct posix_ace_state_array {
441 int n;
442 struct posix_user_ace_state aces[];
443};
444
445/*
446 * While processing the NFSv4 ACE, this maintains the partial permissions
447 * calculated so far: */
448
449struct posix_acl_state {
3160a711 450 int empty;
09229edb
BF
451 struct posix_ace_state owner;
452 struct posix_ace_state group;
453 struct posix_ace_state other;
454 struct posix_ace_state everyone;
455 struct posix_ace_state mask; /* Deny unused in this case */
456 struct posix_ace_state_array *users;
457 struct posix_ace_state_array *groups;
458};
459
1da177e4 460static int
09229edb 461init_state(struct posix_acl_state *state, int cnt)
1da177e4 462{
09229edb
BF
463 int alloc;
464
465 memset(state, 0, sizeof(struct posix_acl_state));
3160a711 466 state->empty = 1;
09229edb
BF
467 /*
468 * In the worst case, each individual acl could be for a distinct
469 * named user or group, but we don't no which, so we allocate
470 * enough space for either:
471 */
472 alloc = sizeof(struct posix_ace_state_array)
91b80969 473 + cnt*sizeof(struct posix_user_ace_state);
09229edb
BF
474 state->users = kzalloc(alloc, GFP_KERNEL);
475 if (!state->users)
476 return -ENOMEM;
477 state->groups = kzalloc(alloc, GFP_KERNEL);
478 if (!state->groups) {
479 kfree(state->users);
480 return -ENOMEM;
481 }
482 return 0;
1da177e4
LT
483}
484
09229edb
BF
485static void
486free_state(struct posix_acl_state *state) {
487 kfree(state->users);
488 kfree(state->groups);
1da177e4
LT
489}
490
09229edb 491static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
1da177e4 492{
09229edb 493 state->mask.allow |= astate->allow;
1da177e4
LT
494}
495
09229edb
BF
496/*
497 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
498 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
499 * to traditional read/write/execute permissions.
500 *
501 * It's problematic to reject acls that use certain mode bits, because it
502 * places the burden on users to learn the rules about which bits one
503 * particular server sets, without giving the user a lot of help--we return an
504 * error that could mean any number of different things. To make matters
505 * worse, the problematic bits might be introduced by some application that's
506 * automatically mapping from some other acl model.
507 *
508 * So wherever possible we accept anything, possibly erring on the side of
509 * denying more permissions than necessary.
510 *
511 * However we do reject *explicit* DENY's of a few bits representing
512 * permissions we could never deny:
513 */
1da177e4 514
09229edb
BF
515static inline int check_deny(u32 mask, int isowner)
516{
517 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
518 return -EINVAL;
519 if (!isowner)
520 return 0;
521 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
522 return -EINVAL;
523 return 0;
1da177e4
LT
524}
525
09229edb
BF
526static struct posix_acl *
527posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
1da177e4 528{
09229edb
BF
529 struct posix_acl_entry *pace;
530 struct posix_acl *pacl;
531 int nace;
532 int i, error = 0;
1da177e4 533
3160a711
BF
534 /*
535 * ACLs with no ACEs are treated differently in the inheritable
536 * and effective cases: when there are no inheritable ACEs, we
537 * set a zero-length default posix acl:
538 */
539 if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT)) {
540 pacl = posix_acl_alloc(0, GFP_KERNEL);
541 return pacl ? pacl : ERR_PTR(-ENOMEM);
542 }
543 /*
544 * When there are no effective ACEs, the following will end
545 * up setting a 3-element effective posix ACL with all
546 * permissions zero.
547 */
09229edb
BF
548 nace = 4 + state->users->n + state->groups->n;
549 pacl = posix_acl_alloc(nace, GFP_KERNEL);
550 if (!pacl)
551 return ERR_PTR(-ENOMEM);
1da177e4 552
09229edb
BF
553 pace = pacl->a_entries;
554 pace->e_tag = ACL_USER_OBJ;
555 error = check_deny(state->owner.deny, 1);
556 if (error)
557 goto out_err;
558 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
09229edb
BF
559
560 for (i=0; i < state->users->n; i++) {
561 pace++;
562 pace->e_tag = ACL_USER;
563 error = check_deny(state->users->aces[i].perms.deny, 0);
564 if (error)
565 goto out_err;
566 low_mode_from_nfs4(state->users->aces[i].perms.allow,
567 &pace->e_perm, flags);
ab8e4aee 568 pace->e_uid = state->users->aces[i].uid;
09229edb 569 add_to_mask(state, &state->users->aces[i].perms);
1da177e4
LT
570 }
571
09229edb
BF
572 pace++;
573 pace->e_tag = ACL_GROUP_OBJ;
574 error = check_deny(state->group.deny, 0);
575 if (error)
576 goto out_err;
577 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
09229edb
BF
578 add_to_mask(state, &state->group);
579
580 for (i=0; i < state->groups->n; i++) {
581 pace++;
582 pace->e_tag = ACL_GROUP;
583 error = check_deny(state->groups->aces[i].perms.deny, 0);
584 if (error)
585 goto out_err;
586 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
587 &pace->e_perm, flags);
ab8e4aee 588 pace->e_gid = state->groups->aces[i].gid;
09229edb
BF
589 add_to_mask(state, &state->groups->aces[i].perms);
590 }
1da177e4 591
09229edb
BF
592 pace++;
593 pace->e_tag = ACL_MASK;
594 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
1da177e4 595
09229edb
BF
596 pace++;
597 pace->e_tag = ACL_OTHER;
598 error = check_deny(state->other.deny, 0);
599 if (error)
600 goto out_err;
601 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
1da177e4 602
09229edb
BF
603 return pacl;
604out_err:
605 posix_acl_release(pacl);
606 return ERR_PTR(error);
607}
1da177e4 608
09229edb
BF
609static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
610{
611 /* Allow all bits in the mask not already denied: */
612 astate->allow |= mask & ~astate->deny;
613}
1da177e4 614
09229edb
BF
615static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
616{
617 /* Deny all bits in the mask not already allowed: */
618 astate->deny |= mask & ~astate->allow;
619}
1da177e4 620
ab8e4aee 621static int find_uid(struct posix_acl_state *state, kuid_t uid)
09229edb 622{
ab8e4aee 623 struct posix_ace_state_array *a = state->users;
09229edb 624 int i;
1da177e4 625
09229edb 626 for (i = 0; i < a->n; i++)
ab8e4aee 627 if (uid_eq(a->aces[i].uid, uid))
09229edb
BF
628 return i;
629 /* Not found: */
630 a->n++;
631 a->aces[i].uid = uid;
632 a->aces[i].perms.allow = state->everyone.allow;
633 a->aces[i].perms.deny = state->everyone.deny;
1da177e4 634
09229edb 635 return i;
1da177e4
LT
636}
637
ab8e4aee
EB
638static int find_gid(struct posix_acl_state *state, kgid_t gid)
639{
640 struct posix_ace_state_array *a = state->groups;
641 int i;
642
643 for (i = 0; i < a->n; i++)
644 if (gid_eq(a->aces[i].gid, gid))
645 return i;
646 /* Not found: */
647 a->n++;
648 a->aces[i].gid = gid;
649 a->aces[i].perms.allow = state->everyone.allow;
650 a->aces[i].perms.deny = state->everyone.deny;
651
652 return i;
653}
654
09229edb 655static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
1da177e4 656{
09229edb 657 int i;
1da177e4 658
09229edb
BF
659 for (i=0; i < a->n; i++)
660 deny_bits(&a->aces[i].perms, mask);
1da177e4
LT
661}
662
09229edb 663static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
1da177e4 664{
09229edb 665 int i;
1da177e4 666
09229edb
BF
667 for (i=0; i < a->n; i++)
668 allow_bits(&a->aces[i].perms, mask);
1da177e4
LT
669}
670
09229edb
BF
671static void process_one_v4_ace(struct posix_acl_state *state,
672 struct nfs4_ace *ace)
1da177e4 673{
09229edb
BF
674 u32 mask = ace->access_mask;
675 int i;
676
3160a711
BF
677 state->empty = 0;
678
09229edb
BF
679 switch (ace2type(ace)) {
680 case ACL_USER_OBJ:
681 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
682 allow_bits(&state->owner, mask);
683 } else {
684 deny_bits(&state->owner, mask);
685 }
686 break;
687 case ACL_USER:
ab8e4aee 688 i = find_uid(state, ace->who_uid);
09229edb
BF
689 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
690 allow_bits(&state->users->aces[i].perms, mask);
691 } else {
692 deny_bits(&state->users->aces[i].perms, mask);
693 mask = state->users->aces[i].perms.deny;
694 deny_bits(&state->owner, mask);
695 }
696 break;
697 case ACL_GROUP_OBJ:
698 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
699 allow_bits(&state->group, mask);
700 } else {
701 deny_bits(&state->group, mask);
702 mask = state->group.deny;
703 deny_bits(&state->owner, mask);
704 deny_bits(&state->everyone, mask);
705 deny_bits_array(state->users, mask);
706 deny_bits_array(state->groups, mask);
707 }
708 break;
709 case ACL_GROUP:
ab8e4aee 710 i = find_gid(state, ace->who_gid);
09229edb
BF
711 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
712 allow_bits(&state->groups->aces[i].perms, mask);
713 } else {
714 deny_bits(&state->groups->aces[i].perms, mask);
715 mask = state->groups->aces[i].perms.deny;
716 deny_bits(&state->owner, mask);
717 deny_bits(&state->group, mask);
718 deny_bits(&state->everyone, mask);
719 deny_bits_array(state->users, mask);
720 deny_bits_array(state->groups, mask);
721 }
722 break;
723 case ACL_OTHER:
724 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
725 allow_bits(&state->owner, mask);
726 allow_bits(&state->group, mask);
727 allow_bits(&state->other, mask);
728 allow_bits(&state->everyone, mask);
729 allow_bits_array(state->users, mask);
730 allow_bits_array(state->groups, mask);
731 } else {
732 deny_bits(&state->owner, mask);
733 deny_bits(&state->group, mask);
734 deny_bits(&state->other, mask);
735 deny_bits(&state->everyone, mask);
736 deny_bits_array(state->users, mask);
737 deny_bits_array(state->groups, mask);
738 }
1da177e4
LT
739 }
740}
741
4ac7249e
CH
742static int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl,
743 struct posix_acl **pacl, struct posix_acl **dpacl,
744 unsigned int flags)
1da177e4 745{
575a6290 746 struct posix_acl_state effective_acl_state, default_acl_state;
09229edb
BF
747 struct nfs4_ace *ace;
748 int ret;
1da177e4 749
575a6290 750 ret = init_state(&effective_acl_state, acl->naces);
09229edb 751 if (ret)
575a6290
BF
752 return ret;
753 ret = init_state(&default_acl_state, acl->naces);
754 if (ret)
755 goto out_estate;
756 ret = -EINVAL;
28e05dd8 757 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
09229edb
BF
758 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
759 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
575a6290 760 goto out_dstate;
b548edc2 761 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
575a6290 762 goto out_dstate;
7bdfa68c 763 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
575a6290 764 process_one_v4_ace(&effective_acl_state, ace);
b548edc2 765 continue;
7bdfa68c 766 }
575a6290
BF
767 if (!(flags & NFS4_ACL_DIR))
768 goto out_dstate;
7bdfa68c
BF
769 /*
770 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
771 * is set, we're effectively turning on the other. That's OK,
772 * according to rfc 3530.
773 */
575a6290
BF
774 process_one_v4_ace(&default_acl_state, ace);
775
776 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
777 process_one_v4_ace(&effective_acl_state, ace);
1da177e4 778 }
575a6290
BF
779 *pacl = posix_state_to_acl(&effective_acl_state, flags);
780 if (IS_ERR(*pacl)) {
781 ret = PTR_ERR(*pacl);
4b2ca38a 782 *pacl = NULL;
575a6290
BF
783 goto out_dstate;
784 }
3160a711
BF
785 *dpacl = posix_state_to_acl(&default_acl_state,
786 flags | NFS4_ACL_TYPE_DEFAULT);
575a6290
BF
787 if (IS_ERR(*dpacl)) {
788 ret = PTR_ERR(*dpacl);
4b2ca38a 789 *dpacl = NULL;
575a6290 790 posix_acl_release(*pacl);
4b2ca38a 791 *pacl = NULL;
575a6290
BF
792 goto out_dstate;
793 }
794 sort_pacl(*pacl);
795 sort_pacl(*dpacl);
796 ret = 0;
797out_dstate:
798 free_state(&default_acl_state);
799out_estate:
800 free_state(&effective_acl_state);
801 return ret;
1da177e4
LT
802}
803
4ac7249e
CH
804__be32
805nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
806 struct nfs4_acl *acl)
807{
808 __be32 error;
809 int host_error;
810 struct dentry *dentry;
811 struct inode *inode;
812 struct posix_acl *pacl = NULL, *dpacl = NULL;
813 unsigned int flags = 0;
814
815 /* Get inode */
816 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
817 if (error)
818 return error;
819
820 dentry = fhp->fh_dentry;
821 inode = dentry->d_inode;
822
823 if (!inode->i_op->set_acl || !IS_POSIXACL(inode))
824 return nfserr_attrnotsupp;
825
826 if (S_ISDIR(inode->i_mode))
827 flags = NFS4_ACL_DIR;
828
829 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
830 if (host_error == -EINVAL)
831 return nfserr_attrnotsupp;
832 if (host_error < 0)
833 goto out_nfserr;
834
835 host_error = inode->i_op->set_acl(inode, pacl, ACL_TYPE_ACCESS);
836 if (host_error < 0)
837 goto out_release;
838
839 if (S_ISDIR(inode->i_mode)) {
840 host_error = inode->i_op->set_acl(inode, dpacl,
841 ACL_TYPE_DEFAULT);
842 }
843
844out_release:
845 posix_acl_release(pacl);
846 posix_acl_release(dpacl);
847out_nfserr:
848 if (host_error == -EOPNOTSUPP)
849 return nfserr_attrnotsupp;
850 else
851 return nfserrno(host_error);
852}
853
854
1da177e4
LT
855static short
856ace2type(struct nfs4_ace *ace)
857{
858 switch (ace->whotype) {
859 case NFS4_ACL_WHO_NAMED:
860 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
861 ACL_GROUP : ACL_USER);
862 case NFS4_ACL_WHO_OWNER:
863 return ACL_USER_OBJ;
864 case NFS4_ACL_WHO_GROUP:
865 return ACL_GROUP_OBJ;
866 case NFS4_ACL_WHO_EVERYONE:
867 return ACL_OTHER;
868 }
869 BUG();
870 return -1;
871}
872
1da177e4 873struct nfs4_acl *
28e05dd8 874nfs4_acl_new(int n)
1da177e4
LT
875{
876 struct nfs4_acl *acl;
877
28e05dd8
BF
878 acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL);
879 if (acl == NULL)
1da177e4 880 return NULL;
1da177e4 881 acl->naces = 0;
1da177e4
LT
882 return acl;
883}
884
1da177e4
LT
885static struct {
886 char *string;
887 int stringlen;
888 int type;
889} s2t_map[] = {
890 {
891 .string = "OWNER@",
892 .stringlen = sizeof("OWNER@") - 1,
893 .type = NFS4_ACL_WHO_OWNER,
894 },
895 {
896 .string = "GROUP@",
897 .stringlen = sizeof("GROUP@") - 1,
898 .type = NFS4_ACL_WHO_GROUP,
899 },
900 {
901 .string = "EVERYONE@",
902 .stringlen = sizeof("EVERYONE@") - 1,
903 .type = NFS4_ACL_WHO_EVERYONE,
904 },
905};
906
907int
908nfs4_acl_get_whotype(char *p, u32 len)
909{
910 int i;
911
e8c96f8c 912 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
1da177e4
LT
913 if (s2t_map[i].stringlen == len &&
914 0 == memcmp(s2t_map[i].string, p, len))
915 return s2t_map[i].type;
916 }
917 return NFS4_ACL_WHO_NAMED;
918}
919
3554116d 920__be32 nfs4_acl_write_who(int who, __be32 **p, int *len)
1da177e4
LT
921{
922 int i;
3554116d 923 int bytes;
1da177e4 924
e8c96f8c 925 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
3554116d
BF
926 if (s2t_map[i].type != who)
927 continue;
928 bytes = 4 + (XDR_QUADLEN(s2t_map[i].stringlen) << 2);
929 if (bytes > *len)
930 return nfserr_resource;
931 *p = xdr_encode_opaque(*p, s2t_map[i].string,
932 s2t_map[i].stringlen);
933 *len -= bytes;
934 return 0;
1da177e4 935 }
3554116d 936 WARN_ON_ONCE(1);
1da177e4
LT
937 return -1;
938}