FreeBSD ZFS
The Zettabyte File System

zfs_byteswap.c

Go to the documentation of this file.
00001 /*
00002  * CDDL HEADER START
00003  *
00004  * The contents of this file are subject to the terms of the
00005  * Common Development and Distribution License (the "License").
00006  * You may not use this file except in compliance with the License.
00007  *
00008  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
00009  * or http://www.opensolaris.org/os/licensing.
00010  * See the License for the specific language governing permissions
00011  * and limitations under the License.
00012  *
00013  * When distributing Covered Code, include this CDDL HEADER in each
00014  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
00015  * If applicable, add the following below this CDDL HEADER, with the
00016  * fields enclosed by brackets "[]" replaced with your own identifying
00017  * information: Portions Copyright [yyyy] [name of copyright owner]
00018  *
00019  * CDDL HEADER END
00020  */
00021 /*
00022  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
00023  * Use is subject to license terms.
00024  */
00025 
00026 #include <sys/zfs_context.h>
00027 #include <sys/vfs.h>
00028 #include <sys/fs/zfs.h>
00029 #include <sys/zfs_znode.h>
00030 #include <sys/zfs_sa.h>
00031 #include <sys/zfs_acl.h>
00032 
00033 void
00034 zfs_oldace_byteswap(ace_t *ace, int ace_cnt)
00035 {
00036         int i;
00037 
00038         for (i = 0; i != ace_cnt; i++, ace++) {
00039                 ace->a_who = BSWAP_32(ace->a_who);
00040                 ace->a_access_mask = BSWAP_32(ace->a_access_mask);
00041                 ace->a_flags = BSWAP_16(ace->a_flags);
00042                 ace->a_type = BSWAP_16(ace->a_type);
00043         }
00044 }
00045 
00049 void
00050 zfs_ace_byteswap(void *buf, size_t size, boolean_t zfs_layout)
00051 {
00052         caddr_t end;
00053         caddr_t ptr;
00054         zfs_ace_t *zacep;
00055         ace_t *acep;
00056         uint16_t entry_type;
00057         size_t entry_size;
00058         int ace_type;
00059 
00060         end = (caddr_t)buf + size;
00061         ptr = buf;
00062 
00063         while (ptr < end) {
00064                 if (zfs_layout) {
00065                         /*
00066                          * Avoid overrun.  Embedded aces can have one
00067                          * of several sizes.  We don't know exactly
00068                          * how many our present, only the size of the
00069                          * buffer containing them.  That size may be
00070                          * larger than needed to hold the aces
00071                          * present.  As long as we do not do any
00072                          * swapping beyond the end of our block we are
00073                          * okay.  It it safe to swap any non-ace data
00074                          * within the block since it is just zeros.
00075                          */
00076                         if (ptr + sizeof (zfs_ace_hdr_t) > end) {
00077                                 break;
00078                         }
00079                         zacep = (zfs_ace_t *)ptr;
00080                         zacep->z_hdr.z_access_mask =
00081                             BSWAP_32(zacep->z_hdr.z_access_mask);
00082                         zacep->z_hdr.z_flags = BSWAP_16(zacep->z_hdr.z_flags);
00083                         ace_type = zacep->z_hdr.z_type =
00084                             BSWAP_16(zacep->z_hdr.z_type);
00085                         entry_type = zacep->z_hdr.z_flags & ACE_TYPE_FLAGS;
00086                 } else {
00087                         /* Overrun avoidance */
00088                         if (ptr + sizeof (ace_t) > end) {
00089                                 break;
00090                         }
00091                         acep = (ace_t *)ptr;
00092                         acep->a_access_mask = BSWAP_32(acep->a_access_mask);
00093                         acep->a_flags = BSWAP_16(acep->a_flags);
00094                         ace_type = acep->a_type = BSWAP_16(acep->a_type);
00095                         acep->a_who = BSWAP_32(acep->a_who);
00096                         entry_type = acep->a_flags & ACE_TYPE_FLAGS;
00097                 }
00098                 switch (entry_type) {
00099                 case ACE_OWNER:
00100                 case ACE_EVERYONE:
00101                 case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
00102                         entry_size = zfs_layout ?
00103                             sizeof (zfs_ace_hdr_t) : sizeof (ace_t);
00104                         break;
00105                 case ACE_IDENTIFIER_GROUP:
00106                 default:
00107                         /* Overrun avoidance */
00108                         if (zfs_layout) {
00109                                 if (ptr + sizeof (zfs_ace_t) <= end) {
00110                                         zacep->z_fuid = BSWAP_64(zacep->z_fuid);
00111                                 } else {
00112                                         entry_size = sizeof (zfs_ace_t);
00113                                         break;
00114                                 }
00115                         }
00116                         switch (ace_type) {
00117                         case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
00118                         case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
00119                         case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
00120                         case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
00121                                 entry_size = zfs_layout ?
00122                                     sizeof (zfs_object_ace_t) :
00123                                     sizeof (ace_object_t);
00124                                 break;
00125                         default:
00126                                 entry_size = zfs_layout ? sizeof (zfs_ace_t) :
00127                                     sizeof (ace_t);
00128                                 break;
00129                         }
00130                 }
00131                 ptr = ptr + entry_size;
00132         }
00133 }
00134 
00135 /* ARGSUSED */
00136 void
00137 zfs_oldacl_byteswap(void *buf, size_t size)
00138 {
00139         int cnt;
00140 
00141         /*
00142          * Arggh, since we don't know how many ACEs are in
00143          * the array, we have to swap the entire block
00144          */
00145 
00146         cnt = size / sizeof (ace_t);
00147 
00148         zfs_oldace_byteswap((ace_t *)buf, cnt);
00149 }
00150 
00151 /* ARGSUSED */
00152 void
00153 zfs_acl_byteswap(void *buf, size_t size)
00154 {
00155         zfs_ace_byteswap(buf, size, B_TRUE);
00156 }
00157 
00158 void
00159 zfs_znode_byteswap(void *buf, size_t size)
00160 {
00161         znode_phys_t *zp = buf;
00162 
00163         ASSERT(size >= sizeof (znode_phys_t));
00164 
00165         zp->zp_crtime[0] = BSWAP_64(zp->zp_crtime[0]);
00166         zp->zp_crtime[1] = BSWAP_64(zp->zp_crtime[1]);
00167         zp->zp_atime[0] = BSWAP_64(zp->zp_atime[0]);
00168         zp->zp_atime[1] = BSWAP_64(zp->zp_atime[1]);
00169         zp->zp_mtime[0] = BSWAP_64(zp->zp_mtime[0]);
00170         zp->zp_mtime[1] = BSWAP_64(zp->zp_mtime[1]);
00171         zp->zp_ctime[0] = BSWAP_64(zp->zp_ctime[0]);
00172         zp->zp_ctime[1] = BSWAP_64(zp->zp_ctime[1]);
00173         zp->zp_gen = BSWAP_64(zp->zp_gen);
00174         zp->zp_mode = BSWAP_64(zp->zp_mode);
00175         zp->zp_size = BSWAP_64(zp->zp_size);
00176         zp->zp_parent = BSWAP_64(zp->zp_parent);
00177         zp->zp_links = BSWAP_64(zp->zp_links);
00178         zp->zp_xattr = BSWAP_64(zp->zp_xattr);
00179         zp->zp_rdev = BSWAP_64(zp->zp_rdev);
00180         zp->zp_flags = BSWAP_64(zp->zp_flags);
00181         zp->zp_uid = BSWAP_64(zp->zp_uid);
00182         zp->zp_gid = BSWAP_64(zp->zp_gid);
00183         zp->zp_zap = BSWAP_64(zp->zp_zap);
00184         zp->zp_pad[0] = BSWAP_64(zp->zp_pad[0]);
00185         zp->zp_pad[1] = BSWAP_64(zp->zp_pad[1]);
00186         zp->zp_pad[2] = BSWAP_64(zp->zp_pad[2]);
00187 
00188         zp->zp_acl.z_acl_extern_obj = BSWAP_64(zp->zp_acl.z_acl_extern_obj);
00189         zp->zp_acl.z_acl_size = BSWAP_32(zp->zp_acl.z_acl_size);
00190         zp->zp_acl.z_acl_version = BSWAP_16(zp->zp_acl.z_acl_version);
00191         zp->zp_acl.z_acl_count = BSWAP_16(zp->zp_acl.z_acl_count);
00192         if (zp->zp_acl.z_acl_version == ZFS_ACL_VERSION) {
00193                 zfs_acl_byteswap((void *)&zp->zp_acl.z_ace_data[0],
00194                     ZFS_ACE_SPACE);
00195         } else {
00196                 zfs_oldace_byteswap((ace_t *)&zp->zp_acl.z_ace_data[0],
00197                     ACE_SLOT_CNT);
00198         }
00199 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines