Home | History | Annotate | Line # | Download | only in ffs
ffs_bswap.c revision 1.25
      1 /*	$NetBSD: ffs_bswap.c,v 1.25 2003/12/31 18:40:23 dbj Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  */
     32 
     33 #if HAVE_NBTOOL_CONFIG_H
     34 #include "nbtool_config.h"
     35 #endif
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: ffs_bswap.c,v 1.25 2003/12/31 18:40:23 dbj Exp $");
     39 
     40 #include <sys/param.h>
     41 #if defined(_KERNEL)
     42 #include <sys/systm.h>
     43 #endif
     44 
     45 #include <ufs/ufs/dinode.h>
     46 #include <ufs/ufs/ufs_bswap.h>
     47 #include <ufs/ffs/fs.h>
     48 #include <ufs/ffs/ffs_extern.h>
     49 
     50 #if !defined(_KERNEL)
     51 #include <stddef.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #define panic(x)	printf("%s\n", (x)), abort()
     56 #endif
     57 
     58 void
     59 ffs_sb_swap(struct fs *o, struct fs *n)
     60 {
     61 	int i;
     62 	u_int32_t *o32, *n32;
     63 
     64 	/*
     65 	 * In order to avoid a lot of lines, as the first N fields (52)
     66 	 * of the superblock up to fs_fmod are u_int32_t, we just loop
     67 	 * here to convert them.
     68 	 */
     69 	o32 = (u_int32_t *)o;
     70 	n32 = (u_int32_t *)n;
     71 	for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
     72 		n32[i] = bswap32(o32[i]);
     73 
     74 	n->fs_swuid = bswap64(o->fs_swuid);
     75 	n->fs_cgrotor = bswap32(o->fs_cgrotor); /* Unused */
     76 	n->fs_old_cpc = bswap32(o->fs_old_cpc);
     77 
     78 	/* These fields overlap with a possible location for the
     79 	 * historic FS_DYNAMICPOSTBLFMT postbl table, and with the
     80 	 * first half of the historic FS_42POSTBLFMT postbl table.
     81 	 */
     82 	n->fs_maxbsize = bswap32(o->fs_maxbsize);
     83 	n->fs_sblockloc = bswap64(o->fs_sblockloc);
     84 	ffs_csumtotal_swap(&o->fs_cstotal, &n->fs_cstotal);
     85 	n->fs_time = bswap64(o->fs_time);
     86 	n->fs_size = bswap64(o->fs_size);
     87 	n->fs_dsize = bswap64(o->fs_dsize);
     88 	n->fs_csaddr = bswap64(o->fs_csaddr);
     89 	n->fs_pendingblocks = bswap64(o->fs_pendingblocks);
     90 	n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
     91 
     92 	/* These fields overlap with the second half of the
     93 	 * historic FS_42POSTBLFMT postbl table
     94 	 */
     95 	/* fs_snapinum[20] - ignore for now */
     96 	n->fs_avgfilesize = bswap32(o->fs_avgfilesize);
     97 	n->fs_avgfpdir = bswap32(o->fs_avgfpdir);
     98 	/* fs_sparecon[28] - ignore for now */
     99 	n->fs_flags = bswap32(o->fs_flags);
    100 	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
    101 	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
    102 	n->fs_old_inodefmt = bswap32(o->fs_old_inodefmt);
    103 	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
    104 	n->fs_qbmask = bswap64(o->fs_qbmask);
    105 	n->fs_qfmask = bswap64(o->fs_qfmask);
    106 	n->fs_state = bswap32(o->fs_state);
    107 	n->fs_old_postblformat = bswap32(o->fs_old_postblformat);
    108 	n->fs_old_nrpos = bswap32(o->fs_old_nrpos);
    109 	n->fs_old_postbloff = bswap32(o->fs_old_postbloff);
    110 	n->fs_old_rotbloff = bswap32(o->fs_old_rotbloff);
    111 
    112 	n->fs_magic = bswap32(o->fs_magic);
    113 }
    114 
    115 void
    116 ffs_dinode1_swap(struct ufs1_dinode *o, struct ufs1_dinode *n)
    117 {
    118 
    119 	n->di_mode = bswap16(o->di_mode);
    120 	n->di_nlink = bswap16(o->di_nlink);
    121 	n->di_u.oldids[0] = bswap16(o->di_u.oldids[0]);
    122 	n->di_u.oldids[1] = bswap16(o->di_u.oldids[1]);
    123 	n->di_size = bswap64(o->di_size);
    124 	n->di_atime = bswap32(o->di_atime);
    125 	n->di_atimensec = bswap32(o->di_atimensec);
    126 	n->di_mtime = bswap32(o->di_mtime);
    127 	n->di_mtimensec = bswap32(o->di_mtimensec);
    128 	n->di_ctime = bswap32(o->di_ctime);
    129 	n->di_ctimensec = bswap32(o->di_ctimensec);
    130 	memcpy(n->di_db, o->di_db, (NDADDR + NIADDR) * sizeof(u_int32_t));
    131 	n->di_flags = bswap32(o->di_flags);
    132 	n->di_blocks = bswap32(o->di_blocks);
    133 	n->di_gen = bswap32(o->di_gen);
    134 	n->di_uid = bswap32(o->di_uid);
    135 	n->di_gid = bswap32(o->di_gid);
    136 }
    137 
    138 void
    139 ffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n)
    140 {
    141 	n->di_mode = bswap16(o->di_mode);
    142 	n->di_nlink = bswap16(o->di_nlink);
    143 	n->di_uid = bswap32(o->di_uid);
    144 	n->di_gid = bswap32(o->di_gid);
    145 	n->di_blksize = bswap32(o->di_blksize);
    146 	n->di_size = bswap64(o->di_size);
    147 	n->di_blocks = bswap64(o->di_blocks);
    148 	n->di_atime = bswap64(o->di_atime);
    149 	n->di_atimensec = bswap32(o->di_atimensec);
    150 	n->di_mtime = bswap64(o->di_mtime);
    151 	n->di_mtimensec = bswap32(o->di_mtimensec);
    152 	n->di_ctime = bswap64(o->di_ctime);
    153 	n->di_ctimensec = bswap32(o->di_ctimensec);
    154 	n->di_birthtime = bswap64(o->di_ctime);
    155 	n->di_birthnsec = bswap32(o->di_ctimensec);
    156 	n->di_gen = bswap32(o->di_gen);
    157 	n->di_kernflags = bswap32(o->di_kernflags);
    158 	n->di_flags = bswap32(o->di_flags);
    159 	n->di_extsize = bswap32(o->di_extsize);
    160 	memcpy(n->di_extb, o->di_extb, (NXADDR + NDADDR + NIADDR) * 8);
    161 }
    162 
    163 void
    164 ffs_csum_swap(struct csum *o, struct csum *n, int size)
    165 {
    166 	int i;
    167 	u_int32_t *oint, *nint;
    168 
    169 	oint = (u_int32_t*)o;
    170 	nint = (u_int32_t*)n;
    171 
    172 	for (i = 0; i < size / sizeof(u_int32_t); i++)
    173 		nint[i] = bswap32(oint[i]);
    174 }
    175 
    176 void
    177 ffs_csumtotal_swap(struct csum_total *o, struct csum_total *n)
    178 {
    179 	n->cs_ndir = bswap64(o->cs_ndir);
    180 	n->cs_nbfree = bswap64(o->cs_nbfree);
    181 	n->cs_nifree = bswap64(o->cs_nifree);
    182 	n->cs_nffree = bswap64(o->cs_nffree);
    183 	n->cs_numclusters = bswap64(o->cs_numclusters);
    184 }
    185 
    186 /*
    187  * Note that ffs_cg_swap may be called with o == n.
    188  */
    189 void
    190 ffs_cg_swap(struct cg *o, struct cg *n, struct fs *fs)
    191 {
    192 	int i;
    193 	u_int32_t *n32, *o32;
    194 	u_int16_t *n16, *o16;
    195 	int32_t btotoff, boff, clustersumoff;
    196 
    197 	n->cg_firstfield = bswap32(o->cg_firstfield);
    198 	n->cg_magic = bswap32(o->cg_magic);
    199 	n->cg_old_time = bswap32(o->cg_old_time);
    200 	n->cg_cgx = bswap32(o->cg_cgx);
    201 	n->cg_old_ncyl = bswap16(o->cg_old_ncyl);
    202 	n->cg_old_niblk = bswap16(o->cg_old_niblk);
    203 	n->cg_ndblk = bswap32(o->cg_ndblk);
    204 	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
    205 	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
    206 	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
    207 	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
    208 	n->cg_rotor = bswap32(o->cg_rotor);
    209 	n->cg_frotor = bswap32(o->cg_frotor);
    210 	n->cg_irotor = bswap32(o->cg_irotor);
    211 	for (i = 0; i < MAXFRAG; i++)
    212 		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
    213 
    214 	/* XXX This check should probably really just be
    215 	 * (n->cg_magic != CG_MAGIC) -- dbj
    216 	 */
    217 	if ((fs->fs_magic != FS_UFS2_MAGIC) &&
    218 			(fs->fs_old_postblformat == FS_42POSTBLFMT)) { /* old format */
    219 		struct ocg *on, *oo;
    220 		int j;
    221 		on = (struct ocg *)n;
    222 		oo = (struct ocg *)o;
    223 
    224 		for (i = 0; i < 32; i++) {
    225 			on->cg_btot[i] = bswap32(oo->cg_btot[i]);
    226 			for (j = 0; j < 8; j++)
    227 				on->cg_b[i][j] = bswap16(oo->cg_b[i][j]);
    228 		}
    229 		memmove(on->cg_iused, oo->cg_iused, 256);
    230 		on->cg_magic = bswap32(oo->cg_magic);
    231 	} else {  /* new format */
    232 
    233 		n->cg_old_btotoff = bswap32(o->cg_old_btotoff);
    234 		n->cg_old_boff = bswap32(o->cg_old_boff);
    235 		n->cg_iusedoff = bswap32(o->cg_iusedoff);
    236 		n->cg_freeoff = bswap32(o->cg_freeoff);
    237 		n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
    238 		n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
    239 		n->cg_clusteroff = bswap32(o->cg_clusteroff);
    240 		n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
    241 		n->cg_niblk = bswap32(o->cg_niblk);
    242 		n->cg_initediblk = bswap32(o->cg_initediblk);
    243 		n->cg_time = bswap64(o->cg_time);
    244 
    245 		if (fs->fs_magic == FS_UFS2_MAGIC)
    246 			return;
    247 
    248 		if (n->cg_magic == CG_MAGIC) {
    249 			btotoff = n->cg_old_btotoff;
    250 			boff = n->cg_old_boff;
    251 			clustersumoff = n->cg_clustersumoff;
    252 		} else {
    253 			/* XXX this shouldn't happen -- dbj */
    254 			btotoff = bswap32(n->cg_old_btotoff);
    255 			boff = bswap32(n->cg_old_boff);
    256 			clustersumoff = bswap32(n->cg_clustersumoff);
    257 		}
    258 		n32 = (u_int32_t *)((u_int8_t *)n + btotoff);
    259 		o32 = (u_int32_t *)((u_int8_t *)o + btotoff);
    260 		n16 = (u_int16_t *)((u_int8_t *)n + boff);
    261 		o16 = (u_int16_t *)((u_int8_t *)o + boff);
    262 
    263 		for (i = 0; i < fs->fs_old_cpg; i++)
    264 			n32[i] = bswap32(o32[i]);
    265 
    266 		for (i = 0; i < fs->fs_old_cpg * fs->fs_old_nrpos; i++)
    267 			n16[i] = bswap16(o16[i]);
    268 
    269 		n32 = (u_int32_t *)((u_int8_t *)n + clustersumoff);
    270 		o32 = (u_int32_t *)((u_int8_t *)o + clustersumoff);
    271 		for (i = 1; i < fs->fs_contigsumsize + 1; i++)
    272 			n32[i] = bswap32(o32[i]);
    273 	}
    274 }
    275