xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_byteswap.c (revision 5331:3047ad28a67b)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
5*5331Samw  * Common Development and Distribution License (the "License").
6*5331Samw  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/vfs.h>
30789Sahrens #include <sys/fs/zfs.h>
31789Sahrens #include <sys/zfs_znode.h>
32789Sahrens #include <sys/zfs_acl.h>
33789Sahrens 
34789Sahrens void
35*5331Samw zfs_oldace_byteswap(ace_t *ace, int ace_cnt)
36789Sahrens {
37789Sahrens 	int i;
38789Sahrens 
39789Sahrens 	for (i = 0; i != ace_cnt; i++, ace++) {
40789Sahrens 		ace->a_who = BSWAP_32(ace->a_who);
41789Sahrens 		ace->a_access_mask = BSWAP_32(ace->a_access_mask);
42789Sahrens 		ace->a_flags = BSWAP_16(ace->a_flags);
43789Sahrens 		ace->a_type = BSWAP_16(ace->a_type);
44789Sahrens 	}
45789Sahrens }
46789Sahrens 
47*5331Samw /*
48*5331Samw  * swap ace_t and ace_oject_t
49*5331Samw  */
50*5331Samw void
51*5331Samw zfs_ace_byteswap(void *buf, size_t size, boolean_t zfs_layout)
52*5331Samw {
53*5331Samw 	caddr_t end;
54*5331Samw 	caddr_t ptr;
55*5331Samw 	zfs_ace_t *zacep;
56*5331Samw 	ace_t *acep;
57*5331Samw 	uint16_t entry_type;
58*5331Samw 	size_t entry_size;
59*5331Samw 	int ace_type;
60*5331Samw 
61*5331Samw 	end = (caddr_t)buf + size;
62*5331Samw 	ptr = buf;
63*5331Samw 
64*5331Samw 	while (ptr < end) {
65*5331Samw 		if (zfs_layout) {
66*5331Samw 			zacep = (zfs_ace_t *)ptr;
67*5331Samw 			zacep->z_hdr.z_access_mask =
68*5331Samw 			    BSWAP_32(zacep->z_hdr.z_access_mask);
69*5331Samw 			zacep->z_hdr.z_flags = BSWAP_16(zacep->z_hdr.z_flags);
70*5331Samw 			ace_type = zacep->z_hdr.z_type =
71*5331Samw 			    BSWAP_16(zacep->z_hdr.z_type);
72*5331Samw 			entry_type = zacep->z_hdr.z_flags & ACE_TYPE_FLAGS;
73*5331Samw 		} else {
74*5331Samw 			acep = (ace_t *)ptr;
75*5331Samw 			acep->a_access_mask = BSWAP_32(acep->a_access_mask);
76*5331Samw 			acep->a_flags = BSWAP_16(acep->a_flags);
77*5331Samw 			ace_type = acep->a_type = BSWAP_16(acep->a_type);
78*5331Samw 			acep->a_who = BSWAP_32(acep->a_who);
79*5331Samw 			entry_type = acep->a_flags & ACE_TYPE_FLAGS;
80*5331Samw 		}
81*5331Samw 		switch (entry_type) {
82*5331Samw 		case ACE_OWNER:
83*5331Samw 		case ACE_EVERYONE:
84*5331Samw 		case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
85*5331Samw 			entry_size = zfs_layout ?
86*5331Samw 			    sizeof (zfs_ace_hdr_t) : sizeof (ace_t);
87*5331Samw 			break;
88*5331Samw 		case ACE_IDENTIFIER_GROUP:
89*5331Samw 		default:
90*5331Samw 			if (zfs_layout) {
91*5331Samw 				zacep->z_fuid = BSWAP_64(zacep->z_fuid);
92*5331Samw 			}
93*5331Samw 			switch (ace_type) {
94*5331Samw 			case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
95*5331Samw 			case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
96*5331Samw 			case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
97*5331Samw 			case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
98*5331Samw 				entry_size = zfs_layout ?
99*5331Samw 				    sizeof (zfs_object_ace_t) :
100*5331Samw 				    sizeof (ace_object_t);
101*5331Samw 				break;
102*5331Samw 			default:
103*5331Samw 				entry_size = zfs_layout ? sizeof (zfs_ace_t) :
104*5331Samw 				    sizeof (ace_t);
105*5331Samw 				break;
106*5331Samw 			}
107*5331Samw 		}
108*5331Samw 		ptr = ptr + entry_size;
109*5331Samw 	}
110*5331Samw }
111*5331Samw 
112789Sahrens /* ARGSUSED */
113789Sahrens void
114*5331Samw zfs_oldacl_byteswap(void *buf, size_t size)
115789Sahrens {
116789Sahrens 	int cnt;
117789Sahrens 
118789Sahrens 	/*
119789Sahrens 	 * Arggh, since we don't know how many ACEs are in
120789Sahrens 	 * the array, we have to swap the entire block
121789Sahrens 	 */
122789Sahrens 
123789Sahrens 	cnt = size / sizeof (ace_t);
124789Sahrens 
125*5331Samw 	zfs_oldace_byteswap((ace_t *)buf, cnt);
126*5331Samw }
127*5331Samw 
128*5331Samw /* ARGSUSED */
129*5331Samw void
130*5331Samw zfs_acl_byteswap(void *buf, size_t size)
131*5331Samw {
132*5331Samw 	zfs_ace_byteswap(buf, size, B_TRUE);
133789Sahrens }
134789Sahrens 
135789Sahrens void
136789Sahrens zfs_znode_byteswap(void *buf, size_t size)
137789Sahrens {
138789Sahrens 	znode_phys_t *zp = buf;
139789Sahrens 
140789Sahrens 	ASSERT(size >= sizeof (znode_phys_t));
141789Sahrens 
142789Sahrens 	zp->zp_crtime[0] = BSWAP_64(zp->zp_crtime[0]);
143789Sahrens 	zp->zp_crtime[1] = BSWAP_64(zp->zp_crtime[1]);
144789Sahrens 	zp->zp_atime[0] = BSWAP_64(zp->zp_atime[0]);
145789Sahrens 	zp->zp_atime[1] = BSWAP_64(zp->zp_atime[1]);
146789Sahrens 	zp->zp_mtime[0] = BSWAP_64(zp->zp_mtime[0]);
147789Sahrens 	zp->zp_mtime[1] = BSWAP_64(zp->zp_mtime[1]);
148789Sahrens 	zp->zp_ctime[0] = BSWAP_64(zp->zp_ctime[0]);
149789Sahrens 	zp->zp_ctime[1] = BSWAP_64(zp->zp_ctime[1]);
150789Sahrens 	zp->zp_gen = BSWAP_64(zp->zp_gen);
151789Sahrens 	zp->zp_mode = BSWAP_64(zp->zp_mode);
152789Sahrens 	zp->zp_size = BSWAP_64(zp->zp_size);
153789Sahrens 	zp->zp_parent = BSWAP_64(zp->zp_parent);
154789Sahrens 	zp->zp_links = BSWAP_64(zp->zp_links);
155789Sahrens 	zp->zp_xattr = BSWAP_64(zp->zp_xattr);
156789Sahrens 	zp->zp_rdev = BSWAP_64(zp->zp_rdev);
157789Sahrens 	zp->zp_flags = BSWAP_64(zp->zp_flags);
158789Sahrens 	zp->zp_uid = BSWAP_64(zp->zp_uid);
159789Sahrens 	zp->zp_gid = BSWAP_64(zp->zp_gid);
160*5331Samw 	zp->zp_zap = BSWAP_64(zp->zp_zap);
161789Sahrens 	zp->zp_pad[0] = BSWAP_64(zp->zp_pad[0]);
162789Sahrens 	zp->zp_pad[1] = BSWAP_64(zp->zp_pad[1]);
163789Sahrens 	zp->zp_pad[2] = BSWAP_64(zp->zp_pad[2]);
164789Sahrens 
165789Sahrens 	zp->zp_acl.z_acl_extern_obj = BSWAP_64(zp->zp_acl.z_acl_extern_obj);
166*5331Samw 	zp->zp_acl.z_acl_size = BSWAP_32(zp->zp_acl.z_acl_size);
167789Sahrens 	zp->zp_acl.z_acl_version = BSWAP_16(zp->zp_acl.z_acl_version);
168*5331Samw 	zp->zp_acl.z_acl_count = BSWAP_16(zp->zp_acl.z_acl_count);
169*5331Samw 	if (zp->zp_acl.z_acl_version == ZFS_ACL_VERSION) {
170*5331Samw 		zfs_acl_byteswap((void *)&zp->zp_acl.z_ace_data[0],
171*5331Samw 		    ZFS_ACE_SPACE);
172*5331Samw 	} else
173*5331Samw 		zfs_oldace_byteswap((ace_t *)&zp->zp_acl.z_ace_data[0],
174*5331Samw 		    ACE_SLOT_CNT);
175789Sahrens }
176