xref: /titanic_52/usr/src/uts/common/fs/zfs/zfs_byteswap.c (revision d583b39bfb4e2571d3e41097c5c357ffe353ad45)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/vfs.h>
28 #include <sys/fs/zfs.h>
29 #include <sys/zfs_znode.h>
30 #include <sys/zfs_sa.h>
31 #include <sys/zfs_acl.h>
32 
33 void
34 zfs_oldace_byteswap(ace_t *ace, int ace_cnt)
35 {
36 	int i;
37 
38 	for (i = 0; i != ace_cnt; i++, ace++) {
39 		ace->a_who = BSWAP_32(ace->a_who);
40 		ace->a_access_mask = BSWAP_32(ace->a_access_mask);
41 		ace->a_flags = BSWAP_16(ace->a_flags);
42 		ace->a_type = BSWAP_16(ace->a_type);
43 	}
44 }
45 
46 /*
47  * swap ace_t and ace_oject_t
48  */
49 void
50 zfs_ace_byteswap(void *buf, size_t size, boolean_t zfs_layout)
51 {
52 	caddr_t end;
53 	caddr_t ptr;
54 	zfs_ace_t *zacep;
55 	ace_t *acep;
56 	uint16_t entry_type;
57 	size_t entry_size;
58 	int ace_type;
59 
60 	end = (caddr_t)buf + size;
61 	ptr = buf;
62 
63 	while (ptr < end) {
64 		if (zfs_layout) {
65 			/*
66 			 * Avoid overrun.  Embedded aces can have one
67 			 * of several sizes.  We don't know exactly
68 			 * how many our present, only the size of the
69 			 * buffer containing them.  That size may be
70 			 * larger than needed to hold the aces
71 			 * present.  As long as we do not do any
72 			 * swapping beyond the end of our block we are
73 			 * okay.  It it safe to swap any non-ace data
74 			 * within the block since it is just zeros.
75 			 */
76 			if (ptr + sizeof (zfs_ace_hdr_t) > end) {
77 				break;
78 			}
79 			zacep = (zfs_ace_t *)ptr;
80 			zacep->z_hdr.z_access_mask =
81 			    BSWAP_32(zacep->z_hdr.z_access_mask);
82 			zacep->z_hdr.z_flags = BSWAP_16(zacep->z_hdr.z_flags);
83 			ace_type = zacep->z_hdr.z_type =
84 			    BSWAP_16(zacep->z_hdr.z_type);
85 			entry_type = zacep->z_hdr.z_flags & ACE_TYPE_FLAGS;
86 		} else {
87 			/* Overrun avoidance */
88 			if (ptr + sizeof (ace_t) > end) {
89 				break;
90 			}
91 			acep = (ace_t *)ptr;
92 			acep->a_access_mask = BSWAP_32(acep->a_access_mask);
93 			acep->a_flags = BSWAP_16(acep->a_flags);
94 			ace_type = acep->a_type = BSWAP_16(acep->a_type);
95 			acep->a_who = BSWAP_32(acep->a_who);
96 			entry_type = acep->a_flags & ACE_TYPE_FLAGS;
97 		}
98 		switch (entry_type) {
99 		case ACE_OWNER:
100 		case ACE_EVERYONE:
101 		case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
102 			entry_size = zfs_layout ?
103 			    sizeof (zfs_ace_hdr_t) : sizeof (ace_t);
104 			break;
105 		case ACE_IDENTIFIER_GROUP:
106 		default:
107 			/* Overrun avoidance */
108 			if (zfs_layout) {
109 				if (ptr + sizeof (zfs_ace_t) <= end) {
110 					zacep->z_fuid = BSWAP_64(zacep->z_fuid);
111 				} else {
112 					entry_size = sizeof (zfs_ace_t);
113 					break;
114 				}
115 			}
116 			switch (ace_type) {
117 			case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
118 			case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
119 			case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
120 			case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
121 				entry_size = zfs_layout ?
122 				    sizeof (zfs_object_ace_t) :
123 				    sizeof (ace_object_t);
124 				break;
125 			default:
126 				entry_size = zfs_layout ? sizeof (zfs_ace_t) :
127 				    sizeof (ace_t);
128 				break;
129 			}
130 		}
131 		ptr = ptr + entry_size;
132 	}
133 }
134 
135 /* ARGSUSED */
136 void
137 zfs_oldacl_byteswap(void *buf, size_t size)
138 {
139 	int cnt;
140 
141 	/*
142 	 * Arggh, since we don't know how many ACEs are in
143 	 * the array, we have to swap the entire block
144 	 */
145 
146 	cnt = size / sizeof (ace_t);
147 
148 	zfs_oldace_byteswap((ace_t *)buf, cnt);
149 }
150 
151 /* ARGSUSED */
152 void
153 zfs_acl_byteswap(void *buf, size_t size)
154 {
155 	zfs_ace_byteswap(buf, size, B_TRUE);
156 }
157 
158 void
159 zfs_znode_byteswap(void *buf, size_t size)
160 {
161 	znode_phys_t *zp = buf;
162 
163 	ASSERT(size >= sizeof (znode_phys_t));
164 
165 	zp->zp_crtime[0] = BSWAP_64(zp->zp_crtime[0]);
166 	zp->zp_crtime[1] = BSWAP_64(zp->zp_crtime[1]);
167 	zp->zp_atime[0] = BSWAP_64(zp->zp_atime[0]);
168 	zp->zp_atime[1] = BSWAP_64(zp->zp_atime[1]);
169 	zp->zp_mtime[0] = BSWAP_64(zp->zp_mtime[0]);
170 	zp->zp_mtime[1] = BSWAP_64(zp->zp_mtime[1]);
171 	zp->zp_ctime[0] = BSWAP_64(zp->zp_ctime[0]);
172 	zp->zp_ctime[1] = BSWAP_64(zp->zp_ctime[1]);
173 	zp->zp_gen = BSWAP_64(zp->zp_gen);
174 	zp->zp_mode = BSWAP_64(zp->zp_mode);
175 	zp->zp_size = BSWAP_64(zp->zp_size);
176 	zp->zp_parent = BSWAP_64(zp->zp_parent);
177 	zp->zp_links = BSWAP_64(zp->zp_links);
178 	zp->zp_xattr = BSWAP_64(zp->zp_xattr);
179 	zp->zp_rdev = BSWAP_64(zp->zp_rdev);
180 	zp->zp_flags = BSWAP_64(zp->zp_flags);
181 	zp->zp_uid = BSWAP_64(zp->zp_uid);
182 	zp->zp_gid = BSWAP_64(zp->zp_gid);
183 	zp->zp_zap = BSWAP_64(zp->zp_zap);
184 	zp->zp_pad[0] = BSWAP_64(zp->zp_pad[0]);
185 	zp->zp_pad[1] = BSWAP_64(zp->zp_pad[1]);
186 	zp->zp_pad[2] = BSWAP_64(zp->zp_pad[2]);
187 
188 	zp->zp_acl.z_acl_extern_obj = BSWAP_64(zp->zp_acl.z_acl_extern_obj);
189 	zp->zp_acl.z_acl_size = BSWAP_32(zp->zp_acl.z_acl_size);
190 	zp->zp_acl.z_acl_version = BSWAP_16(zp->zp_acl.z_acl_version);
191 	zp->zp_acl.z_acl_count = BSWAP_16(zp->zp_acl.z_acl_count);
192 	if (zp->zp_acl.z_acl_version == ZFS_ACL_VERSION) {
193 		zfs_acl_byteswap((void *)&zp->zp_acl.z_ace_data[0],
194 		    ZFS_ACE_SPACE);
195 	} else {
196 		zfs_oldace_byteswap((ace_t *)&zp->zp_acl.z_ace_data[0],
197 		    ACE_SLOT_CNT);
198 	}
199 }
200