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