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