1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5da6c28aamw * Common Development and Distribution License (the "License").
6da6c28aamw * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
220a586ceMark Shellenbaum * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23fa9e406ahrens * Use is subject to license terms.
24fa9e406ahrens */
25fa9e406ahrens
26fa9e406ahrens#include <sys/zfs_context.h>
27fa9e406ahrens#include <sys/vfs.h>
28fa9e406ahrens#include <sys/fs/zfs.h>
29fa9e406ahrens#include <sys/zfs_znode.h>
300a586ceMark Shellenbaum#include <sys/zfs_sa.h>
31fa9e406ahrens#include <sys/zfs_acl.h>
32fa9e406ahrens
33fa9e406ahrensvoid
34da6c28aamwzfs_oldace_byteswap(ace_t *ace, int ace_cnt)
35fa9e406ahrens{
36fa9e406ahrens	int i;
37fa9e406ahrens
38fa9e406ahrens	for (i = 0; i != ace_cnt; i++, ace++) {
39fa9e406ahrens		ace->a_who = BSWAP_32(ace->a_who);
40fa9e406ahrens		ace->a_access_mask = BSWAP_32(ace->a_access_mask);
41fa9e406ahrens		ace->a_flags = BSWAP_16(ace->a_flags);
42fa9e406ahrens		ace->a_type = BSWAP_16(ace->a_type);
43fa9e406ahrens	}
44fa9e406ahrens}
45fa9e406ahrens
46da6c28aamw/*
47da6c28aamw * swap ace_t and ace_oject_t
48da6c28aamw */
49da6c28aamwvoid
50da6c28aamwzfs_ace_byteswap(void *buf, size_t size, boolean_t zfs_layout)
51da6c28aamw{
52da6c28aamw	caddr_t end;
53da6c28aamw	caddr_t ptr;
54d5285caGeorge Wilson	zfs_ace_t *zacep = NULL;
55da6c28aamw	ace_t *acep;
56da6c28aamw	uint16_t entry_type;
57da6c28aamw	size_t entry_size;
58da6c28aamw	int ace_type;
59da6c28aamw
60da6c28aamw	end = (caddr_t)buf + size;
61da6c28aamw	ptr = buf;
62da6c28aamw
63da6c28aamw	while (ptr < end) {
64da6c28aamw		if (zfs_layout) {
65ae48068Tim Haley			/*
66ae48068Tim Haley			 * Avoid overrun.  Embedded aces can have one
67ae48068Tim Haley			 * of several sizes.  We don't know exactly
68ae48068Tim Haley			 * how many our present, only the size of the
69ae48068Tim Haley			 * buffer containing them.  That size may be
70ae48068Tim Haley			 * larger than needed to hold the aces
71ae48068Tim Haley			 * present.  As long as we do not do any
72ae48068Tim Haley			 * swapping beyond the end of our block we are
73ae48068Tim Haley			 * okay.  It it safe to swap any non-ace data
74ae48068Tim Haley			 * within the block since it is just zeros.
75ae48068Tim Haley			 */
76ae48068Tim Haley			if (ptr + sizeof (zfs_ace_hdr_t) > end) {
77ae48068Tim Haley				break;
78ae48068Tim Haley			}
79da6c28aamw			zacep = (zfs_ace_t *)ptr;
80da6c28aamw			zacep->z_hdr.z_access_mask =
81da6c28aamw			    BSWAP_32(zacep->z_hdr.z_access_mask);
82da6c28aamw			zacep->z_hdr.z_flags = BSWAP_16(zacep->z_hdr.z_flags);
83da6c28aamw			ace_type = zacep->z_hdr.z_type =
84da6c28aamw			    BSWAP_16(zacep->z_hdr.z_type);
85da6c28aamw			entry_type = zacep->z_hdr.z_flags & ACE_TYPE_FLAGS;
86da6c28aamw		} else {
87ae48068Tim Haley			/* Overrun avoidance */
88ae48068Tim Haley			if (ptr + sizeof (ace_t) > end) {
89ae48068Tim Haley				break;
90ae48068Tim Haley			}
91da6c28aamw			acep = (ace_t *)ptr;
92da6c28aamw			acep->a_access_mask = BSWAP_32(acep->a_access_mask);
93da6c28aamw			acep->a_flags = BSWAP_16(acep->a_flags);
94da6c28aamw			ace_type = acep->a_type = BSWAP_16(acep->a_type);
95da6c28aamw			acep->a_who = BSWAP_32(acep->a_who);
96da6c28aamw			entry_type = acep->a_flags & ACE_TYPE_FLAGS;
97da6c28aamw		}
98da6c28aamw		switch (entry_type) {
99da6c28aamw		case ACE_OWNER:
100da6c28aamw		case ACE_EVERYONE:
101da6c28aamw		case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
102da6c28aamw			entry_size = zfs_layout ?
103da6c28aamw			    sizeof (zfs_ace_hdr_t) : sizeof (ace_t);
104da6c28aamw			break;
105da6c28aamw		case ACE_IDENTIFIER_GROUP:
106da6c28aamw		default:
107ae48068Tim Haley			/* Overrun avoidance */
108da6c28aamw			if (zfs_layout) {
109ae48068Tim Haley				if (ptr + sizeof (zfs_ace_t) <= end) {
110ae48068Tim Haley					zacep->z_fuid = BSWAP_64(zacep->z_fuid);
111ae48068Tim Haley				} else {
112ae48068Tim Haley					entry_size = sizeof (zfs_ace_t);
113ae48068Tim Haley					break;
114ae48068Tim Haley				}
115da6c28aamw			}
116da6c28aamw			switch (ace_type) {
117da6c28aamw			case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
118da6c28aamw			case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
119da6c28aamw			case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
120da6c28aamw			case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
121da6c28aamw				entry_size = zfs_layout ?
122da6c28aamw				    sizeof (zfs_object_ace_t) :
123da6c28aamw				    sizeof (ace_object_t);
124da6c28aamw				break;
125da6c28aamw			default:
126da6c28aamw				entry_size = zfs_layout ? sizeof (zfs_ace_t) :
127da6c28aamw				    sizeof (ace_t);
128da6c28aamw				break;
129da6c28aamw			}
130da6c28aamw		}
131da6c28aamw		ptr = ptr + entry_size;
132da6c28aamw	}
133da6c28aamw}
134da6c28aamw
135fa9e406ahrens/* ARGSUSED */
136fa9e406ahrensvoid
137da6c28aamwzfs_oldacl_byteswap(void *buf, size_t size)
138fa9e406ahrens{
139fa9e406ahrens	int cnt;
140fa9e406ahrens
141fa9e406ahrens	/*
142fa9e406ahrens	 * Arggh, since we don't know how many ACEs are in
143fa9e406ahrens	 * the array, we have to swap the entire block
144fa9e406ahrens	 */
145fa9e406ahrens
146fa9e406ahrens	cnt = size / sizeof (ace_t);
147fa9e406ahrens
148da6c28aamw	zfs_oldace_byteswap((ace_t *)buf, cnt);
149da6c28aamw}
150da6c28aamw
151da6c28aamw/* ARGSUSED */
152da6c28aamwvoid
153da6c28aamwzfs_acl_byteswap(void *buf, size_t size)
154da6c28aamw{
155da6c28aamw	zfs_ace_byteswap(buf, size, B_TRUE);
156fa9e406ahrens}
157fa9e406ahrens
158fa9e406ahrensvoid
159fa9e406ahrenszfs_znode_byteswap(void *buf, size_t size)
160fa9e406ahrens{
161fa9e406ahrens	znode_phys_t *zp = buf;
162fa9e406ahrens
163fa9e406ahrens	ASSERT(size >= sizeof (znode_phys_t));
164fa9e406ahrens
165fa9e406ahrens	zp->zp_crtime[0] = BSWAP_64(zp->zp_crtime[0]);
166fa9e406ahrens	zp->zp_crtime[1] = BSWAP_64(zp->zp_crtime[1]);
167fa9e406ahrens	zp->zp_atime[0] = BSWAP_64(zp->zp_atime[0]);
168fa9e406ahrens	zp->zp_atime[1] = BSWAP_64(zp->zp_atime[1]);
169fa9e406ahrens	zp->zp_mtime[0] = BSWAP_64(zp->zp_mtime[0]);
170fa9e406ahrens	zp->zp_mtime[1] = BSWAP_64(zp->zp_mtime[1]);
171fa9e406ahrens	zp->zp_ctime[0] = BSWAP_64(zp->zp_ctime[0]);
172fa9e406ahrens	zp->zp_ctime[1] = BSWAP_64(zp->zp_ctime[1]);
173fa9e406ahrens	zp->zp_gen = BSWAP_64(zp->zp_gen);
174fa9e406ahrens	zp->zp_mode = BSWAP_64(zp->zp_mode);
175fa9e406ahrens	zp->zp_size = BSWAP_64(zp->zp_size);
176fa9e406ahrens	zp->zp_parent = BSWAP_64(zp->zp_parent);
177fa9e406ahrens	zp->zp_links = BSWAP_64(zp->zp_links);
178fa9e406ahrens	zp->zp_xattr = BSWAP_64(zp->zp_xattr);
179fa9e406ahrens	zp->zp_rdev = BSWAP_64(zp->zp_rdev);
180fa9e406ahrens	zp->zp_flags = BSWAP_64(zp->zp_flags);
181fa9e406ahrens	zp->zp_uid = BSWAP_64(zp->zp_uid);
182fa9e406ahrens	zp->zp_gid = BSWAP_64(zp->zp_gid);
183da6c28aamw	zp->zp_zap = BSWAP_64(zp->zp_zap);
184fa9e406ahrens	zp->zp_pad[0] = BSWAP_64(zp->zp_pad[0]);
185fa9e406ahrens	zp->zp_pad[1] = BSWAP_64(zp->zp_pad[1]);
186fa9e406ahrens	zp->zp_pad[2] = BSWAP_64(zp->zp_pad[2]);
187fa9e406ahrens
188fa9e406ahrens	zp->zp_acl.z_acl_extern_obj = BSWAP_64(zp->zp_acl.z_acl_extern_obj);
189da6c28aamw	zp->zp_acl.z_acl_size = BSWAP_32(zp->zp_acl.z_acl_size);
190fa9e406ahrens	zp->zp_acl.z_acl_version = BSWAP_16(zp->zp_acl.z_acl_version);
191da6c28aamw	zp->zp_acl.z_acl_count = BSWAP_16(zp->zp_acl.z_acl_count);
192da6c28aamw	if (zp->zp_acl.z_acl_version == ZFS_ACL_VERSION) {
193da6c28aamw		zfs_acl_byteswap((void *)&zp->zp_acl.z_ace_data[0],
194da6c28aamw		    ZFS_ACE_SPACE);
195ae48068Tim Haley	} else {
196da6c28aamw		zfs_oldace_byteswap((ace_t *)&zp->zp_acl.z_ace_data[0],
197da6c28aamw		    ACE_SLOT_CNT);
198ae48068Tim Haley	}
199fa9e406ahrens}
200