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