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