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