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