xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_byteswap.c (revision 8283:1ca59f393041)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * 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*8283STim.Haley@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/zfs_context.h>
27789Sahrens #include <sys/vfs.h>
28789Sahrens #include <sys/fs/zfs.h>
29789Sahrens #include <sys/zfs_znode.h>
30789Sahrens #include <sys/zfs_acl.h>
31789Sahrens 
32789Sahrens void
335331Samw zfs_oldace_byteswap(ace_t *ace, int ace_cnt)
34789Sahrens {
35789Sahrens 	int i;
36789Sahrens 
37789Sahrens 	for (i = 0; i != ace_cnt; i++, ace++) {
38789Sahrens 		ace->a_who = BSWAP_32(ace->a_who);
39789Sahrens 		ace->a_access_mask = BSWAP_32(ace->a_access_mask);
40789Sahrens 		ace->a_flags = BSWAP_16(ace->a_flags);
41789Sahrens 		ace->a_type = BSWAP_16(ace->a_type);
42789Sahrens 	}
43789Sahrens }
44789Sahrens 
455331Samw /*
465331Samw  * swap ace_t and ace_oject_t
475331Samw  */
485331Samw void
495331Samw zfs_ace_byteswap(void *buf, size_t size, boolean_t zfs_layout)
505331Samw {
515331Samw 	caddr_t end;
525331Samw 	caddr_t ptr;
535331Samw 	zfs_ace_t *zacep;
545331Samw 	ace_t *acep;
555331Samw 	uint16_t entry_type;
565331Samw 	size_t entry_size;
575331Samw 	int ace_type;
585331Samw 
595331Samw 	end = (caddr_t)buf + size;
605331Samw 	ptr = buf;
615331Samw 
625331Samw 	while (ptr < end) {
635331Samw 		if (zfs_layout) {
64*8283STim.Haley@Sun.COM 			/*
65*8283STim.Haley@Sun.COM 			 * Avoid overrun.  Embedded aces can have one
66*8283STim.Haley@Sun.COM 			 * of several sizes.  We don't know exactly
67*8283STim.Haley@Sun.COM 			 * how many our present, only the size of the
68*8283STim.Haley@Sun.COM 			 * buffer containing them.  That size may be
69*8283STim.Haley@Sun.COM 			 * larger than needed to hold the aces
70*8283STim.Haley@Sun.COM 			 * present.  As long as we do not do any
71*8283STim.Haley@Sun.COM 			 * swapping beyond the end of our block we are
72*8283STim.Haley@Sun.COM 			 * okay.  It it safe to swap any non-ace data
73*8283STim.Haley@Sun.COM 			 * within the block since it is just zeros.
74*8283STim.Haley@Sun.COM 			 */
75*8283STim.Haley@Sun.COM 			if (ptr + sizeof (zfs_ace_hdr_t) > end) {
76*8283STim.Haley@Sun.COM 				break;
77*8283STim.Haley@Sun.COM 			}
785331Samw 			zacep = (zfs_ace_t *)ptr;
795331Samw 			zacep->z_hdr.z_access_mask =
805331Samw 			    BSWAP_32(zacep->z_hdr.z_access_mask);
815331Samw 			zacep->z_hdr.z_flags = BSWAP_16(zacep->z_hdr.z_flags);
825331Samw 			ace_type = zacep->z_hdr.z_type =
835331Samw 			    BSWAP_16(zacep->z_hdr.z_type);
845331Samw 			entry_type = zacep->z_hdr.z_flags & ACE_TYPE_FLAGS;
855331Samw 		} else {
86*8283STim.Haley@Sun.COM 			/* Overrun avoidance */
87*8283STim.Haley@Sun.COM 			if (ptr + sizeof (ace_t) > end) {
88*8283STim.Haley@Sun.COM 				break;
89*8283STim.Haley@Sun.COM 			}
905331Samw 			acep = (ace_t *)ptr;
915331Samw 			acep->a_access_mask = BSWAP_32(acep->a_access_mask);
925331Samw 			acep->a_flags = BSWAP_16(acep->a_flags);
935331Samw 			ace_type = acep->a_type = BSWAP_16(acep->a_type);
945331Samw 			acep->a_who = BSWAP_32(acep->a_who);
955331Samw 			entry_type = acep->a_flags & ACE_TYPE_FLAGS;
965331Samw 		}
975331Samw 		switch (entry_type) {
985331Samw 		case ACE_OWNER:
995331Samw 		case ACE_EVERYONE:
1005331Samw 		case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
1015331Samw 			entry_size = zfs_layout ?
1025331Samw 			    sizeof (zfs_ace_hdr_t) : sizeof (ace_t);
1035331Samw 			break;
1045331Samw 		case ACE_IDENTIFIER_GROUP:
1055331Samw 		default:
106*8283STim.Haley@Sun.COM 			/* Overrun avoidance */
1075331Samw 			if (zfs_layout) {
108*8283STim.Haley@Sun.COM 				if (ptr + sizeof (zfs_ace_t) <= end) {
109*8283STim.Haley@Sun.COM 					zacep->z_fuid = BSWAP_64(zacep->z_fuid);
110*8283STim.Haley@Sun.COM 				} else {
111*8283STim.Haley@Sun.COM 					entry_size = sizeof (zfs_ace_t);
112*8283STim.Haley@Sun.COM 					break;
113*8283STim.Haley@Sun.COM 				}
1145331Samw 			}
1155331Samw 			switch (ace_type) {
1165331Samw 			case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1175331Samw 			case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1185331Samw 			case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1195331Samw 			case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1205331Samw 				entry_size = zfs_layout ?
1215331Samw 				    sizeof (zfs_object_ace_t) :
1225331Samw 				    sizeof (ace_object_t);
1235331Samw 				break;
1245331Samw 			default:
1255331Samw 				entry_size = zfs_layout ? sizeof (zfs_ace_t) :
1265331Samw 				    sizeof (ace_t);
1275331Samw 				break;
1285331Samw 			}
1295331Samw 		}
1305331Samw 		ptr = ptr + entry_size;
1315331Samw 	}
1325331Samw }
1335331Samw 
134789Sahrens /* ARGSUSED */
135789Sahrens void
1365331Samw zfs_oldacl_byteswap(void *buf, size_t size)
137789Sahrens {
138789Sahrens 	int cnt;
139789Sahrens 
140789Sahrens 	/*
141789Sahrens 	 * Arggh, since we don't know how many ACEs are in
142789Sahrens 	 * the array, we have to swap the entire block
143789Sahrens 	 */
144789Sahrens 
145789Sahrens 	cnt = size / sizeof (ace_t);
146789Sahrens 
1475331Samw 	zfs_oldace_byteswap((ace_t *)buf, cnt);
1485331Samw }
1495331Samw 
1505331Samw /* ARGSUSED */
1515331Samw void
1525331Samw zfs_acl_byteswap(void *buf, size_t size)
1535331Samw {
1545331Samw 	zfs_ace_byteswap(buf, size, B_TRUE);
155789Sahrens }
156789Sahrens 
157789Sahrens void
158789Sahrens zfs_znode_byteswap(void *buf, size_t size)
159789Sahrens {
160789Sahrens 	znode_phys_t *zp = buf;
161789Sahrens 
162789Sahrens 	ASSERT(size >= sizeof (znode_phys_t));
163789Sahrens 
164789Sahrens 	zp->zp_crtime[0] = BSWAP_64(zp->zp_crtime[0]);
165789Sahrens 	zp->zp_crtime[1] = BSWAP_64(zp->zp_crtime[1]);
166789Sahrens 	zp->zp_atime[0] = BSWAP_64(zp->zp_atime[0]);
167789Sahrens 	zp->zp_atime[1] = BSWAP_64(zp->zp_atime[1]);
168789Sahrens 	zp->zp_mtime[0] = BSWAP_64(zp->zp_mtime[0]);
169789Sahrens 	zp->zp_mtime[1] = BSWAP_64(zp->zp_mtime[1]);
170789Sahrens 	zp->zp_ctime[0] = BSWAP_64(zp->zp_ctime[0]);
171789Sahrens 	zp->zp_ctime[1] = BSWAP_64(zp->zp_ctime[1]);
172789Sahrens 	zp->zp_gen = BSWAP_64(zp->zp_gen);
173789Sahrens 	zp->zp_mode = BSWAP_64(zp->zp_mode);
174789Sahrens 	zp->zp_size = BSWAP_64(zp->zp_size);
175789Sahrens 	zp->zp_parent = BSWAP_64(zp->zp_parent);
176789Sahrens 	zp->zp_links = BSWAP_64(zp->zp_links);
177789Sahrens 	zp->zp_xattr = BSWAP_64(zp->zp_xattr);
178789Sahrens 	zp->zp_rdev = BSWAP_64(zp->zp_rdev);
179789Sahrens 	zp->zp_flags = BSWAP_64(zp->zp_flags);
180789Sahrens 	zp->zp_uid = BSWAP_64(zp->zp_uid);
181789Sahrens 	zp->zp_gid = BSWAP_64(zp->zp_gid);
1825331Samw 	zp->zp_zap = BSWAP_64(zp->zp_zap);
183789Sahrens 	zp->zp_pad[0] = BSWAP_64(zp->zp_pad[0]);
184789Sahrens 	zp->zp_pad[1] = BSWAP_64(zp->zp_pad[1]);
185789Sahrens 	zp->zp_pad[2] = BSWAP_64(zp->zp_pad[2]);
186789Sahrens 
187789Sahrens 	zp->zp_acl.z_acl_extern_obj = BSWAP_64(zp->zp_acl.z_acl_extern_obj);
1885331Samw 	zp->zp_acl.z_acl_size = BSWAP_32(zp->zp_acl.z_acl_size);
189789Sahrens 	zp->zp_acl.z_acl_version = BSWAP_16(zp->zp_acl.z_acl_version);
1905331Samw 	zp->zp_acl.z_acl_count = BSWAP_16(zp->zp_acl.z_acl_count);
1915331Samw 	if (zp->zp_acl.z_acl_version == ZFS_ACL_VERSION) {
1925331Samw 		zfs_acl_byteswap((void *)&zp->zp_acl.z_ace_data[0],
1935331Samw 		    ZFS_ACE_SPACE);
194*8283STim.Haley@Sun.COM 	} else {
1955331Samw 		zfs_oldace_byteswap((ace_t *)&zp->zp_acl.z_ace_data[0],
1965331Samw 		    ACE_SLOT_CNT);
197*8283STim.Haley@Sun.COM 	}
198789Sahrens }
199