Home | History | Annotate | Download | only in zfs
      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