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