ffs_bswap.c revision 185222
1333149Serj/* $NetBSD: ffs_bswap.c,v 1.28 2004/05/25 14:54:59 hannken Exp $ */ 2333149Serj 3349163Serj/* 4333149Serj * Copyright (c) 1998 Manuel Bouyer. 5333149Serj * 6333149Serj * Redistribution and use in source and binary forms, with or without 7333149Serj * modification, are permitted provided that the following conditions 8333149Serj * are met: 9333149Serj * 1. Redistributions of source code must retain the above copyright 10333149Serj * notice, this list of conditions and the following disclaimer. 11333149Serj * 2. Redistributions in binary form must reproduce the above copyright 12333149Serj * notice, this list of conditions and the following disclaimer in the 13333149Serj * documentation and/or other materials provided with the distribution. 14333149Serj * 3. All advertising materials mentioning features or use of this software 15333149Serj * must display the following acknowledgement: 16333149Serj * This product includes software developed by Manuel Bouyer. 17333149Serj * 4. The name of the author may not be used to endorse or promote products 18333149Serj * derived from this software without specific prior written permission. 19333149Serj * 20333149Serj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21333149Serj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22333149Serj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23333149Serj * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24333149Serj * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25333149Serj * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26333149Serj * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27333149Serj * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28333149Serj * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29333149Serj * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30333149Serj * 31333149Serj */ 32333149Serj 33333149Serj#if HAVE_NBTOOL_CONFIG_H 34333149Serj#include "nbtool_config.h" 35349163Serj#endif 36333149Serj 37333149Serj#include <sys/cdefs.h> 38333149Serj__KERNEL_RCSID(0, "$NetBSD: ffs_bswap.c,v 1.28 2004/05/25 14:54:59 hannken Exp $"); 39333149Serj 40333149Serj#include <sys/param.h> 41333149Serj#if defined(_KERNEL) 42333149Serj#include <sys/systm.h> 43333149Serj#endif 44333149Serj 45333149Serj#include <ufs/ufs/dinode.h> 46333149Serj#include <ufs/ufs/ufs_bswap.h> 47333149Serj#include <ufs/ffs/fs.h> 48333149Serj#include <ufs/ffs/ffs_extern.h> 49333149Serj 50333149Serj#if !defined(_KERNEL) 51333149Serj#include <stddef.h> 52333149Serj#include <stdio.h> 53333149Serj#include <stdlib.h> 54333149Serj#include <string.h> 55333149Serj#define panic(x) printf("%s\n", (x)), abort() 56333149Serj#endif 57333149Serj 58333149Serjvoid 59333149Serjffs_sb_swap(struct fs *o, struct fs *n) 60333149Serj{ 61333149Serj int i; 62333149Serj u_int32_t *o32, *n32; 63333149Serj 64333149Serj /* 65333149Serj * In order to avoid a lot of lines, as the first N fields (52) 66333149Serj * of the superblock up to fs_fmod are u_int32_t, we just loop 67333149Serj * here to convert them. 68333149Serj */ 69333149Serj o32 = (u_int32_t *)o; 70333149Serj n32 = (u_int32_t *)n; 71333149Serj for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++) 72333149Serj n32[i] = bswap32(o32[i]); 73333149Serj 74349163Serj n->fs_swuid = bswap64(o->fs_swuid); 75349163Serj n->fs_cgrotor = bswap32(o->fs_cgrotor); /* Unused */ 76333149Serj n->fs_old_cpc = bswap32(o->fs_old_cpc); 77333149Serj 78333149Serj /* These fields overlap with a possible location for the 79349163Serj * historic FS_DYNAMICPOSTBLFMT postbl table, and with the 80349163Serj * first half of the historic FS_42POSTBLFMT postbl table. 81333149Serj */ 82333149Serj n->fs_maxbsize = bswap32(o->fs_maxbsize); 83349163Serj n->fs_sblockloc = bswap64(o->fs_sblockloc); 84349163Serj ffs_csumtotal_swap(&o->fs_cstotal, &n->fs_cstotal); 85349163Serj n->fs_time = bswap64(o->fs_time); 86349163Serj n->fs_size = bswap64(o->fs_size); 87349163Serj n->fs_dsize = bswap64(o->fs_dsize); 88333149Serj n->fs_csaddr = bswap64(o->fs_csaddr); 89333149Serj n->fs_pendingblocks = bswap64(o->fs_pendingblocks); 90333149Serj n->fs_pendinginodes = bswap32(o->fs_pendinginodes); 91333149Serj 92333149Serj /* These fields overlap with the second half of the 93333149Serj * historic FS_42POSTBLFMT postbl table 94349163Serj */ 95333149Serj for (i = 0; i < FSMAXSNAP; i++) 96333149Serj n->fs_snapinum[i] = bswap32(o->fs_snapinum[i]); 97333149Serj n->fs_avgfilesize = bswap32(o->fs_avgfilesize); 98333149Serj n->fs_avgfpdir = bswap32(o->fs_avgfpdir); 99333149Serj /* fs_sparecon[28] - ignore for now */ 100333149Serj n->fs_flags = bswap32(o->fs_flags); 101333149Serj n->fs_contigsumsize = bswap32(o->fs_contigsumsize); 102333149Serj n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen); 103333149Serj n->fs_old_inodefmt = bswap32(o->fs_old_inodefmt); 104349163Serj n->fs_maxfilesize = bswap64(o->fs_maxfilesize); 105349163Serj n->fs_qbmask = bswap64(o->fs_qbmask); 106333149Serj n->fs_qfmask = bswap64(o->fs_qfmask); 107333149Serj n->fs_state = bswap32(o->fs_state); 108333149Serj n->fs_old_postblformat = bswap32(o->fs_old_postblformat); 109333149Serj n->fs_old_nrpos = bswap32(o->fs_old_nrpos); 110333149Serj n->fs_old_postbloff = bswap32(o->fs_old_postbloff); 111333149Serj n->fs_old_rotbloff = bswap32(o->fs_old_rotbloff); 112333149Serj 113333149Serj n->fs_magic = bswap32(o->fs_magic); 114333149Serj} 115333149Serj 116333149Serjvoid 117333149Serjffs_dinode1_swap(struct ufs1_dinode *o, struct ufs1_dinode *n) 118333149Serj{ 119333149Serj 120333149Serj n->di_mode = bswap16(o->di_mode); 121333149Serj n->di_nlink = bswap16(o->di_nlink); 122333149Serj n->di_u.oldids[0] = bswap16(o->di_u.oldids[0]); 123333149Serj n->di_u.oldids[1] = bswap16(o->di_u.oldids[1]); 124333149Serj n->di_size = bswap64(o->di_size); 125333149Serj n->di_atime = bswap32(o->di_atime); 126333149Serj n->di_atimensec = bswap32(o->di_atimensec); 127333149Serj n->di_mtime = bswap32(o->di_mtime); 128333149Serj n->di_mtimensec = bswap32(o->di_mtimensec); 129333149Serj n->di_ctime = bswap32(o->di_ctime); 130333149Serj n->di_ctimensec = bswap32(o->di_ctimensec); 131333149Serj memcpy(n->di_db, o->di_db, (NDADDR + NIADDR) * sizeof(u_int32_t)); 132333149Serj n->di_flags = bswap32(o->di_flags); 133333149Serj n->di_blocks = bswap32(o->di_blocks); 134333149Serj n->di_gen = bswap32(o->di_gen); 135333149Serj n->di_uid = bswap32(o->di_uid); 136333149Serj n->di_gid = bswap32(o->di_gid); 137333149Serj} 138333149Serj 139333149Serjvoid 140333149Serjffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n) 141333149Serj{ 142333149Serj n->di_mode = bswap16(o->di_mode); 143333149Serj n->di_nlink = bswap16(o->di_nlink); 144333149Serj n->di_uid = bswap32(o->di_uid); 145333149Serj n->di_gid = bswap32(o->di_gid); 146333149Serj n->di_blksize = bswap32(o->di_blksize); 147333149Serj n->di_size = bswap64(o->di_size); 148333149Serj n->di_blocks = bswap64(o->di_blocks); 149349163Serj n->di_atime = bswap64(o->di_atime); 150333149Serj n->di_atimensec = bswap32(o->di_atimensec); 151333149Serj n->di_mtime = bswap64(o->di_mtime); 152333149Serj n->di_mtimensec = bswap32(o->di_mtimensec); 153333149Serj n->di_ctime = bswap64(o->di_ctime); 154333149Serj n->di_ctimensec = bswap32(o->di_ctimensec); 155333149Serj n->di_birthtime = bswap64(o->di_ctime); 156333149Serj n->di_birthnsec = bswap32(o->di_ctimensec); 157333149Serj n->di_gen = bswap32(o->di_gen); 158333149Serj n->di_kernflags = bswap32(o->di_kernflags); 159333149Serj n->di_flags = bswap32(o->di_flags); 160349163Serj n->di_extsize = bswap32(o->di_extsize); 161349163Serj memcpy(n->di_extb, o->di_extb, (NXADDR + NDADDR + NIADDR) * 8); 162349163Serj} 163349163Serj 164333149Serjvoid 165333149Serjffs_csum_swap(struct csum *o, struct csum *n, int size) 166349163Serj{ 167333149Serj int i; 168349163Serj u_int32_t *oint, *nint; 169349163Serj 170333149Serj oint = (u_int32_t*)o; 171333149Serj nint = (u_int32_t*)n; 172349163Serj 173349163Serj for (i = 0; i < size / sizeof(u_int32_t); i++) 174349163Serj nint[i] = bswap32(oint[i]); 175333149Serj} 176333149Serj 177333149Serjvoid 178333149Serjffs_csumtotal_swap(struct csum_total *o, struct csum_total *n) 179333149Serj{ 180333149Serj n->cs_ndir = bswap64(o->cs_ndir); 181333149Serj n->cs_nbfree = bswap64(o->cs_nbfree); 182333149Serj n->cs_nifree = bswap64(o->cs_nifree); 183333149Serj n->cs_nffree = bswap64(o->cs_nffree); 184333149Serj} 185333149Serj 186333149Serj/* 187333149Serj * Note that ffs_cg_swap may be called with o == n. 188333149Serj */ 189349163Serjvoid 190333149Serjffs_cg_swap(struct cg *o, struct cg *n, struct fs *fs) 191333149Serj{ 192333149Serj int i; 193333149Serj u_int32_t *n32, *o32; 194333149Serj u_int16_t *n16, *o16; 195333149Serj int32_t btotoff, boff, clustersumoff; 196333149Serj 197333149Serj n->cg_firstfield = bswap32(o->cg_firstfield); 198333149Serj n->cg_magic = bswap32(o->cg_magic); 199333149Serj n->cg_old_time = bswap32(o->cg_old_time); 200333149Serj n->cg_cgx = bswap32(o->cg_cgx); 201333149Serj n->cg_old_ncyl = bswap16(o->cg_old_ncyl); 202333149Serj n->cg_old_niblk = bswap16(o->cg_old_niblk); 203333149Serj n->cg_ndblk = bswap32(o->cg_ndblk); 204333149Serj n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir); 205333149Serj n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree); 206333149Serj n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree); 207333149Serj n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree); 208333149Serj n->cg_rotor = bswap32(o->cg_rotor); 209333149Serj n->cg_frotor = bswap32(o->cg_frotor); 210333149Serj n->cg_irotor = bswap32(o->cg_irotor); 211333149Serj for (i = 0; i < MAXFRAG; i++) 212333149Serj n->cg_frsum[i] = bswap32(o->cg_frsum[i]); 213333149Serj 214333149Serj if ((fs->fs_magic != FS_UFS2_MAGIC) && 215333149Serj (fs->fs_old_postblformat == FS_42POSTBLFMT)) { /* old format */ 216333149Serj struct ocg *on, *oo; 217333149Serj int j; 218333149Serj on = (struct ocg *)n; 219333149Serj oo = (struct ocg *)o; 220333149Serj 221333149Serj for (i = 0; i < 32; i++) { 222333149Serj on->cg_btot[i] = bswap32(oo->cg_btot[i]); 223333149Serj for (j = 0; j < 8; j++) 224333149Serj on->cg_b[i][j] = bswap16(oo->cg_b[i][j]); 225333149Serj } 226333149Serj memmove(on->cg_iused, oo->cg_iused, 256); 227333149Serj on->cg_magic = bswap32(oo->cg_magic); 228333149Serj } else { /* new format */ 229333149Serj 230333149Serj n->cg_old_btotoff = bswap32(o->cg_old_btotoff); 231333149Serj n->cg_old_boff = bswap32(o->cg_old_boff); 232333149Serj n->cg_iusedoff = bswap32(o->cg_iusedoff); 233333149Serj n->cg_freeoff = bswap32(o->cg_freeoff); 234333149Serj n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff); 235333149Serj n->cg_clustersumoff = bswap32(o->cg_clustersumoff); 236333149Serj n->cg_clusteroff = bswap32(o->cg_clusteroff); 237333149Serj n->cg_nclusterblks = bswap32(o->cg_nclusterblks); 238333149Serj n->cg_niblk = bswap32(o->cg_niblk); 239333149Serj n->cg_initediblk = bswap32(o->cg_initediblk); 240333149Serj n->cg_time = bswap64(o->cg_time); 241333149Serj 242333149Serj if (fs->fs_magic == FS_UFS2_MAGIC) 243333149Serj return; 244333149Serj 245333149Serj if (n->cg_magic == CG_MAGIC) { 246333149Serj btotoff = n->cg_old_btotoff; 247333149Serj boff = n->cg_old_boff; 248333149Serj clustersumoff = n->cg_clustersumoff; 249333149Serj } else { 250333149Serj btotoff = bswap32(n->cg_old_btotoff); 251333149Serj boff = bswap32(n->cg_old_boff); 252333149Serj clustersumoff = bswap32(n->cg_clustersumoff); 253333149Serj } 254333149Serj n32 = (u_int32_t *)((u_int8_t *)n + btotoff); 255333149Serj o32 = (u_int32_t *)((u_int8_t *)o + btotoff); 256333149Serj n16 = (u_int16_t *)((u_int8_t *)n + boff); 257333149Serj o16 = (u_int16_t *)((u_int8_t *)o + boff); 258333149Serj 259333149Serj for (i = 0; i < fs->fs_old_cpg; i++) 260333149Serj n32[i] = bswap32(o32[i]); 261333149Serj 262333149Serj for (i = 0; i < fs->fs_old_cpg * fs->fs_old_nrpos; i++) 263333149Serj n16[i] = bswap16(o16[i]); 264333149Serj 265333149Serj n32 = (u_int32_t *)((u_int8_t *)n + clustersumoff); 266333149Serj o32 = (u_int32_t *)((u_int8_t *)o + clustersumoff); 267349163Serj for (i = 1; i < fs->fs_contigsumsize + 1; i++) 268333149Serj n32[i] = bswap32(o32[i]); 269333149Serj } 270333149Serj} 271333149Serj