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