xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_acl.c (revision b87f3af36bb994656da117319f5129ddfd05ed21)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/vfs.h>
35 #include <sys/vnode.h>
36 #include <sys/sid.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39 #include <sys/kmem.h>
40 #include <sys/cmn_err.h>
41 #include <sys/errno.h>
42 #include <sys/unistd.h>
43 #include <sys/sdt.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/mode.h>
46 #include <sys/policy.h>
47 #include <sys/zfs_znode.h>
48 #include <sys/zfs_fuid.h>
49 #include <sys/zfs_acl.h>
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_vfsops.h>
52 #include <sys/dmu.h>
53 #include <sys/dnode.h>
54 #include <sys/zap.h>
55 #include "fs/fs_subr.h"
56 #include <acl/acl_common.h>
57 
58 #define	ALLOW	ACE_ACCESS_ALLOWED_ACE_TYPE
59 #define	DENY	ACE_ACCESS_DENIED_ACE_TYPE
60 #define	MAX_ACE_TYPE	ACE_SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE
61 
62 #define	OWNING_GROUP		(ACE_GROUP|ACE_IDENTIFIER_GROUP)
63 #define	EVERYONE_ALLOW_MASK (ACE_READ_ACL|ACE_READ_ATTRIBUTES | \
64     ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE)
65 #define	EVERYONE_DENY_MASK (ACE_WRITE_ACL|ACE_WRITE_OWNER | \
66     ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
67 #define	OWNER_ALLOW_MASK (ACE_WRITE_ACL | ACE_WRITE_OWNER | \
68     ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
69 #define	WRITE_MASK_DATA (ACE_WRITE_DATA|ACE_APPEND_DATA|ACE_WRITE_NAMED_ATTRS)
70 
71 #define	ZFS_CHECKED_MASKS (ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_DATA| \
72     ACE_READ_NAMED_ATTRS|ACE_WRITE_DATA|ACE_WRITE_ATTRIBUTES| \
73     ACE_WRITE_NAMED_ATTRS|ACE_APPEND_DATA|ACE_EXECUTE|ACE_WRITE_OWNER| \
74     ACE_WRITE_ACL|ACE_DELETE|ACE_DELETE_CHILD|ACE_SYNCHRONIZE)
75 
76 #define	WRITE_MASK (WRITE_MASK_DATA|ACE_WRITE_ATTRIBUTES|ACE_WRITE_ACL|\
77     ACE_WRITE_OWNER|ACE_DELETE|ACE_DELETE_CHILD)
78 
79 #define	OGE_CLEAR	(ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
80     ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
81 
82 #define	OKAY_MASK_BITS (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
83     ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
84 
85 #define	ALL_INHERIT	(ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE | \
86     ACE_NO_PROPAGATE_INHERIT_ACE|ACE_INHERIT_ONLY_ACE|ACE_INHERITED_ACE)
87 
88 #define	RESTRICTED_CLEAR	(ACE_WRITE_ACL|ACE_WRITE_OWNER)
89 
90 #define	V4_ACL_WIDE_FLAGS (ZFS_ACL_AUTO_INHERIT|ZFS_ACL_DEFAULTED|\
91     ZFS_ACL_PROTECTED)
92 
93 #define	ZFS_ACL_WIDE_FLAGS (V4_ACL_WIDE_FLAGS|ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|\
94     ZFS_ACL_OBJ_ACE)
95 
96 static uint16_t
97 zfs_ace_v0_get_type(void *acep)
98 {
99 	return (((zfs_oldace_t *)acep)->z_type);
100 }
101 
102 static uint16_t
103 zfs_ace_v0_get_flags(void *acep)
104 {
105 	return (((zfs_oldace_t *)acep)->z_flags);
106 }
107 
108 static uint32_t
109 zfs_ace_v0_get_mask(void *acep)
110 {
111 	return (((zfs_oldace_t *)acep)->z_access_mask);
112 }
113 
114 static uint64_t
115 zfs_ace_v0_get_who(void *acep)
116 {
117 	return (((zfs_oldace_t *)acep)->z_fuid);
118 }
119 
120 static void
121 zfs_ace_v0_set_type(void *acep, uint16_t type)
122 {
123 	((zfs_oldace_t *)acep)->z_type = type;
124 }
125 
126 static void
127 zfs_ace_v0_set_flags(void *acep, uint16_t flags)
128 {
129 	((zfs_oldace_t *)acep)->z_flags = flags;
130 }
131 
132 static void
133 zfs_ace_v0_set_mask(void *acep, uint32_t mask)
134 {
135 	((zfs_oldace_t *)acep)->z_access_mask = mask;
136 }
137 
138 static void
139 zfs_ace_v0_set_who(void *acep, uint64_t who)
140 {
141 	((zfs_oldace_t *)acep)->z_fuid = who;
142 }
143 
144 /*ARGSUSED*/
145 static size_t
146 zfs_ace_v0_size(void *acep)
147 {
148 	return (sizeof (zfs_oldace_t));
149 }
150 
151 static size_t
152 zfs_ace_v0_abstract_size(void)
153 {
154 	return (sizeof (zfs_oldace_t));
155 }
156 
157 static int
158 zfs_ace_v0_mask_off(void)
159 {
160 	return (offsetof(zfs_oldace_t, z_access_mask));
161 }
162 
163 /*ARGSUSED*/
164 static int
165 zfs_ace_v0_data(void *acep, void **datap)
166 {
167 	*datap = NULL;
168 	return (0);
169 }
170 
171 static acl_ops_t zfs_acl_v0_ops = {
172 	zfs_ace_v0_get_mask,
173 	zfs_ace_v0_set_mask,
174 	zfs_ace_v0_get_flags,
175 	zfs_ace_v0_set_flags,
176 	zfs_ace_v0_get_type,
177 	zfs_ace_v0_set_type,
178 	zfs_ace_v0_get_who,
179 	zfs_ace_v0_set_who,
180 	zfs_ace_v0_size,
181 	zfs_ace_v0_abstract_size,
182 	zfs_ace_v0_mask_off,
183 	zfs_ace_v0_data
184 };
185 
186 static uint16_t
187 zfs_ace_fuid_get_type(void *acep)
188 {
189 	return (((zfs_ace_hdr_t *)acep)->z_type);
190 }
191 
192 static uint16_t
193 zfs_ace_fuid_get_flags(void *acep)
194 {
195 	return (((zfs_ace_hdr_t *)acep)->z_flags);
196 }
197 
198 static uint32_t
199 zfs_ace_fuid_get_mask(void *acep)
200 {
201 	return (((zfs_ace_hdr_t *)acep)->z_access_mask);
202 }
203 
204 static uint64_t
205 zfs_ace_fuid_get_who(void *args)
206 {
207 	uint16_t entry_type;
208 	zfs_ace_t *acep = args;
209 
210 	entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS;
211 
212 	if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP ||
213 	    entry_type == ACE_EVERYONE)
214 		return (-1);
215 	return (((zfs_ace_t *)acep)->z_fuid);
216 }
217 
218 static void
219 zfs_ace_fuid_set_type(void *acep, uint16_t type)
220 {
221 	((zfs_ace_hdr_t *)acep)->z_type = type;
222 }
223 
224 static void
225 zfs_ace_fuid_set_flags(void *acep, uint16_t flags)
226 {
227 	((zfs_ace_hdr_t *)acep)->z_flags = flags;
228 }
229 
230 static void
231 zfs_ace_fuid_set_mask(void *acep, uint32_t mask)
232 {
233 	((zfs_ace_hdr_t *)acep)->z_access_mask = mask;
234 }
235 
236 static void
237 zfs_ace_fuid_set_who(void *arg, uint64_t who)
238 {
239 	zfs_ace_t *acep = arg;
240 
241 	uint16_t entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS;
242 
243 	if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP ||
244 	    entry_type == ACE_EVERYONE)
245 		return;
246 	acep->z_fuid = who;
247 }
248 
249 static size_t
250 zfs_ace_fuid_size(void *acep)
251 {
252 	zfs_ace_hdr_t *zacep = acep;
253 	uint16_t entry_type;
254 
255 	switch (zacep->z_type) {
256 	case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
257 	case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
258 	case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
259 	case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
260 		return (sizeof (zfs_object_ace_t));
261 	case ALLOW:
262 	case DENY:
263 		entry_type =
264 		    (((zfs_ace_hdr_t *)acep)->z_flags & ACE_TYPE_FLAGS);
265 		if (entry_type == ACE_OWNER ||
266 		    entry_type == (ACE_GROUP | ACE_IDENTIFIER_GROUP) ||
267 		    entry_type == ACE_EVERYONE)
268 			return (sizeof (zfs_ace_hdr_t));
269 		/*FALLTHROUGH*/
270 	default:
271 		return (sizeof (zfs_ace_t));
272 	}
273 }
274 
275 static size_t
276 zfs_ace_fuid_abstract_size(void)
277 {
278 	return (sizeof (zfs_ace_hdr_t));
279 }
280 
281 static int
282 zfs_ace_fuid_mask_off(void)
283 {
284 	return (offsetof(zfs_ace_hdr_t, z_access_mask));
285 }
286 
287 static int
288 zfs_ace_fuid_data(void *acep, void **datap)
289 {
290 	zfs_ace_t *zacep = acep;
291 	zfs_object_ace_t *zobjp;
292 
293 	switch (zacep->z_hdr.z_type) {
294 	case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
295 	case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
296 	case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
297 	case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
298 		zobjp = acep;
299 		*datap = (caddr_t)zobjp + sizeof (zfs_ace_t);
300 		return (sizeof (zfs_object_ace_t) - sizeof (zfs_ace_t));
301 	default:
302 		*datap = NULL;
303 		return (0);
304 	}
305 }
306 
307 static acl_ops_t zfs_acl_fuid_ops = {
308 	zfs_ace_fuid_get_mask,
309 	zfs_ace_fuid_set_mask,
310 	zfs_ace_fuid_get_flags,
311 	zfs_ace_fuid_set_flags,
312 	zfs_ace_fuid_get_type,
313 	zfs_ace_fuid_set_type,
314 	zfs_ace_fuid_get_who,
315 	zfs_ace_fuid_set_who,
316 	zfs_ace_fuid_size,
317 	zfs_ace_fuid_abstract_size,
318 	zfs_ace_fuid_mask_off,
319 	zfs_ace_fuid_data
320 };
321 
322 static int
323 zfs_acl_version(int version)
324 {
325 	if (version < ZPL_VERSION_FUID)
326 		return (ZFS_ACL_VERSION_INITIAL);
327 	else
328 		return (ZFS_ACL_VERSION_FUID);
329 }
330 
331 static int
332 zfs_acl_version_zp(znode_t *zp)
333 {
334 	return (zfs_acl_version(zp->z_zfsvfs->z_version));
335 }
336 
337 static zfs_acl_t *
338 zfs_acl_alloc(int vers)
339 {
340 	zfs_acl_t *aclp;
341 
342 	aclp = kmem_zalloc(sizeof (zfs_acl_t), KM_SLEEP);
343 	list_create(&aclp->z_acl, sizeof (zfs_acl_node_t),
344 	    offsetof(zfs_acl_node_t, z_next));
345 	aclp->z_version = vers;
346 	if (vers == ZFS_ACL_VERSION_FUID)
347 		aclp->z_ops = zfs_acl_fuid_ops;
348 	else
349 		aclp->z_ops = zfs_acl_v0_ops;
350 	return (aclp);
351 }
352 
353 static zfs_acl_node_t *
354 zfs_acl_node_alloc(size_t bytes)
355 {
356 	zfs_acl_node_t *aclnode;
357 
358 	aclnode = kmem_zalloc(sizeof (zfs_acl_node_t), KM_SLEEP);
359 	if (bytes) {
360 		aclnode->z_acldata = kmem_alloc(bytes, KM_SLEEP);
361 		aclnode->z_allocdata = aclnode->z_acldata;
362 		aclnode->z_allocsize = bytes;
363 		aclnode->z_size = bytes;
364 	}
365 
366 	return (aclnode);
367 }
368 
369 static void
370 zfs_acl_node_free(zfs_acl_node_t *aclnode)
371 {
372 	if (aclnode->z_allocsize)
373 		kmem_free(aclnode->z_allocdata, aclnode->z_allocsize);
374 	kmem_free(aclnode, sizeof (zfs_acl_node_t));
375 }
376 
377 static void
378 zfs_acl_release_nodes(zfs_acl_t *aclp)
379 {
380 	zfs_acl_node_t *aclnode;
381 
382 	while (aclnode = list_head(&aclp->z_acl)) {
383 		list_remove(&aclp->z_acl, aclnode);
384 		zfs_acl_node_free(aclnode);
385 	}
386 	aclp->z_acl_count = 0;
387 	aclp->z_acl_bytes = 0;
388 }
389 
390 void
391 zfs_acl_free(zfs_acl_t *aclp)
392 {
393 	zfs_acl_release_nodes(aclp);
394 	list_destroy(&aclp->z_acl);
395 	kmem_free(aclp, sizeof (zfs_acl_t));
396 }
397 
398 static boolean_t
399 zfs_ace_valid(vtype_t obj_type, zfs_acl_t *aclp, uint16_t type, uint16_t iflags)
400 {
401 	/*
402 	 * first check type of entry
403 	 */
404 
405 	switch (iflags & ACE_TYPE_FLAGS) {
406 	case ACE_OWNER:
407 	case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
408 	case ACE_IDENTIFIER_GROUP:
409 	case ACE_EVERYONE:
410 	case 0:	/* User entry */
411 		break;
412 	default:
413 		return (B_FALSE);
414 
415 	}
416 
417 	/*
418 	 * next check inheritance level flags
419 	 */
420 
421 	if (type != ALLOW && type > MAX_ACE_TYPE) {
422 		return (B_FALSE);
423 	}
424 
425 	switch (type) {
426 	case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
427 	case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
428 	case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
429 	case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
430 		if (aclp->z_version < ZFS_ACL_VERSION_FUID)
431 			return (B_FALSE);
432 		aclp->z_hints |= ZFS_ACL_OBJ_ACE;
433 	}
434 
435 	if (obj_type == VDIR &&
436 	    (iflags & (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE)))
437 		aclp->z_hints |= ZFS_INHERIT_ACE;
438 
439 	if (iflags & (ACE_INHERIT_ONLY_ACE|ACE_NO_PROPAGATE_INHERIT_ACE)) {
440 		if ((iflags & (ACE_FILE_INHERIT_ACE|
441 		    ACE_DIRECTORY_INHERIT_ACE)) == 0) {
442 			return (B_FALSE);
443 		}
444 	}
445 
446 	return (B_TRUE);
447 }
448 
449 static void *
450 zfs_acl_next_ace(zfs_acl_t *aclp, void *start, uint64_t *who,
451     uint32_t *access_mask, uint16_t *iflags, uint16_t *type)
452 {
453 	zfs_acl_node_t *aclnode;
454 
455 	if (start == NULL) {
456 		aclnode = list_head(&aclp->z_acl);
457 		if (aclnode == NULL)
458 			return (NULL);
459 
460 		aclp->z_next_ace = aclnode->z_acldata;
461 		aclp->z_curr_node = aclnode;
462 		aclnode->z_ace_idx = 0;
463 	}
464 
465 	aclnode = aclp->z_curr_node;
466 
467 	if (aclnode == NULL)
468 		return (NULL);
469 
470 	if (aclnode->z_ace_idx >= aclnode->z_ace_count) {
471 		aclnode = list_next(&aclp->z_acl, aclnode);
472 		if (aclnode == NULL)
473 			return (NULL);
474 		else {
475 			aclp->z_curr_node = aclnode;
476 			aclnode->z_ace_idx = 0;
477 			aclp->z_next_ace = aclnode->z_acldata;
478 		}
479 	}
480 
481 	if (aclnode->z_ace_idx < aclnode->z_ace_count) {
482 		void *acep = aclp->z_next_ace;
483 		*iflags = aclp->z_ops.ace_flags_get(acep);
484 		*type = aclp->z_ops.ace_type_get(acep);
485 		*access_mask = aclp->z_ops.ace_mask_get(acep);
486 		*who = aclp->z_ops.ace_who_get(acep);
487 		aclp->z_next_ace = (caddr_t)aclp->z_next_ace +
488 		    aclp->z_ops.ace_size(acep);
489 		aclnode->z_ace_idx++;
490 		return ((void *)acep);
491 	}
492 	return (NULL);
493 }
494 
495 /*ARGSUSED*/
496 static uint64_t
497 zfs_ace_walk(void *datap, uint64_t cookie, int aclcnt,
498     uint16_t *flags, uint16_t *type, uint32_t *mask)
499 {
500 	zfs_acl_t *aclp = datap;
501 	zfs_ace_hdr_t *acep = (zfs_ace_hdr_t *)(uintptr_t)cookie;
502 	uint64_t who;
503 
504 	acep = zfs_acl_next_ace(aclp, acep, &who, mask,
505 	    flags, type);
506 	return ((uint64_t)(uintptr_t)acep);
507 }
508 
509 static zfs_acl_node_t *
510 zfs_acl_curr_node(zfs_acl_t *aclp)
511 {
512 	ASSERT(aclp->z_curr_node);
513 	return (aclp->z_curr_node);
514 }
515 
516 /*
517  * Copy ACE to internal ZFS format.
518  * While processing the ACL each ACE will be validated for correctness.
519  * ACE FUIDs will be created later.
520  */
521 int
522 zfs_copy_ace_2_fuid(vtype_t obj_type, zfs_acl_t *aclp, void *datap,
523     zfs_ace_t *z_acl, int aclcnt, size_t *size)
524 {
525 	int i;
526 	uint16_t entry_type;
527 	zfs_ace_t *aceptr = z_acl;
528 	ace_t *acep = datap;
529 	zfs_object_ace_t *zobjacep;
530 	ace_object_t *aceobjp;
531 
532 	for (i = 0; i != aclcnt; i++) {
533 		aceptr->z_hdr.z_access_mask = acep->a_access_mask;
534 		aceptr->z_hdr.z_flags = acep->a_flags;
535 		aceptr->z_hdr.z_type = acep->a_type;
536 		entry_type = aceptr->z_hdr.z_flags & ACE_TYPE_FLAGS;
537 		if (entry_type != ACE_OWNER && entry_type != OWNING_GROUP &&
538 		    entry_type != ACE_EVERYONE) {
539 			if (!aclp->z_has_fuids)
540 				aclp->z_has_fuids = IS_EPHEMERAL(acep->a_who);
541 			aceptr->z_fuid = (uint64_t)acep->a_who;
542 		}
543 
544 		/*
545 		 * Make sure ACE is valid
546 		 */
547 		if (zfs_ace_valid(obj_type, aclp, aceptr->z_hdr.z_type,
548 		    aceptr->z_hdr.z_flags) != B_TRUE)
549 			return (EINVAL);
550 
551 		switch (acep->a_type) {
552 		case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
553 		case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
554 		case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
555 		case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
556 			zobjacep = (zfs_object_ace_t *)aceptr;
557 			aceobjp = (ace_object_t *)acep;
558 
559 			bcopy(aceobjp->a_obj_type, zobjacep->z_object_type,
560 			    sizeof (aceobjp->a_obj_type));
561 			bcopy(aceobjp->a_inherit_obj_type,
562 			    zobjacep->z_inherit_type,
563 			    sizeof (aceobjp->a_inherit_obj_type));
564 			acep = (ace_t *)((caddr_t)acep + sizeof (ace_object_t));
565 			break;
566 		default:
567 			acep = (ace_t *)((caddr_t)acep + sizeof (ace_t));
568 		}
569 
570 		aceptr = (zfs_ace_t *)((caddr_t)aceptr +
571 		    aclp->z_ops.ace_size(aceptr));
572 	}
573 
574 	*size = (caddr_t)aceptr - (caddr_t)z_acl;
575 
576 	return (0);
577 }
578 
579 /*
580  * Copy ZFS ACEs to fixed size ace_t layout
581  */
582 static void
583 zfs_copy_fuid_2_ace(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, cred_t *cr,
584     void *datap, int filter)
585 {
586 	uint64_t who;
587 	uint32_t access_mask;
588 	uint16_t iflags, type;
589 	zfs_ace_hdr_t *zacep = NULL;
590 	ace_t *acep = datap;
591 	ace_object_t *objacep;
592 	zfs_object_ace_t *zobjacep;
593 	size_t ace_size;
594 	uint16_t entry_type;
595 
596 	while (zacep = zfs_acl_next_ace(aclp, zacep,
597 	    &who, &access_mask, &iflags, &type)) {
598 
599 		switch (type) {
600 		case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
601 		case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
602 		case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
603 		case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
604 			if (filter) {
605 				continue;
606 			}
607 			zobjacep = (zfs_object_ace_t *)zacep;
608 			objacep = (ace_object_t *)acep;
609 			bcopy(zobjacep->z_object_type,
610 			    objacep->a_obj_type,
611 			    sizeof (zobjacep->z_object_type));
612 			bcopy(zobjacep->z_inherit_type,
613 			    objacep->a_inherit_obj_type,
614 			    sizeof (zobjacep->z_inherit_type));
615 			ace_size = sizeof (ace_object_t);
616 			break;
617 		default:
618 			ace_size = sizeof (ace_t);
619 			break;
620 		}
621 
622 		entry_type = (iflags & ACE_TYPE_FLAGS);
623 		if ((entry_type != ACE_OWNER &&
624 		    entry_type != (ACE_GROUP | ACE_IDENTIFIER_GROUP) &&
625 		    entry_type != ACE_EVERYONE)) {
626 			acep->a_who = zfs_fuid_map_id(zfsvfs, who,
627 			    cr, (entry_type & ACE_IDENTIFIER_GROUP) ?
628 			    ZFS_ACE_GROUP : ZFS_ACE_USER);
629 		} else {
630 			acep->a_who = (uid_t)(int64_t)who;
631 		}
632 		acep->a_access_mask = access_mask;
633 		acep->a_flags = iflags;
634 		acep->a_type = type;
635 		acep = (ace_t *)((caddr_t)acep + ace_size);
636 	}
637 }
638 
639 static int
640 zfs_copy_ace_2_oldace(vtype_t obj_type, zfs_acl_t *aclp, ace_t *acep,
641     zfs_oldace_t *z_acl, int aclcnt, size_t *size)
642 {
643 	int i;
644 	zfs_oldace_t *aceptr = z_acl;
645 
646 	for (i = 0; i != aclcnt; i++, aceptr++) {
647 		aceptr->z_access_mask = acep[i].a_access_mask;
648 		aceptr->z_type = acep[i].a_type;
649 		aceptr->z_flags = acep[i].a_flags;
650 		aceptr->z_fuid = acep[i].a_who;
651 		/*
652 		 * Make sure ACE is valid
653 		 */
654 		if (zfs_ace_valid(obj_type, aclp, aceptr->z_type,
655 		    aceptr->z_flags) != B_TRUE)
656 			return (EINVAL);
657 	}
658 	*size = (caddr_t)aceptr - (caddr_t)z_acl;
659 	return (0);
660 }
661 
662 /*
663  * convert old ACL format to new
664  */
665 void
666 zfs_acl_xform(znode_t *zp, zfs_acl_t *aclp)
667 {
668 	zfs_oldace_t *oldaclp;
669 	int i;
670 	uint16_t type, iflags;
671 	uint32_t access_mask;
672 	uint64_t who;
673 	void *cookie = NULL;
674 	zfs_acl_node_t *newaclnode;
675 
676 	ASSERT(aclp->z_version == ZFS_ACL_VERSION_INITIAL);
677 	/*
678 	 * First create the ACE in a contiguous piece of memory
679 	 * for zfs_copy_ace_2_fuid().
680 	 *
681 	 * We only convert an ACL once, so this won't happen
682 	 * everytime.
683 	 */
684 	oldaclp = kmem_alloc(sizeof (zfs_oldace_t) * aclp->z_acl_count,
685 	    KM_SLEEP);
686 	i = 0;
687 	while (cookie = zfs_acl_next_ace(aclp, cookie, &who,
688 	    &access_mask, &iflags, &type)) {
689 		oldaclp[i].z_flags = iflags;
690 		oldaclp[i].z_type = type;
691 		oldaclp[i].z_fuid = who;
692 		oldaclp[i++].z_access_mask = access_mask;
693 	}
694 
695 	newaclnode = zfs_acl_node_alloc(aclp->z_acl_count *
696 	    sizeof (zfs_object_ace_t));
697 	aclp->z_ops = zfs_acl_fuid_ops;
698 	VERIFY(zfs_copy_ace_2_fuid(ZTOV(zp)->v_type, aclp, oldaclp,
699 	    newaclnode->z_acldata, aclp->z_acl_count,
700 	    &newaclnode->z_size) == 0);
701 	newaclnode->z_ace_count = aclp->z_acl_count;
702 	aclp->z_version = ZFS_ACL_VERSION;
703 	kmem_free(oldaclp, aclp->z_acl_count * sizeof (zfs_oldace_t));
704 
705 	/*
706 	 * Release all previous ACL nodes
707 	 */
708 
709 	zfs_acl_release_nodes(aclp);
710 
711 	list_insert_head(&aclp->z_acl, newaclnode);
712 
713 	aclp->z_acl_bytes = newaclnode->z_size;
714 	aclp->z_acl_count = newaclnode->z_ace_count;
715 
716 }
717 
718 /*
719  * Convert unix access mask to v4 access mask
720  */
721 static uint32_t
722 zfs_unix_to_v4(uint32_t access_mask)
723 {
724 	uint32_t new_mask = 0;
725 
726 	if (access_mask & S_IXOTH)
727 		new_mask |= ACE_EXECUTE;
728 	if (access_mask & S_IWOTH)
729 		new_mask |= ACE_WRITE_DATA;
730 	if (access_mask & S_IROTH)
731 		new_mask |= ACE_READ_DATA;
732 	return (new_mask);
733 }
734 
735 static void
736 zfs_set_ace(zfs_acl_t *aclp, void *acep, uint32_t access_mask,
737     uint16_t access_type, uint64_t fuid, uint16_t entry_type)
738 {
739 	uint16_t type = entry_type & ACE_TYPE_FLAGS;
740 
741 	aclp->z_ops.ace_mask_set(acep, access_mask);
742 	aclp->z_ops.ace_type_set(acep, access_type);
743 	aclp->z_ops.ace_flags_set(acep, entry_type);
744 	if ((type != ACE_OWNER && type != (ACE_GROUP | ACE_IDENTIFIER_GROUP) &&
745 	    type != ACE_EVERYONE))
746 		aclp->z_ops.ace_who_set(acep, fuid);
747 }
748 
749 /*
750  * Determine mode of file based on ACL.
751  * Also, create FUIDs for any User/Group ACEs
752  */
753 static uint64_t
754 zfs_mode_fuid_compute(znode_t *zp, zfs_acl_t *aclp, cred_t *cr,
755     zfs_fuid_info_t **fuidp, dmu_tx_t *tx)
756 {
757 	int		entry_type;
758 	mode_t		mode;
759 	mode_t		seen = 0;
760 	zfs_ace_hdr_t 	*acep = NULL;
761 	uint64_t	who;
762 	uint16_t	iflags, type;
763 	uint32_t	access_mask;
764 
765 	mode = (zp->z_phys->zp_mode & (S_IFMT | S_ISUID | S_ISGID | S_ISVTX));
766 
767 	while (acep = zfs_acl_next_ace(aclp, acep, &who,
768 	    &access_mask, &iflags, &type)) {
769 
770 		/*
771 		 * Skip over inherit only ACEs
772 		 */
773 		if (iflags & ACE_INHERIT_ONLY_ACE)
774 			continue;
775 
776 		entry_type = (iflags & ACE_TYPE_FLAGS);
777 
778 		if (entry_type == ACE_OWNER) {
779 			if ((access_mask & ACE_READ_DATA) &&
780 			    (!(seen & S_IRUSR))) {
781 				seen |= S_IRUSR;
782 				if (type == ALLOW) {
783 					mode |= S_IRUSR;
784 				}
785 			}
786 			if ((access_mask & ACE_WRITE_DATA) &&
787 			    (!(seen & S_IWUSR))) {
788 				seen |= S_IWUSR;
789 				if (type == ALLOW) {
790 					mode |= S_IWUSR;
791 				}
792 			}
793 			if ((access_mask & ACE_EXECUTE) &&
794 			    (!(seen & S_IXUSR))) {
795 				seen |= S_IXUSR;
796 				if (type == ALLOW) {
797 					mode |= S_IXUSR;
798 				}
799 			}
800 		} else if (entry_type == OWNING_GROUP) {
801 			if ((access_mask & ACE_READ_DATA) &&
802 			    (!(seen & S_IRGRP))) {
803 				seen |= S_IRGRP;
804 				if (type == ALLOW) {
805 					mode |= S_IRGRP;
806 				}
807 			}
808 			if ((access_mask & ACE_WRITE_DATA) &&
809 			    (!(seen & S_IWGRP))) {
810 				seen |= S_IWGRP;
811 				if (type == ALLOW) {
812 					mode |= S_IWGRP;
813 				}
814 			}
815 			if ((access_mask & ACE_EXECUTE) &&
816 			    (!(seen & S_IXGRP))) {
817 				seen |= S_IXGRP;
818 				if (type == ALLOW) {
819 					mode |= S_IXGRP;
820 				}
821 			}
822 		} else if (entry_type == ACE_EVERYONE) {
823 			if ((access_mask & ACE_READ_DATA)) {
824 				if (!(seen & S_IRUSR)) {
825 					seen |= S_IRUSR;
826 					if (type == ALLOW) {
827 						mode |= S_IRUSR;
828 					}
829 				}
830 				if (!(seen & S_IRGRP)) {
831 					seen |= S_IRGRP;
832 					if (type == ALLOW) {
833 						mode |= S_IRGRP;
834 					}
835 				}
836 				if (!(seen & S_IROTH)) {
837 					seen |= S_IROTH;
838 					if (type == ALLOW) {
839 						mode |= S_IROTH;
840 					}
841 				}
842 			}
843 			if ((access_mask & ACE_WRITE_DATA)) {
844 				if (!(seen & S_IWUSR)) {
845 					seen |= S_IWUSR;
846 					if (type == ALLOW) {
847 						mode |= S_IWUSR;
848 					}
849 				}
850 				if (!(seen & S_IWGRP)) {
851 					seen |= S_IWGRP;
852 					if (type == ALLOW) {
853 						mode |= S_IWGRP;
854 					}
855 				}
856 				if (!(seen & S_IWOTH)) {
857 					seen |= S_IWOTH;
858 					if (type == ALLOW) {
859 						mode |= S_IWOTH;
860 					}
861 				}
862 			}
863 			if ((access_mask & ACE_EXECUTE)) {
864 				if (!(seen & S_IXUSR)) {
865 					seen |= S_IXUSR;
866 					if (type == ALLOW) {
867 						mode |= S_IXUSR;
868 					}
869 				}
870 				if (!(seen & S_IXGRP)) {
871 					seen |= S_IXGRP;
872 					if (type == ALLOW) {
873 						mode |= S_IXGRP;
874 					}
875 				}
876 				if (!(seen & S_IXOTH)) {
877 					seen |= S_IXOTH;
878 					if (type == ALLOW) {
879 						mode |= S_IXOTH;
880 					}
881 				}
882 			}
883 		}
884 		/*
885 		 * Now handle FUID create for user/group ACEs
886 		 */
887 		if (entry_type == 0 || entry_type == ACE_IDENTIFIER_GROUP) {
888 			aclp->z_ops.ace_who_set(acep,
889 			    zfs_fuid_create(zp->z_zfsvfs, who, cr,
890 			    (entry_type == 0) ? ZFS_ACE_USER : ZFS_ACE_GROUP,
891 			    tx, fuidp));
892 		}
893 	}
894 	return (mode);
895 }
896 
897 static zfs_acl_t *
898 zfs_acl_node_read_internal(znode_t *zp, boolean_t will_modify)
899 {
900 	zfs_acl_t	*aclp;
901 	zfs_acl_node_t	*aclnode;
902 
903 	aclp = zfs_acl_alloc(zp->z_phys->zp_acl.z_acl_version);
904 
905 	/*
906 	 * Version 0 to 1 znode_acl_phys has the size/count fields swapped.
907 	 * Version 0 didn't have a size field, only a count.
908 	 */
909 	if (zp->z_phys->zp_acl.z_acl_version == ZFS_ACL_VERSION_INITIAL) {
910 		aclp->z_acl_count = zp->z_phys->zp_acl.z_acl_size;
911 		aclp->z_acl_bytes = ZFS_ACL_SIZE(aclp->z_acl_count);
912 	} else {
913 		aclp->z_acl_count = zp->z_phys->zp_acl.z_acl_count;
914 		aclp->z_acl_bytes = zp->z_phys->zp_acl.z_acl_size;
915 	}
916 
917 	aclnode = zfs_acl_node_alloc(will_modify ? aclp->z_acl_bytes : 0);
918 	aclnode->z_ace_count = aclp->z_acl_count;
919 	if (will_modify) {
920 		bcopy(zp->z_phys->zp_acl.z_ace_data, aclnode->z_acldata,
921 		    aclp->z_acl_bytes);
922 	} else {
923 		aclnode->z_size = aclp->z_acl_bytes;
924 		aclnode->z_acldata = &zp->z_phys->zp_acl.z_ace_data[0];
925 	}
926 
927 	list_insert_head(&aclp->z_acl, aclnode);
928 
929 	return (aclp);
930 }
931 
932 /*
933  * Read an external acl object.
934  */
935 static int
936 zfs_acl_node_read(znode_t *zp, zfs_acl_t **aclpp, boolean_t will_modify)
937 {
938 	uint64_t extacl = zp->z_phys->zp_acl.z_acl_extern_obj;
939 	zfs_acl_t	*aclp;
940 	size_t		aclsize;
941 	size_t		acl_count;
942 	zfs_acl_node_t	*aclnode;
943 	int error;
944 
945 	ASSERT(MUTEX_HELD(&zp->z_acl_lock));
946 
947 	if (zp->z_phys->zp_acl.z_acl_extern_obj == 0) {
948 		*aclpp = zfs_acl_node_read_internal(zp, will_modify);
949 		return (0);
950 	}
951 
952 	aclp = zfs_acl_alloc(zp->z_phys->zp_acl.z_acl_version);
953 	if (zp->z_phys->zp_acl.z_acl_version == ZFS_ACL_VERSION_INITIAL) {
954 		zfs_acl_phys_v0_t *zacl0 =
955 		    (zfs_acl_phys_v0_t *)&zp->z_phys->zp_acl;
956 
957 		aclsize = ZFS_ACL_SIZE(zacl0->z_acl_count);
958 		acl_count = zacl0->z_acl_count;
959 	} else {
960 		aclsize = zp->z_phys->zp_acl.z_acl_size;
961 		acl_count = zp->z_phys->zp_acl.z_acl_count;
962 		if (aclsize == 0)
963 			aclsize = acl_count * sizeof (zfs_ace_t);
964 	}
965 	aclnode = zfs_acl_node_alloc(aclsize);
966 	list_insert_head(&aclp->z_acl, aclnode);
967 	error = dmu_read(zp->z_zfsvfs->z_os, extacl, 0,
968 	    aclsize, aclnode->z_acldata);
969 	aclnode->z_ace_count = acl_count;
970 	aclp->z_acl_count = acl_count;
971 	aclp->z_acl_bytes = aclsize;
972 
973 	if (error != 0) {
974 		zfs_acl_free(aclp);
975 		/* convert checksum errors into IO errors */
976 		if (error == ECKSUM)
977 			error = EIO;
978 		return (error);
979 	}
980 
981 	*aclpp = aclp;
982 	return (0);
983 }
984 
985 /*
986  * common code for setting ACLs.
987  *
988  * This function is called from zfs_mode_update, zfs_perm_init, and zfs_setacl.
989  * zfs_setacl passes a non-NULL inherit pointer (ihp) to indicate that it's
990  * already checked the acl and knows whether to inherit.
991  */
992 int
993 zfs_aclset_common(znode_t *zp, zfs_acl_t *aclp, cred_t *cr,
994     zfs_fuid_info_t **fuidp, dmu_tx_t *tx)
995 {
996 	int		error;
997 	znode_phys_t	*zphys = zp->z_phys;
998 	zfs_acl_phys_t	*zacl = &zphys->zp_acl;
999 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1000 	uint64_t	aoid = zphys->zp_acl.z_acl_extern_obj;
1001 	uint64_t	off = 0;
1002 	dmu_object_type_t otype;
1003 	zfs_acl_node_t	*aclnode;
1004 
1005 	ASSERT(MUTEX_HELD(&zp->z_lock));
1006 	ASSERT(MUTEX_HELD(&zp->z_acl_lock));
1007 
1008 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1009 
1010 	zphys->zp_mode = zfs_mode_fuid_compute(zp, aclp, cr, fuidp, tx);
1011 
1012 	/*
1013 	 * Decide which opbject type to use.  If we are forced to
1014 	 * use old ACL format than transform ACL into zfs_oldace_t
1015 	 * layout.
1016 	 */
1017 	if (!zfsvfs->z_use_fuids) {
1018 		otype = DMU_OT_OLDACL;
1019 	} else {
1020 		if ((aclp->z_version == ZFS_ACL_VERSION_INITIAL) &&
1021 		    (zfsvfs->z_version >= ZPL_VERSION_FUID))
1022 			zfs_acl_xform(zp, aclp);
1023 		ASSERT(aclp->z_version >= ZFS_ACL_VERSION_FUID);
1024 		otype = DMU_OT_ACL;
1025 	}
1026 
1027 	if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1028 		/*
1029 		 * If ACL was previously external and we are now
1030 		 * converting to new ACL format then release old
1031 		 * ACL object and create a new one.
1032 		 */
1033 		if (aoid && aclp->z_version != zacl->z_acl_version) {
1034 			error = dmu_object_free(zfsvfs->z_os,
1035 			    zp->z_phys->zp_acl.z_acl_extern_obj, tx);
1036 			if (error)
1037 				return (error);
1038 			aoid = 0;
1039 		}
1040 		if (aoid == 0) {
1041 			aoid = dmu_object_alloc(zfsvfs->z_os,
1042 			    otype, aclp->z_acl_bytes,
1043 			    otype == DMU_OT_ACL ? DMU_OT_SYSACL : DMU_OT_NONE,
1044 			    otype == DMU_OT_ACL ? DN_MAX_BONUSLEN : 0, tx);
1045 		} else {
1046 			(void) dmu_object_set_blocksize(zfsvfs->z_os, aoid,
1047 			    aclp->z_acl_bytes, 0, tx);
1048 		}
1049 		zphys->zp_acl.z_acl_extern_obj = aoid;
1050 		for (aclnode = list_head(&aclp->z_acl); aclnode;
1051 		    aclnode = list_next(&aclp->z_acl, aclnode)) {
1052 			if (aclnode->z_ace_count == 0)
1053 				continue;
1054 			dmu_write(zfsvfs->z_os, aoid, off,
1055 			    aclnode->z_size, aclnode->z_acldata, tx);
1056 			off += aclnode->z_size;
1057 		}
1058 	} else {
1059 		void *start = zacl->z_ace_data;
1060 		/*
1061 		 * Migrating back embedded?
1062 		 */
1063 		if (zphys->zp_acl.z_acl_extern_obj) {
1064 			error = dmu_object_free(zfsvfs->z_os,
1065 			    zp->z_phys->zp_acl.z_acl_extern_obj, tx);
1066 			if (error)
1067 				return (error);
1068 			zphys->zp_acl.z_acl_extern_obj = 0;
1069 		}
1070 
1071 		for (aclnode = list_head(&aclp->z_acl); aclnode;
1072 		    aclnode = list_next(&aclp->z_acl, aclnode)) {
1073 			if (aclnode->z_ace_count == 0)
1074 				continue;
1075 			bcopy(aclnode->z_acldata, start, aclnode->z_size);
1076 			start = (caddr_t)start + aclnode->z_size;
1077 		}
1078 	}
1079 
1080 	/*
1081 	 * If Old version then swap count/bytes to match old
1082 	 * layout of znode_acl_phys_t.
1083 	 */
1084 	if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) {
1085 		zphys->zp_acl.z_acl_size = aclp->z_acl_count;
1086 		zphys->zp_acl.z_acl_count = aclp->z_acl_bytes;
1087 	} else {
1088 		zphys->zp_acl.z_acl_size = aclp->z_acl_bytes;
1089 		zphys->zp_acl.z_acl_count = aclp->z_acl_count;
1090 	}
1091 
1092 	zphys->zp_acl.z_acl_version = aclp->z_version;
1093 
1094 	/*
1095 	 * Replace ACL wide bits, but first clear them.
1096 	 */
1097 	zp->z_phys->zp_flags &= ~ZFS_ACL_WIDE_FLAGS;
1098 
1099 	zp->z_phys->zp_flags |= aclp->z_hints;
1100 
1101 	if (ace_trivial_common(aclp, 0, zfs_ace_walk) == 0)
1102 		zp->z_phys->zp_flags |= ZFS_ACL_TRIVIAL;
1103 
1104 	zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
1105 	return (0);
1106 }
1107 
1108 /*
1109  * Update access mask for prepended ACE
1110  *
1111  * This applies the "groupmask" value for aclmode property.
1112  */
1113 static void
1114 zfs_acl_prepend_fixup(zfs_acl_t *aclp, void  *acep, void  *origacep,
1115     mode_t mode, uint64_t owner)
1116 {
1117 	int	rmask, wmask, xmask;
1118 	int	user_ace;
1119 	uint16_t aceflags;
1120 	uint32_t origmask, acepmask;
1121 	uint64_t fuid;
1122 
1123 	aceflags = aclp->z_ops.ace_flags_get(acep);
1124 	fuid = aclp->z_ops.ace_who_get(acep);
1125 	origmask = aclp->z_ops.ace_mask_get(origacep);
1126 	acepmask = aclp->z_ops.ace_mask_get(acep);
1127 
1128 	user_ace = (!(aceflags &
1129 	    (ACE_OWNER|ACE_GROUP|ACE_IDENTIFIER_GROUP)));
1130 
1131 	if (user_ace && (fuid == owner)) {
1132 		rmask = S_IRUSR;
1133 		wmask = S_IWUSR;
1134 		xmask = S_IXUSR;
1135 	} else {
1136 		rmask = S_IRGRP;
1137 		wmask = S_IWGRP;
1138 		xmask = S_IXGRP;
1139 	}
1140 
1141 	if (origmask & ACE_READ_DATA) {
1142 		if (mode & rmask) {
1143 			acepmask &= ~ACE_READ_DATA;
1144 		} else {
1145 			acepmask |= ACE_READ_DATA;
1146 		}
1147 	}
1148 
1149 	if (origmask & ACE_WRITE_DATA) {
1150 		if (mode & wmask) {
1151 			acepmask &= ~ACE_WRITE_DATA;
1152 		} else {
1153 			acepmask |= ACE_WRITE_DATA;
1154 		}
1155 	}
1156 
1157 	if (origmask & ACE_APPEND_DATA) {
1158 		if (mode & wmask) {
1159 			acepmask &= ~ACE_APPEND_DATA;
1160 		} else {
1161 			acepmask |= ACE_APPEND_DATA;
1162 		}
1163 	}
1164 
1165 	if (origmask & ACE_EXECUTE) {
1166 		if (mode & xmask) {
1167 			acepmask &= ~ACE_EXECUTE;
1168 		} else {
1169 			acepmask |= ACE_EXECUTE;
1170 		}
1171 	}
1172 	aclp->z_ops.ace_mask_set(acep, acepmask);
1173 }
1174 
1175 /*
1176  * Apply mode to canonical six ACEs.
1177  */
1178 static void
1179 zfs_acl_fixup_canonical_six(zfs_acl_t *aclp, mode_t mode)
1180 {
1181 	zfs_acl_node_t *aclnode = list_tail(&aclp->z_acl);
1182 	void	*acep;
1183 	int	maskoff = aclp->z_ops.ace_mask_off();
1184 	size_t abstract_size = aclp->z_ops.ace_abstract_size();
1185 
1186 	ASSERT(aclnode != NULL);
1187 
1188 	acep = (void *)((caddr_t)aclnode->z_acldata +
1189 	    aclnode->z_size - (abstract_size * 6));
1190 
1191 	/*
1192 	 * Fixup final ACEs to match the mode
1193 	 */
1194 
1195 	adjust_ace_pair_common(acep, maskoff, abstract_size,
1196 	    (mode & 0700) >> 6);	/* owner@ */
1197 
1198 	acep = (caddr_t)acep + (abstract_size * 2);
1199 
1200 	adjust_ace_pair_common(acep, maskoff, abstract_size,
1201 	    (mode & 0070) >> 3);	/* group@ */
1202 
1203 	acep = (caddr_t)acep + (abstract_size * 2);
1204 	adjust_ace_pair_common(acep, maskoff,
1205 	    abstract_size, mode);	/* everyone@ */
1206 }
1207 
1208 
1209 static int
1210 zfs_acl_ace_match(zfs_acl_t *aclp, void *acep, int allow_deny,
1211     int entry_type, int accessmask)
1212 {
1213 	uint32_t mask = aclp->z_ops.ace_mask_get(acep);
1214 	uint16_t type = aclp->z_ops.ace_type_get(acep);
1215 	uint16_t flags = aclp->z_ops.ace_flags_get(acep);
1216 
1217 	return (mask == accessmask && type == allow_deny &&
1218 	    ((flags & ACE_TYPE_FLAGS) == entry_type));
1219 }
1220 
1221 /*
1222  * Can prepended ACE be reused?
1223  */
1224 static int
1225 zfs_reuse_deny(zfs_acl_t *aclp, void *acep, void *prevacep)
1226 {
1227 	int okay_masks;
1228 	uint16_t prevtype;
1229 	uint16_t prevflags;
1230 	uint16_t flags;
1231 	uint32_t mask, prevmask;
1232 
1233 	if (prevacep == NULL)
1234 		return (B_FALSE);
1235 
1236 	prevtype = aclp->z_ops.ace_type_get(prevacep);
1237 	prevflags = aclp->z_ops.ace_flags_get(prevacep);
1238 	flags = aclp->z_ops.ace_flags_get(acep);
1239 	mask = aclp->z_ops.ace_mask_get(acep);
1240 	prevmask = aclp->z_ops.ace_mask_get(prevacep);
1241 
1242 	if (prevtype != DENY)
1243 		return (B_FALSE);
1244 
1245 	if (prevflags != (flags & ACE_IDENTIFIER_GROUP))
1246 		return (B_FALSE);
1247 
1248 	okay_masks = (mask & OKAY_MASK_BITS);
1249 
1250 	if (prevmask & ~okay_masks)
1251 		return (B_FALSE);
1252 
1253 	return (B_TRUE);
1254 }
1255 
1256 
1257 /*
1258  * Insert new ACL node into chain of zfs_acl_node_t's
1259  *
1260  * This will result in two possible results.
1261  * 1. If the ACL is currently just a single zfs_acl_node and
1262  *    we are prepending the entry then current acl node will have
1263  *    a new node inserted above it.
1264  *
1265  * 2. If we are inserting in the middle of current acl node then
1266  *    the current node will be split in two and new node will be inserted
1267  *    in between the two split nodes.
1268  */
1269 static zfs_acl_node_t *
1270 zfs_acl_ace_insert(zfs_acl_t *aclp, void  *acep)
1271 {
1272 	zfs_acl_node_t 	*newnode;
1273 	zfs_acl_node_t 	*trailernode = NULL;
1274 	zfs_acl_node_t 	*currnode = zfs_acl_curr_node(aclp);
1275 	int		curr_idx = aclp->z_curr_node->z_ace_idx;
1276 	int		trailer_count;
1277 	size_t		oldsize;
1278 
1279 	newnode = zfs_acl_node_alloc(aclp->z_ops.ace_size(acep));
1280 	newnode->z_ace_count = 1;
1281 
1282 	oldsize = currnode->z_size;
1283 
1284 	if (curr_idx != 1) {
1285 		trailernode = zfs_acl_node_alloc(0);
1286 		trailernode->z_acldata = acep;
1287 
1288 		trailer_count = currnode->z_ace_count - curr_idx + 1;
1289 		currnode->z_ace_count = curr_idx - 1;
1290 		currnode->z_size = (caddr_t)acep - (caddr_t)currnode->z_acldata;
1291 		trailernode->z_size = oldsize - currnode->z_size;
1292 		trailernode->z_ace_count = trailer_count;
1293 	}
1294 
1295 	aclp->z_acl_count += 1;
1296 	aclp->z_acl_bytes += aclp->z_ops.ace_size(acep);
1297 
1298 	if (curr_idx == 1)
1299 		list_insert_before(&aclp->z_acl, currnode, newnode);
1300 	else
1301 		list_insert_after(&aclp->z_acl, currnode, newnode);
1302 	if (trailernode) {
1303 		list_insert_after(&aclp->z_acl, newnode, trailernode);
1304 		aclp->z_curr_node = trailernode;
1305 		trailernode->z_ace_idx = 1;
1306 	}
1307 
1308 	return (newnode);
1309 }
1310 
1311 /*
1312  * Prepend deny ACE
1313  */
1314 static void *
1315 zfs_acl_prepend_deny(znode_t *zp, zfs_acl_t *aclp, void *acep,
1316     mode_t mode)
1317 {
1318 	zfs_acl_node_t *aclnode;
1319 	void  *newacep;
1320 	uint64_t fuid;
1321 	uint16_t flags;
1322 
1323 	aclnode = zfs_acl_ace_insert(aclp, acep);
1324 	newacep = aclnode->z_acldata;
1325 	fuid = aclp->z_ops.ace_who_get(acep);
1326 	flags = aclp->z_ops.ace_flags_get(acep);
1327 	zfs_set_ace(aclp, newacep, 0, DENY, fuid, (flags & ACE_TYPE_FLAGS));
1328 	zfs_acl_prepend_fixup(aclp, newacep, acep, mode, zp->z_phys->zp_uid);
1329 
1330 	return (newacep);
1331 }
1332 
1333 /*
1334  * Split an inherited ACE into inherit_only ACE
1335  * and original ACE with inheritance flags stripped off.
1336  */
1337 static void
1338 zfs_acl_split_ace(zfs_acl_t *aclp, zfs_ace_hdr_t *acep)
1339 {
1340 	zfs_acl_node_t *aclnode;
1341 	zfs_acl_node_t *currnode;
1342 	void  *newacep;
1343 	uint16_t type, flags;
1344 	uint32_t mask;
1345 	uint64_t fuid;
1346 
1347 	type = aclp->z_ops.ace_type_get(acep);
1348 	flags = aclp->z_ops.ace_flags_get(acep);
1349 	mask = aclp->z_ops.ace_mask_get(acep);
1350 	fuid = aclp->z_ops.ace_who_get(acep);
1351 
1352 	aclnode = zfs_acl_ace_insert(aclp, acep);
1353 	newacep = aclnode->z_acldata;
1354 
1355 	aclp->z_ops.ace_type_set(newacep, type);
1356 	aclp->z_ops.ace_flags_set(newacep, flags | ACE_INHERIT_ONLY_ACE);
1357 	aclp->z_ops.ace_mask_set(newacep, mask);
1358 	aclp->z_ops.ace_type_set(newacep, type);
1359 	aclp->z_ops.ace_who_set(newacep, fuid);
1360 	aclp->z_next_ace = acep;
1361 	flags &= ~ALL_INHERIT;
1362 	aclp->z_ops.ace_flags_set(acep, flags);
1363 	currnode = zfs_acl_curr_node(aclp);
1364 	ASSERT(currnode->z_ace_idx >= 1);
1365 	currnode->z_ace_idx -= 1;
1366 }
1367 
1368 /*
1369  * Are ACES started at index i, the canonical six ACES?
1370  */
1371 static int
1372 zfs_have_canonical_six(zfs_acl_t *aclp)
1373 {
1374 	void *acep;
1375 	zfs_acl_node_t *aclnode = list_tail(&aclp->z_acl);
1376 	int		i = 0;
1377 	size_t abstract_size = aclp->z_ops.ace_abstract_size();
1378 
1379 	ASSERT(aclnode != NULL);
1380 
1381 	if (aclnode->z_ace_count < 6)
1382 		return (0);
1383 
1384 	acep = (void *)((caddr_t)aclnode->z_acldata +
1385 	    aclnode->z_size - (aclp->z_ops.ace_abstract_size() * 6));
1386 
1387 	if ((zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++),
1388 	    DENY, ACE_OWNER, 0) &&
1389 	    zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++),
1390 	    ALLOW, ACE_OWNER, OWNER_ALLOW_MASK) &&
1391 	    zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++), DENY,
1392 	    OWNING_GROUP, 0) && zfs_acl_ace_match(aclp, (caddr_t)acep +
1393 	    (abstract_size * i++),
1394 	    ALLOW, OWNING_GROUP, 0) &&
1395 	    zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++),
1396 	    DENY, ACE_EVERYONE, EVERYONE_DENY_MASK) &&
1397 	    zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++),
1398 	    ALLOW, ACE_EVERYONE, EVERYONE_ALLOW_MASK))) {
1399 		return (1);
1400 	} else {
1401 		return (0);
1402 	}
1403 }
1404 
1405 
1406 /*
1407  * Apply step 1g, to group entries
1408  *
1409  * Need to deal with corner case where group may have
1410  * greater permissions than owner.  If so then limit
1411  * group permissions, based on what extra permissions
1412  * group has.
1413  */
1414 static void
1415 zfs_fixup_group_entries(zfs_acl_t *aclp, void *acep, void *prevacep,
1416     mode_t mode)
1417 {
1418 	uint32_t prevmask = aclp->z_ops.ace_mask_get(prevacep);
1419 	uint32_t mask = aclp->z_ops.ace_mask_get(acep);
1420 	uint16_t prevflags = aclp->z_ops.ace_flags_get(prevacep);
1421 	mode_t extramode = (mode >> 3) & 07;
1422 	mode_t ownermode = (mode >> 6);
1423 
1424 	if (prevflags & ACE_IDENTIFIER_GROUP) {
1425 
1426 		extramode &= ~ownermode;
1427 
1428 		if (extramode) {
1429 			if (extramode & S_IROTH) {
1430 				prevmask &= ~ACE_READ_DATA;
1431 				mask &= ~ACE_READ_DATA;
1432 			}
1433 			if (extramode & S_IWOTH) {
1434 				prevmask &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
1435 				mask &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
1436 			}
1437 			if (extramode & S_IXOTH) {
1438 				prevmask  &= ~ACE_EXECUTE;
1439 				mask &= ~ACE_EXECUTE;
1440 			}
1441 		}
1442 	}
1443 	aclp->z_ops.ace_mask_set(acep, mask);
1444 	aclp->z_ops.ace_mask_set(prevacep, prevmask);
1445 }
1446 
1447 /*
1448  * Apply the chmod algorithm as described
1449  * in PSARC/2002/240
1450  */
1451 static void
1452 zfs_acl_chmod(znode_t *zp, uint64_t mode, zfs_acl_t *aclp)
1453 {
1454 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1455 	void		*acep = NULL, *prevacep = NULL;
1456 	uint64_t	who;
1457 	int 		i;
1458 	int 		entry_type;
1459 	int 		reuse_deny;
1460 	int 		need_canonical_six = 1;
1461 	uint16_t	iflags, type;
1462 	uint32_t	access_mask;
1463 
1464 	ASSERT(MUTEX_HELD(&zp->z_acl_lock));
1465 	ASSERT(MUTEX_HELD(&zp->z_lock));
1466 
1467 	aclp->z_hints = (zp->z_phys->zp_flags & V4_ACL_WIDE_FLAGS);
1468 
1469 	/*
1470 	 * If discard then just discard all ACL nodes which
1471 	 * represent the ACEs.
1472 	 *
1473 	 * New owner@/group@/everone@ ACEs will be added
1474 	 * later.
1475 	 */
1476 	if (zfsvfs->z_acl_mode == ZFS_ACL_DISCARD)
1477 		zfs_acl_release_nodes(aclp);
1478 
1479 	while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask,
1480 	    &iflags, &type)) {
1481 
1482 		entry_type = (iflags & ACE_TYPE_FLAGS);
1483 		iflags = (iflags & ALL_INHERIT);
1484 
1485 		if ((type != ALLOW && type != DENY) ||
1486 		    (iflags & ACE_INHERIT_ONLY_ACE)) {
1487 			if (iflags)
1488 				aclp->z_hints |= ZFS_INHERIT_ACE;
1489 			switch (type) {
1490 			case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1491 			case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1492 			case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1493 			case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1494 				aclp->z_hints |= ZFS_ACL_OBJ_ACE;
1495 				break;
1496 			}
1497 			goto nextace;
1498 		}
1499 
1500 		/*
1501 		 * Need to split ace into two?
1502 		 */
1503 		if ((iflags & (ACE_FILE_INHERIT_ACE|
1504 		    ACE_DIRECTORY_INHERIT_ACE)) &&
1505 		    (!(iflags & ACE_INHERIT_ONLY_ACE))) {
1506 			zfs_acl_split_ace(aclp, acep);
1507 			aclp->z_hints |= ZFS_INHERIT_ACE;
1508 			goto nextace;
1509 		}
1510 
1511 		if (entry_type == ACE_OWNER || entry_type == ACE_EVERYONE ||
1512 		    (entry_type == OWNING_GROUP)) {
1513 			access_mask &= ~OGE_CLEAR;
1514 			aclp->z_ops.ace_mask_set(acep, access_mask);
1515 			goto nextace;
1516 		} else {
1517 			reuse_deny = B_TRUE;
1518 			if (type == ALLOW) {
1519 
1520 				/*
1521 				 * Check preceding ACE if any, to see
1522 				 * if we need to prepend a DENY ACE.
1523 				 * This is only applicable when the acl_mode
1524 				 * property == groupmask.
1525 				 */
1526 				if (zfsvfs->z_acl_mode == ZFS_ACL_GROUPMASK) {
1527 
1528 					reuse_deny = zfs_reuse_deny(aclp, acep,
1529 					    prevacep);
1530 
1531 					if (!reuse_deny) {
1532 						prevacep =
1533 						    zfs_acl_prepend_deny(zp,
1534 						    aclp, acep, mode);
1535 					} else {
1536 						zfs_acl_prepend_fixup(
1537 						    aclp, prevacep,
1538 						    acep, mode,
1539 						    zp->z_phys->zp_uid);
1540 					}
1541 					zfs_fixup_group_entries(aclp, acep,
1542 					    prevacep, mode);
1543 
1544 				}
1545 			}
1546 		}
1547 nextace:
1548 		prevacep = acep;
1549 	}
1550 
1551 	/*
1552 	 * Check out last six aces, if we have six.
1553 	 */
1554 
1555 	if (aclp->z_acl_count >= 6) {
1556 		if (zfs_have_canonical_six(aclp)) {
1557 			need_canonical_six = 0;
1558 		}
1559 	}
1560 
1561 	if (need_canonical_six) {
1562 		size_t abstract_size = aclp->z_ops.ace_abstract_size();
1563 		void *zacep;
1564 		zfs_acl_node_t *aclnode =
1565 		    zfs_acl_node_alloc(abstract_size * 6);
1566 
1567 		aclnode->z_size = abstract_size * 6;
1568 		aclnode->z_ace_count = 6;
1569 		aclp->z_acl_bytes += aclnode->z_size;
1570 		list_insert_tail(&aclp->z_acl, aclnode);
1571 
1572 		zacep = aclnode->z_acldata;
1573 
1574 		i = 0;
1575 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++),
1576 		    0, DENY, -1, ACE_OWNER);
1577 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++),
1578 		    OWNER_ALLOW_MASK, ALLOW, -1, ACE_OWNER);
1579 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++), 0,
1580 		    DENY, -1, OWNING_GROUP);
1581 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++), 0,
1582 		    ALLOW, -1, OWNING_GROUP);
1583 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++),
1584 		    EVERYONE_DENY_MASK, DENY, -1, ACE_EVERYONE);
1585 		zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++),
1586 		    EVERYONE_ALLOW_MASK, ALLOW, -1, ACE_EVERYONE);
1587 		aclp->z_acl_count += 6;
1588 	}
1589 
1590 	zfs_acl_fixup_canonical_six(aclp, mode);
1591 }
1592 
1593 int
1594 zfs_acl_chmod_setattr(znode_t *zp, zfs_acl_t **aclp, uint64_t mode)
1595 {
1596 	int error;
1597 
1598 	mutex_enter(&zp->z_lock);
1599 	mutex_enter(&zp->z_acl_lock);
1600 	*aclp = NULL;
1601 	error = zfs_acl_node_read(zp, aclp, B_TRUE);
1602 	if (error == 0)
1603 		zfs_acl_chmod(zp, mode, *aclp);
1604 	mutex_exit(&zp->z_acl_lock);
1605 	mutex_exit(&zp->z_lock);
1606 	return (error);
1607 }
1608 
1609 /*
1610  * strip off write_owner and write_acl
1611  */
1612 static void
1613 zfs_restricted_update(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, void *acep)
1614 {
1615 	uint32_t mask = aclp->z_ops.ace_mask_get(acep);
1616 
1617 	if ((zfsvfs->z_acl_inherit == ZFS_ACL_RESTRICTED) &&
1618 	    (aclp->z_ops.ace_type_get(acep) == ALLOW)) {
1619 		mask &= ~RESTRICTED_CLEAR;
1620 		aclp->z_ops.ace_mask_set(acep, mask);
1621 	}
1622 }
1623 
1624 /*
1625  * Should ACE be inherited?
1626  */
1627 static int
1628 zfs_ace_can_use(znode_t *zp, uint16_t acep_flags)
1629 {
1630 	int vtype = ZTOV(zp)->v_type;
1631 	int	iflags = (acep_flags & 0xf);
1632 
1633 	if ((vtype == VDIR) && (iflags & ACE_DIRECTORY_INHERIT_ACE))
1634 		return (1);
1635 	else if (iflags & ACE_FILE_INHERIT_ACE)
1636 		return (!((vtype == VDIR) &&
1637 		    (iflags & ACE_NO_PROPAGATE_INHERIT_ACE)));
1638 	return (0);
1639 }
1640 
1641 /*
1642  * inherit inheritable ACEs from parent
1643  */
1644 static zfs_acl_t *
1645 zfs_acl_inherit(znode_t *zp, zfs_acl_t *paclp, boolean_t *need_chmod)
1646 {
1647 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1648 	void		*pacep;
1649 	void		*acep, *acep2;
1650 	zfs_acl_node_t  *aclnode, *aclnode2;
1651 	zfs_acl_t	*aclp = NULL;
1652 	uint64_t	who;
1653 	uint32_t	access_mask;
1654 	uint16_t	iflags, newflags, type;
1655 	size_t		ace_size;
1656 	void		*data1, *data2;
1657 	size_t		data1sz, data2sz;
1658 	enum vtype	vntype = ZTOV(zp)->v_type;
1659 
1660 	*need_chmod = B_TRUE;
1661 	pacep = NULL;
1662 	aclp = zfs_acl_alloc(zfs_acl_version_zp(zp));
1663 	if (zfsvfs->z_acl_inherit != ZFS_ACL_DISCARD) {
1664 		while (pacep = zfs_acl_next_ace(paclp, pacep, &who,
1665 		    &access_mask, &iflags, &type)) {
1666 
1667 			if (zfsvfs->z_acl_inherit == ZFS_ACL_NOALLOW &&
1668 			    type == ALLOW)
1669 				continue;
1670 
1671 			ace_size = aclp->z_ops.ace_size(pacep);
1672 
1673 			if (!zfs_ace_can_use(zp, iflags))
1674 				continue;
1675 
1676 			/*
1677 			 * If owner@, group@, or everyone@ inheritable
1678 			 * then zfs_acl_chmod() isn't needed.
1679 			 */
1680 			if (zfsvfs->z_acl_inherit ==
1681 			    ZFS_ACL_PASSTHROUGH &&
1682 			    ((iflags & (ACE_OWNER|ACE_EVERYONE)) ||
1683 			    ((iflags & OWNING_GROUP) ==
1684 			    OWNING_GROUP)) && (vntype == VREG ||
1685 			    (vntype == VDIR &&
1686 			    (iflags & ACE_DIRECTORY_INHERIT_ACE))))
1687 				*need_chmod = B_FALSE;
1688 
1689 			aclnode = zfs_acl_node_alloc(ace_size);
1690 			list_insert_tail(&aclp->z_acl, aclnode);
1691 			acep = aclnode->z_acldata;
1692 			zfs_set_ace(aclp, acep, access_mask, type,
1693 			    who, iflags|ACE_INHERITED_ACE);
1694 
1695 			/*
1696 			 * Copy special opaque data if any
1697 			 */
1698 			if ((data1sz = paclp->z_ops.ace_data(pacep,
1699 			    &data1)) != 0) {
1700 				VERIFY((data2sz = aclp->z_ops.ace_data(acep,
1701 				    &data2)) == data1sz);
1702 				bcopy(data1, data2, data2sz);
1703 			}
1704 			aclp->z_acl_count++;
1705 			aclnode->z_ace_count++;
1706 			aclp->z_acl_bytes += aclnode->z_size;
1707 			newflags = aclp->z_ops.ace_flags_get(acep);
1708 
1709 			if (vntype == VDIR)
1710 				aclp->z_hints |= ZFS_INHERIT_ACE;
1711 
1712 			if ((iflags & ACE_NO_PROPAGATE_INHERIT_ACE) ||
1713 			    (vntype != VDIR)) {
1714 				newflags &= ~ALL_INHERIT;
1715 				aclp->z_ops.ace_flags_set(acep,
1716 				    newflags|ACE_INHERITED_ACE);
1717 				zfs_restricted_update(zfsvfs, aclp, acep);
1718 				continue;
1719 			}
1720 
1721 			ASSERT(vntype == VDIR);
1722 
1723 			newflags = aclp->z_ops.ace_flags_get(acep);
1724 			if ((iflags & (ACE_FILE_INHERIT_ACE |
1725 			    ACE_DIRECTORY_INHERIT_ACE)) !=
1726 			    ACE_FILE_INHERIT_ACE) {
1727 				aclnode2 = zfs_acl_node_alloc(ace_size);
1728 				list_insert_tail(&aclp->z_acl, aclnode2);
1729 				acep2 = aclnode2->z_acldata;
1730 				zfs_set_ace(aclp, acep2,
1731 				    access_mask, type, who,
1732 				    iflags|ACE_INHERITED_ACE);
1733 				newflags |= ACE_INHERIT_ONLY_ACE;
1734 				aclp->z_ops.ace_flags_set(acep, newflags);
1735 				newflags &= ~ALL_INHERIT;
1736 				aclp->z_ops.ace_flags_set(acep2,
1737 				    newflags|ACE_INHERITED_ACE);
1738 
1739 				/*
1740 				 * Copy special opaque data if any
1741 				 */
1742 				if ((data1sz = aclp->z_ops.ace_data(acep,
1743 				    &data1)) != 0) {
1744 					VERIFY((data2sz =
1745 					    aclp->z_ops.ace_data(acep2,
1746 					    &data2)) == data1sz);
1747 					bcopy(data1, data2, data1sz);
1748 				}
1749 				aclp->z_acl_count++;
1750 				aclnode2->z_ace_count++;
1751 				aclp->z_acl_bytes += aclnode->z_size;
1752 				zfs_restricted_update(zfsvfs, aclp, acep2);
1753 			} else {
1754 				newflags |= ACE_INHERIT_ONLY_ACE;
1755 				aclp->z_ops.ace_flags_set(acep,
1756 				    newflags|ACE_INHERITED_ACE);
1757 			}
1758 		}
1759 	}
1760 	return (aclp);
1761 }
1762 
1763 /*
1764  * Create file system object initial permissions
1765  * including inheritable ACEs.
1766  */
1767 void
1768 zfs_perm_init(znode_t *zp, znode_t *parent, int flag,
1769     vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
1770     zfs_acl_t *setaclp, zfs_fuid_info_t **fuidp)
1771 {
1772 	uint64_t	mode, fuid, fgid;
1773 	int		error;
1774 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1775 	zfs_acl_t	*aclp = NULL;
1776 	zfs_acl_t	*paclp;
1777 	xvattr_t	*xvap = (xvattr_t *)vap;
1778 	gid_t		gid;
1779 	boolean_t	need_chmod = B_TRUE;
1780 
1781 	if (setaclp)
1782 		aclp = setaclp;
1783 
1784 	mode = MAKEIMODE(vap->va_type, vap->va_mode);
1785 
1786 	/*
1787 	 * Determine uid and gid.
1788 	 */
1789 	if ((flag & (IS_ROOT_NODE | IS_REPLAY)) ||
1790 	    ((flag & IS_XATTR) && (vap->va_type == VDIR))) {
1791 		fuid = zfs_fuid_create(zfsvfs, vap->va_uid, cr,
1792 		    ZFS_OWNER, tx, fuidp);
1793 		fgid = zfs_fuid_create(zfsvfs, vap->va_gid, cr,
1794 		    ZFS_GROUP, tx, fuidp);
1795 		gid = vap->va_gid;
1796 	} else {
1797 		fuid = zfs_fuid_create_cred(zfsvfs, ZFS_OWNER, tx, cr, fuidp);
1798 		fgid = 0;
1799 		if (vap->va_mask & AT_GID)  {
1800 			fgid = zfs_fuid_create(zfsvfs, vap->va_gid, cr,
1801 			    ZFS_GROUP, tx, fuidp);
1802 			gid = vap->va_gid;
1803 			if (fgid != parent->z_phys->zp_gid &&
1804 			    !groupmember(vap->va_gid, cr) &&
1805 			    secpolicy_vnode_create_gid(cr) != 0)
1806 				fgid = 0;
1807 		}
1808 		if (fgid == 0) {
1809 			if (parent->z_phys->zp_mode & S_ISGID) {
1810 				fgid = parent->z_phys->zp_gid;
1811 				gid = zfs_fuid_map_id(zfsvfs, fgid,
1812 				    cr, ZFS_GROUP);
1813 			} else {
1814 				fgid = zfs_fuid_create_cred(zfsvfs,
1815 				    ZFS_GROUP, tx, cr, fuidp);
1816 				gid = crgetgid(cr);
1817 			}
1818 		}
1819 	}
1820 
1821 	/*
1822 	 * If we're creating a directory, and the parent directory has the
1823 	 * set-GID bit set, set in on the new directory.
1824 	 * Otherwise, if the user is neither privileged nor a member of the
1825 	 * file's new group, clear the file's set-GID bit.
1826 	 */
1827 
1828 	if ((parent->z_phys->zp_mode & S_ISGID) && (vap->va_type == VDIR)) {
1829 		mode |= S_ISGID;
1830 	} else {
1831 		if ((mode & S_ISGID) &&
1832 		    secpolicy_vnode_setids_setgids(cr, gid) != 0)
1833 			mode &= ~S_ISGID;
1834 	}
1835 
1836 	zp->z_phys->zp_uid = fuid;
1837 	zp->z_phys->zp_gid = fgid;
1838 	zp->z_phys->zp_mode = mode;
1839 
1840 	if (aclp == NULL) {
1841 		mutex_enter(&parent->z_lock);
1842 		if ((ZTOV(parent)->v_type == VDIR &&
1843 		    parent->z_phys->zp_flags & ZFS_INHERIT_ACE)) {
1844 			mutex_enter(&parent->z_acl_lock);
1845 			VERIFY(0 == zfs_acl_node_read(parent, &paclp, B_FALSE));
1846 			mutex_exit(&parent->z_acl_lock);
1847 			aclp = zfs_acl_inherit(zp, paclp, &need_chmod);
1848 			zfs_acl_free(paclp);
1849 		} else {
1850 			aclp = zfs_acl_alloc(zfs_acl_version_zp(zp));
1851 		}
1852 		mutex_exit(&parent->z_lock);
1853 		mutex_enter(&zp->z_lock);
1854 		mutex_enter(&zp->z_acl_lock);
1855 		if (need_chmod)
1856 			zfs_acl_chmod(zp, mode, aclp);
1857 	} else {
1858 		mutex_enter(&zp->z_lock);
1859 		mutex_enter(&zp->z_acl_lock);
1860 	}
1861 
1862 	/* Force auto_inherit on all new directory objects */
1863 	if (vap->va_type == VDIR)
1864 		aclp->z_hints |= ZFS_ACL_AUTO_INHERIT;
1865 
1866 	error = zfs_aclset_common(zp, aclp, cr, fuidp, tx);
1867 
1868 	/* Set optional attributes if any */
1869 	if (vap->va_mask & AT_XVATTR)
1870 		zfs_xvattr_set(zp, xvap);
1871 
1872 	mutex_exit(&zp->z_lock);
1873 	mutex_exit(&zp->z_acl_lock);
1874 	ASSERT3U(error, ==, 0);
1875 
1876 	if (aclp != setaclp)
1877 		zfs_acl_free(aclp);
1878 }
1879 
1880 /*
1881  * Retrieve a files ACL
1882  */
1883 int
1884 zfs_getacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr)
1885 {
1886 	zfs_acl_t	*aclp;
1887 	ulong_t		mask;
1888 	int		error;
1889 	int 		count = 0;
1890 	int		largeace = 0;
1891 
1892 	mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT |
1893 	    VSA_ACE_ACLFLAGS | VSA_ACE_ALLTYPES);
1894 
1895 	if (error = zfs_zaccess(zp, ACE_READ_ACL, 0, skipaclchk, cr))
1896 		return (error);
1897 
1898 	if (mask == 0)
1899 		return (ENOSYS);
1900 
1901 	mutex_enter(&zp->z_acl_lock);
1902 
1903 	error = zfs_acl_node_read(zp, &aclp, B_FALSE);
1904 	if (error != 0) {
1905 		mutex_exit(&zp->z_acl_lock);
1906 		return (error);
1907 	}
1908 
1909 	/*
1910 	 * Scan ACL to determine number of ACEs
1911 	 */
1912 	if ((zp->z_phys->zp_flags & ZFS_ACL_OBJ_ACE) &&
1913 	    !(mask & VSA_ACE_ALLTYPES)) {
1914 		void *zacep = NULL;
1915 		uint64_t who;
1916 		uint32_t access_mask;
1917 		uint16_t type, iflags;
1918 
1919 		while (zacep = zfs_acl_next_ace(aclp, zacep,
1920 		    &who, &access_mask, &iflags, &type)) {
1921 			switch (type) {
1922 			case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1923 			case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1924 			case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1925 			case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1926 				largeace++;
1927 				continue;
1928 			default:
1929 				count++;
1930 			}
1931 		}
1932 		vsecp->vsa_aclcnt = count;
1933 	} else
1934 		count = aclp->z_acl_count;
1935 
1936 	if (mask & VSA_ACECNT) {
1937 		vsecp->vsa_aclcnt = count;
1938 	}
1939 
1940 	if (mask & VSA_ACE) {
1941 		size_t aclsz;
1942 
1943 		zfs_acl_node_t *aclnode = list_head(&aclp->z_acl);
1944 
1945 		aclsz = count * sizeof (ace_t) +
1946 		    sizeof (ace_object_t) * largeace;
1947 
1948 		vsecp->vsa_aclentp = kmem_alloc(aclsz, KM_SLEEP);
1949 		vsecp->vsa_aclentsz = aclsz;
1950 
1951 		if (aclp->z_version == ZFS_ACL_VERSION_FUID)
1952 			zfs_copy_fuid_2_ace(zp->z_zfsvfs, aclp, cr,
1953 			    vsecp->vsa_aclentp, !(mask & VSA_ACE_ALLTYPES));
1954 		else {
1955 			bcopy(aclnode->z_acldata, vsecp->vsa_aclentp,
1956 			    count * sizeof (ace_t));
1957 		}
1958 	}
1959 	if (mask & VSA_ACE_ACLFLAGS) {
1960 		vsecp->vsa_aclflags = 0;
1961 		if (zp->z_phys->zp_flags & ZFS_ACL_DEFAULTED)
1962 			vsecp->vsa_aclflags |= ACL_DEFAULTED;
1963 		if (zp->z_phys->zp_flags & ZFS_ACL_PROTECTED)
1964 			vsecp->vsa_aclflags |= ACL_PROTECTED;
1965 		if (zp->z_phys->zp_flags & ZFS_ACL_AUTO_INHERIT)
1966 			vsecp->vsa_aclflags |= ACL_AUTO_INHERIT;
1967 	}
1968 
1969 	mutex_exit(&zp->z_acl_lock);
1970 
1971 	zfs_acl_free(aclp);
1972 
1973 	return (0);
1974 }
1975 
1976 int
1977 zfs_vsec_2_aclp(zfsvfs_t *zfsvfs, vtype_t obj_type,
1978     vsecattr_t *vsecp, zfs_acl_t **zaclp)
1979 {
1980 	zfs_acl_t *aclp;
1981 	zfs_acl_node_t *aclnode;
1982 	int aclcnt = vsecp->vsa_aclcnt;
1983 	int error;
1984 
1985 	if (vsecp->vsa_aclcnt > MAX_ACL_ENTRIES || vsecp->vsa_aclcnt <= 0)
1986 		return (EINVAL);
1987 
1988 	aclp = zfs_acl_alloc(zfs_acl_version(zfsvfs->z_version));
1989 
1990 	aclp->z_hints = 0;
1991 	aclnode = zfs_acl_node_alloc(aclcnt * sizeof (zfs_object_ace_t));
1992 	if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) {
1993 		if ((error = zfs_copy_ace_2_oldace(obj_type, aclp,
1994 		    (ace_t *)vsecp->vsa_aclentp, aclnode->z_acldata,
1995 		    aclcnt, &aclnode->z_size)) != 0) {
1996 			zfs_acl_free(aclp);
1997 			zfs_acl_node_free(aclnode);
1998 			return (error);
1999 		}
2000 	} else {
2001 		if ((error = zfs_copy_ace_2_fuid(obj_type, aclp,
2002 		    vsecp->vsa_aclentp, aclnode->z_acldata, aclcnt,
2003 		    &aclnode->z_size)) != 0) {
2004 			zfs_acl_free(aclp);
2005 			zfs_acl_node_free(aclnode);
2006 			return (error);
2007 		}
2008 	}
2009 	aclp->z_acl_bytes = aclnode->z_size;
2010 	aclnode->z_ace_count = aclcnt;
2011 	aclp->z_acl_count = aclcnt;
2012 	list_insert_head(&aclp->z_acl, aclnode);
2013 
2014 	/*
2015 	 * If flags are being set then add them to z_hints
2016 	 */
2017 	if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS) {
2018 		if (vsecp->vsa_aclflags & ACL_PROTECTED)
2019 			aclp->z_hints |= ZFS_ACL_PROTECTED;
2020 		if (vsecp->vsa_aclflags & ACL_DEFAULTED)
2021 			aclp->z_hints |= ZFS_ACL_DEFAULTED;
2022 		if (vsecp->vsa_aclflags & ACL_AUTO_INHERIT)
2023 			aclp->z_hints |= ZFS_ACL_AUTO_INHERIT;
2024 	}
2025 
2026 	*zaclp = aclp;
2027 
2028 	return (0);
2029 }
2030 
2031 /*
2032  * Set a files ACL
2033  */
2034 int
2035 zfs_setacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr)
2036 {
2037 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2038 	zilog_t		*zilog = zfsvfs->z_log;
2039 	ulong_t		mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT);
2040 	dmu_tx_t	*tx;
2041 	int		error;
2042 	zfs_acl_t	*aclp;
2043 	zfs_fuid_info_t	*fuidp = NULL;
2044 
2045 	if (mask == 0)
2046 		return (ENOSYS);
2047 
2048 	if (zp->z_phys->zp_flags & ZFS_IMMUTABLE)
2049 		return (EPERM);
2050 
2051 	if (error = zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr))
2052 		return (error);
2053 
2054 	error = zfs_vsec_2_aclp(zfsvfs, ZTOV(zp)->v_type, vsecp, &aclp);
2055 	if (error)
2056 		return (error);
2057 
2058 	/*
2059 	 * If ACL wide flags aren't being set then preserve any
2060 	 * existing flags.
2061 	 */
2062 	if (!(vsecp->vsa_mask & VSA_ACE_ACLFLAGS)) {
2063 		aclp->z_hints |= (zp->z_phys->zp_flags & V4_ACL_WIDE_FLAGS);
2064 	}
2065 top:
2066 	if (error = zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr)) {
2067 		zfs_acl_free(aclp);
2068 		return (error);
2069 	}
2070 
2071 	mutex_enter(&zp->z_lock);
2072 	mutex_enter(&zp->z_acl_lock);
2073 
2074 	tx = dmu_tx_create(zfsvfs->z_os);
2075 	dmu_tx_hold_bonus(tx, zp->z_id);
2076 
2077 	if (zp->z_phys->zp_acl.z_acl_extern_obj) {
2078 		/* Are we upgrading ACL? */
2079 		if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
2080 		    zp->z_phys->zp_acl.z_acl_version ==
2081 		    ZFS_ACL_VERSION_INITIAL) {
2082 			dmu_tx_hold_free(tx,
2083 			    zp->z_phys->zp_acl.z_acl_extern_obj,
2084 			    0, DMU_OBJECT_END);
2085 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2086 			    0, aclp->z_acl_bytes);
2087 		} else {
2088 			dmu_tx_hold_write(tx,
2089 			    zp->z_phys->zp_acl.z_acl_extern_obj,
2090 			    0, aclp->z_acl_bytes);
2091 		}
2092 	} else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2093 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, aclp->z_acl_bytes);
2094 	}
2095 	if (aclp->z_has_fuids) {
2096 		if (zfsvfs->z_fuid_obj == 0) {
2097 			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
2098 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2099 			    FUID_SIZE_ESTIMATE(zfsvfs));
2100 			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
2101 		} else {
2102 			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
2103 			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
2104 			    FUID_SIZE_ESTIMATE(zfsvfs));
2105 		}
2106 	}
2107 
2108 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2109 	if (error) {
2110 		mutex_exit(&zp->z_acl_lock);
2111 		mutex_exit(&zp->z_lock);
2112 
2113 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2114 			dmu_tx_wait(tx);
2115 			dmu_tx_abort(tx);
2116 			goto top;
2117 		}
2118 		dmu_tx_abort(tx);
2119 		zfs_acl_free(aclp);
2120 		return (error);
2121 	}
2122 
2123 	error = zfs_aclset_common(zp, aclp, cr, &fuidp, tx);
2124 	ASSERT(error == 0);
2125 
2126 	zfs_log_acl(zilog, tx, zp, vsecp, fuidp);
2127 
2128 	if (fuidp)
2129 		zfs_fuid_info_free(fuidp);
2130 	zfs_acl_free(aclp);
2131 	dmu_tx_commit(tx);
2132 done:
2133 	mutex_exit(&zp->z_acl_lock);
2134 	mutex_exit(&zp->z_lock);
2135 
2136 	return (error);
2137 }
2138 
2139 /*
2140  * working_mode returns the permissions that were not granted
2141  */
2142 static int
2143 zfs_zaccess_common(znode_t *zp, uint32_t v4_mode, uint32_t *working_mode,
2144     boolean_t *check_privs, boolean_t skipaclchk, cred_t *cr)
2145 {
2146 	zfs_acl_t	*aclp;
2147 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2148 	int		error;
2149 	uid_t		uid = crgetuid(cr);
2150 	uint64_t 	who;
2151 	uint16_t	type, iflags;
2152 	uint16_t	entry_type;
2153 	uint32_t	access_mask;
2154 	uint32_t	deny_mask = 0;
2155 	zfs_ace_hdr_t	*acep = NULL;
2156 	boolean_t	checkit;
2157 	uid_t		fowner;
2158 	uid_t		gowner;
2159 
2160 	/*
2161 	 * Short circuit empty requests
2162 	 */
2163 	if (v4_mode == 0)
2164 		return (0);
2165 
2166 	*check_privs = B_TRUE;
2167 
2168 	if (zfsvfs->z_assign >= TXG_INITIAL) {		/* ZIL replay */
2169 		*working_mode = 0;
2170 		return (0);
2171 	}
2172 
2173 	*working_mode = v4_mode;
2174 
2175 	if ((v4_mode & WRITE_MASK) &&
2176 	    (zp->z_zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) &&
2177 	    (!IS_DEVVP(ZTOV(zp)))) {
2178 		*check_privs = B_FALSE;
2179 		return (EROFS);
2180 	}
2181 
2182 	/*
2183 	 * Only check for READONLY on non-directories.
2184 	 */
2185 	if ((v4_mode & WRITE_MASK_DATA) &&
2186 	    (((ZTOV(zp)->v_type != VDIR) &&
2187 	    (zp->z_phys->zp_flags & (ZFS_READONLY | ZFS_IMMUTABLE))) ||
2188 	    (ZTOV(zp)->v_type == VDIR &&
2189 	    (zp->z_phys->zp_flags & ZFS_IMMUTABLE)))) {
2190 		*check_privs = B_FALSE;
2191 		return (EPERM);
2192 	}
2193 
2194 	if ((v4_mode & (ACE_DELETE | ACE_DELETE_CHILD)) &&
2195 	    (zp->z_phys->zp_flags & ZFS_NOUNLINK)) {
2196 		*check_privs = B_FALSE;
2197 		return (EPERM);
2198 	}
2199 
2200 	if (((v4_mode & (ACE_READ_DATA|ACE_EXECUTE)) &&
2201 	    (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED))) {
2202 		*check_privs = B_FALSE;
2203 		return (EACCES);
2204 	}
2205 
2206 	/*
2207 	 * The caller requested that the ACL check be skipped.  This
2208 	 * would only happen if the caller checked VOP_ACCESS() with a
2209 	 * 32 bit ACE mask and already had the appropriate permissions.
2210 	 */
2211 	if (skipaclchk) {
2212 		*working_mode = 0;
2213 		return (0);
2214 	}
2215 
2216 	zfs_fuid_map_ids(zp, cr, &fowner, &gowner);
2217 
2218 	mutex_enter(&zp->z_acl_lock);
2219 
2220 	error = zfs_acl_node_read(zp, &aclp, B_FALSE);
2221 	if (error != 0) {
2222 		mutex_exit(&zp->z_acl_lock);
2223 		return (error);
2224 	}
2225 
2226 	while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask,
2227 	    &iflags, &type)) {
2228 
2229 		if (ZTOV(zp)->v_type == VDIR && (iflags & ACE_INHERIT_ONLY_ACE))
2230 			continue;
2231 
2232 		entry_type = (iflags & ACE_TYPE_FLAGS);
2233 
2234 		checkit = B_FALSE;
2235 
2236 		switch (entry_type) {
2237 		case ACE_OWNER:
2238 			if (uid == fowner)
2239 				checkit = B_TRUE;
2240 			break;
2241 		case OWNING_GROUP:
2242 			who = gowner;
2243 			/*FALLTHROUGH*/
2244 		case ACE_IDENTIFIER_GROUP:
2245 			checkit = zfs_groupmember(zfsvfs, who, cr);
2246 			break;
2247 		case ACE_EVERYONE:
2248 			checkit = B_TRUE;
2249 			break;
2250 
2251 		/* USER Entry */
2252 		default:
2253 			if (entry_type == 0) {
2254 				uid_t newid;
2255 
2256 				newid = zfs_fuid_map_id(zfsvfs, who, cr,
2257 				    ZFS_ACE_USER);
2258 				if (newid != IDMAP_WK_CREATOR_OWNER_UID &&
2259 				    uid == newid)
2260 					checkit = B_TRUE;
2261 				break;
2262 			} else {
2263 				zfs_acl_free(aclp);
2264 				mutex_exit(&zp->z_acl_lock);
2265 				return (EIO);
2266 			}
2267 		}
2268 
2269 		if (checkit) {
2270 			uint32_t mask_matched = (access_mask & *working_mode);
2271 
2272 			if (mask_matched) {
2273 				if (type == DENY)
2274 					deny_mask |= mask_matched;
2275 
2276 				*working_mode &= ~mask_matched;
2277 			}
2278 		}
2279 
2280 		/* Are we done? */
2281 		if (*working_mode == 0)
2282 			break;
2283 	}
2284 
2285 	mutex_exit(&zp->z_acl_lock);
2286 	zfs_acl_free(aclp);
2287 
2288 	/* Put the found 'denies' back on the working mode */
2289 	if (deny_mask) {
2290 		*working_mode |= deny_mask;
2291 		return (EACCES);
2292 	} else if (*working_mode) {
2293 		return (-1);
2294 	}
2295 
2296 	return (0);
2297 }
2298 
2299 static int
2300 zfs_zaccess_append(znode_t *zp, uint32_t *working_mode, boolean_t *check_privs,
2301     cred_t *cr)
2302 {
2303 	if (*working_mode != ACE_WRITE_DATA)
2304 		return (EACCES);
2305 
2306 	return (zfs_zaccess_common(zp, ACE_APPEND_DATA, working_mode,
2307 	    check_privs, B_FALSE, cr));
2308 }
2309 
2310 /*
2311  * Determine whether Access should be granted/denied, invoking least
2312  * priv subsytem when a deny is determined.
2313  */
2314 int
2315 zfs_zaccess(znode_t *zp, int mode, int flags, boolean_t skipaclchk, cred_t *cr)
2316 {
2317 	uint32_t	working_mode;
2318 	int		error;
2319 	int		is_attr;
2320 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2321 	boolean_t 	check_privs;
2322 	znode_t		*xzp;
2323 	znode_t 	*check_zp = zp;
2324 
2325 	is_attr = ((zp->z_phys->zp_flags & ZFS_XATTR) &&
2326 	    (ZTOV(zp)->v_type == VDIR));
2327 
2328 	/*
2329 	 * If attribute then validate against base file
2330 	 */
2331 	if (is_attr) {
2332 		if ((error = zfs_zget(zp->z_zfsvfs,
2333 		    zp->z_phys->zp_parent, &xzp)) != 0)	{
2334 			return (error);
2335 		}
2336 
2337 		check_zp = xzp;
2338 
2339 		/*
2340 		 * fixup mode to map to xattr perms
2341 		 */
2342 
2343 		if (mode & (ACE_WRITE_DATA|ACE_APPEND_DATA)) {
2344 			mode &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
2345 			mode |= ACE_WRITE_NAMED_ATTRS;
2346 		}
2347 
2348 		if (mode & (ACE_READ_DATA|ACE_EXECUTE)) {
2349 			mode &= ~(ACE_READ_DATA|ACE_EXECUTE);
2350 			mode |= ACE_READ_NAMED_ATTRS;
2351 		}
2352 	}
2353 
2354 	if ((error = zfs_zaccess_common(check_zp, mode, &working_mode,
2355 	    &check_privs, skipaclchk, cr)) == 0) {
2356 		if (is_attr)
2357 			VN_RELE(ZTOV(xzp));
2358 		return (0);
2359 	}
2360 
2361 	if (error && !check_privs) {
2362 		if (is_attr)
2363 			VN_RELE(ZTOV(xzp));
2364 		return (error);
2365 	}
2366 
2367 	if (error && (flags & V_APPEND)) {
2368 		error = zfs_zaccess_append(zp, &working_mode, &check_privs, cr);
2369 	}
2370 
2371 	if (error && check_privs) {
2372 		uid_t		owner;
2373 		mode_t		checkmode = 0;
2374 
2375 		owner = zfs_fuid_map_id(zfsvfs, check_zp->z_phys->zp_uid, cr,
2376 		    ZFS_OWNER);
2377 
2378 		/*
2379 		 * First check for implicit owner permission on
2380 		 * read_acl/read_attributes
2381 		 */
2382 
2383 		error = 0;
2384 		ASSERT(working_mode != 0);
2385 
2386 		if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) &&
2387 		    owner == crgetuid(cr)))
2388 			working_mode &= ~(ACE_READ_ACL|ACE_READ_ATTRIBUTES);
2389 
2390 		if (working_mode & (ACE_READ_DATA|ACE_READ_NAMED_ATTRS|
2391 		    ACE_READ_ACL|ACE_READ_ATTRIBUTES))
2392 			checkmode |= VREAD;
2393 		if (working_mode & (ACE_WRITE_DATA|ACE_WRITE_NAMED_ATTRS|
2394 		    ACE_APPEND_DATA|ACE_WRITE_ATTRIBUTES))
2395 			checkmode |= VWRITE;
2396 		if (working_mode & ACE_EXECUTE)
2397 			checkmode |= VEXEC;
2398 
2399 		if (checkmode)
2400 			error = secpolicy_vnode_access(cr, ZTOV(check_zp),
2401 			    owner, checkmode);
2402 
2403 		if (error == 0 && (working_mode & ACE_WRITE_OWNER))
2404 			error = secpolicy_vnode_create_gid(cr);
2405 		if (error == 0 && (working_mode & ACE_WRITE_ACL))
2406 			error = secpolicy_vnode_setdac(cr, owner);
2407 
2408 		if (error == 0 && (working_mode &
2409 		    (ACE_DELETE|ACE_DELETE_CHILD)))
2410 			error = secpolicy_vnode_remove(cr);
2411 
2412 		if (error == 0 && (working_mode & ACE_SYNCHRONIZE))
2413 			error = secpolicy_vnode_owner(cr, owner);
2414 
2415 		if (error == 0) {
2416 			/*
2417 			 * See if any bits other than those already checked
2418 			 * for are still present.  If so then return EACCES
2419 			 */
2420 			if (working_mode & ~(ZFS_CHECKED_MASKS)) {
2421 				error = EACCES;
2422 			}
2423 		}
2424 	}
2425 
2426 	if (is_attr)
2427 		VN_RELE(ZTOV(xzp));
2428 
2429 	return (error);
2430 }
2431 
2432 /*
2433  * Translate traditional unix VREAD/VWRITE/VEXEC mode into
2434  * native ACL format and call zfs_zaccess()
2435  */
2436 int
2437 zfs_zaccess_rwx(znode_t *zp, mode_t mode, int flags, cred_t *cr)
2438 {
2439 	return (zfs_zaccess(zp, zfs_unix_to_v4(mode >> 6), flags, B_FALSE, cr));
2440 }
2441 
2442 /*
2443  * Access function for secpolicy_vnode_setattr
2444  */
2445 int
2446 zfs_zaccess_unix(znode_t *zp, mode_t mode, cred_t *cr)
2447 {
2448 	int v4_mode = zfs_unix_to_v4(mode >> 6);
2449 
2450 	return (zfs_zaccess(zp, v4_mode, 0, B_FALSE, cr));
2451 }
2452 
2453 static int
2454 zfs_delete_final_check(znode_t *zp, znode_t *dzp,
2455     mode_t missing_perms, cred_t *cr)
2456 {
2457 	int error;
2458 	uid_t downer;
2459 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2460 
2461 	downer = zfs_fuid_map_id(zfsvfs, dzp->z_phys->zp_uid, cr, ZFS_OWNER);
2462 
2463 	error = secpolicy_vnode_access(cr, ZTOV(dzp), downer, missing_perms);
2464 
2465 	if (error == 0)
2466 		error = zfs_sticky_remove_access(dzp, zp, cr);
2467 
2468 	return (error);
2469 }
2470 
2471 /*
2472  * Determine whether Access should be granted/deny, without
2473  * consulting least priv subsystem.
2474  *
2475  *
2476  * The following chart is the recommended NFSv4 enforcement for
2477  * ability to delete an object.
2478  *
2479  *      -------------------------------------------------------
2480  *      |   Parent Dir  |           Target Object Permissions |
2481  *      |  permissions  |                                     |
2482  *      -------------------------------------------------------
2483  *      |               | ACL Allows | ACL Denies| Delete     |
2484  *      |               |  Delete    |  Delete   | unspecified|
2485  *      -------------------------------------------------------
2486  *      |  ACL Allows   | Permit     | Permit    | Permit     |
2487  *      |  DELETE_CHILD |                                     |
2488  *      -------------------------------------------------------
2489  *      |  ACL Denies   | Permit     | Deny      | Deny       |
2490  *      |  DELETE_CHILD |            |           |            |
2491  *      -------------------------------------------------------
2492  *      | ACL specifies |            |           |            |
2493  *      | only allow    | Permit     | Permit    | Permit     |
2494  *      | write and     |            |           |            |
2495  *      | execute       |            |           |            |
2496  *      -------------------------------------------------------
2497  *      | ACL denies    |            |           |            |
2498  *      | write and     | Permit     | Deny      | Deny       |
2499  *      | execute       |            |           |            |
2500  *      -------------------------------------------------------
2501  *         ^
2502  *         |
2503  *         No search privilege, can't even look up file?
2504  *
2505  */
2506 int
2507 zfs_zaccess_delete(znode_t *dzp, znode_t *zp, cred_t *cr)
2508 {
2509 	uint32_t dzp_working_mode = 0;
2510 	uint32_t zp_working_mode = 0;
2511 	int dzp_error, zp_error;
2512 	mode_t missing_perms;
2513 	boolean_t dzpcheck_privs = B_TRUE;
2514 	boolean_t zpcheck_privs = B_TRUE;
2515 
2516 	/*
2517 	 * We want specific DELETE permissions to
2518 	 * take precedence over WRITE/EXECUTE.  We don't
2519 	 * want an ACL such as this to mess us up.
2520 	 * user:joe:write_data:deny,user:joe:delete:allow
2521 	 *
2522 	 * However, deny permissions may ultimately be overridden
2523 	 * by secpolicy_vnode_access().
2524 	 *
2525 	 * We will ask for all of the necessary permissions and then
2526 	 * look at the working modes from the directory and target object
2527 	 * to determine what was found.
2528 	 */
2529 
2530 	if (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_NOUNLINK))
2531 		return (EPERM);
2532 
2533 	/*
2534 	 * First row
2535 	 * If the directory permissions allow the delete, we are done.
2536 	 */
2537 	if ((dzp_error = zfs_zaccess_common(dzp, ACE_DELETE_CHILD,
2538 	    &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr)) == 0)
2539 		return (0);
2540 
2541 	/*
2542 	 * If target object has delete permission then we are done
2543 	 */
2544 	if ((zp_error = zfs_zaccess_common(zp, ACE_DELETE, &zp_working_mode,
2545 	    &zpcheck_privs, B_FALSE, cr)) == 0)
2546 		return (0);
2547 
2548 	ASSERT(dzp_error && zp_error);
2549 
2550 	if (!dzpcheck_privs)
2551 		return (dzp_error);
2552 	if (!zpcheck_privs)
2553 		return (zp_error);
2554 
2555 	/*
2556 	 * Second row
2557 	 *
2558 	 * If directory returns EACCES then delete_child was denied
2559 	 * due to deny delete_child.  In this case send the request through
2560 	 * secpolicy_vnode_remove().  We don't use zfs_delete_final_check()
2561 	 * since that *could* allow the delete based on write/execute permission
2562 	 * and we want delete permissions to override write/execute.
2563 	 */
2564 
2565 	if (dzp_error == EACCES)
2566 		return (secpolicy_vnode_remove(cr));
2567 
2568 	/*
2569 	 * Third Row
2570 	 * only need to see if we have write/execute on directory.
2571 	 */
2572 
2573 	if ((dzp_error = zfs_zaccess_common(dzp, ACE_EXECUTE|ACE_WRITE_DATA,
2574 	    &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr)) == 0)
2575 		return (zfs_sticky_remove_access(dzp, zp, cr));
2576 
2577 	if (!dzpcheck_privs)
2578 		return (dzp_error);
2579 
2580 	/*
2581 	 * Fourth row
2582 	 */
2583 
2584 	missing_perms = (dzp_working_mode & ACE_WRITE_DATA) ? VWRITE : 0;
2585 	missing_perms |= (dzp_working_mode & ACE_EXECUTE) ? VEXEC : 0;
2586 
2587 	ASSERT(missing_perms);
2588 
2589 	return (zfs_delete_final_check(zp, dzp, missing_perms, cr));
2590 
2591 }
2592 
2593 int
2594 zfs_zaccess_rename(znode_t *sdzp, znode_t *szp, znode_t *tdzp,
2595     znode_t *tzp, cred_t *cr)
2596 {
2597 	int add_perm;
2598 	int error;
2599 
2600 	if (szp->z_phys->zp_flags & ZFS_AV_QUARANTINED)
2601 		return (EACCES);
2602 
2603 	add_perm = (ZTOV(szp)->v_type == VDIR) ?
2604 	    ACE_ADD_SUBDIRECTORY : ACE_ADD_FILE;
2605 
2606 	/*
2607 	 * Rename permissions are combination of delete permission +
2608 	 * add file/subdir permission.
2609 	 */
2610 
2611 	/*
2612 	 * first make sure we do the delete portion.
2613 	 *
2614 	 * If that succeeds then check for add_file/add_subdir permissions
2615 	 */
2616 
2617 	if (error = zfs_zaccess_delete(sdzp, szp, cr))
2618 		return (error);
2619 
2620 	/*
2621 	 * If we have a tzp, see if we can delete it?
2622 	 */
2623 	if (tzp) {
2624 		if (error = zfs_zaccess_delete(tdzp, tzp, cr))
2625 			return (error);
2626 	}
2627 
2628 	/*
2629 	 * Now check for add permissions
2630 	 */
2631 	error = zfs_zaccess(tdzp, add_perm, 0, B_FALSE, cr);
2632 
2633 	return (error);
2634 }
2635