Home | History | Annotate | Line # | Download | only in ffs
ffs_bswap.c revision 1.21
      1 /*	$NetBSD: ffs_bswap.c,v 1.21 2003/10/05 17:48:50 bouyer 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 #include <sys/cdefs.h>
     34 #if defined(__KERNEL_RCSID)
     35 __KERNEL_RCSID(0, "$NetBSD: ffs_bswap.c,v 1.21 2003/10/05 17:48:50 bouyer Exp $");
     36 #endif
     37 
     38 #if HAVE_CONFIG_H
     39 #include "config.h"
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #if defined(_KERNEL)
     44 #include <sys/systm.h>
     45 #endif
     46 
     47 #include <ufs/ufs/dinode.h>
     48 #include <ufs/ufs/ufs_bswap.h>
     49 #include <ufs/ffs/fs.h>
     50 #include <ufs/ffs/ffs_extern.h>
     51 
     52 #if !defined(_KERNEL)
     53 #include <stddef.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #define panic(x)	printf("%s\n", (x)), abort()
     58 #endif
     59 
     60 void
     61 ffs_sb_swap(struct fs *o, struct fs *n)
     62 {
     63 	int i;
     64 	u_int32_t *o32, *n32;
     65 
     66 	/*
     67 	 * In order to avoid a lot of lines, as the first N fields (52)
     68 	 * of the superblock up to fs_fmod are u_int32_t, we just loop
     69 	 * here to convert them.
     70 	 */
     71 	o32 = (u_int32_t *)o;
     72 	n32 = (u_int32_t *)n;
     73 	for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
     74 		n32[i] = bswap32(o32[i]);
     75 
     76 	n->fs_swuid = bswap64(o->fs_swuid);
     77 			/* fs_cgrotor is now unused */
     78 	n->fs_old_cpc = bswap32(o->fs_old_cpc);
     79 			/* fs_snapinum[20] - ignore for now */
     80 	n->fs_maxbsize = bswap32(o->fs_maxbsize);
     81 	n->fs_sblockloc = bswap64(o->fs_sblockloc);
     82 	ffs_csumtotal_swap(&o->fs_cstotal, &n->fs_cstotal);
     83 	n->fs_time = bswap64(o->fs_time);
     84 	n->fs_size = bswap64(o->fs_size);
     85 	n->fs_dsize = bswap64(o->fs_dsize);
     86 	n->fs_csaddr = bswap64(o->fs_csaddr);
     87 	n->fs_avgfilesize = bswap32(o->fs_avgfilesize);
     88 	n->fs_avgfpdir = bswap32(o->fs_avgfpdir);
     89 			/* fs_sparecon[28] - ignore for now */
     90 	n->fs_pendingblocks = bswap64(o->fs_pendingblocks);
     91 	n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
     92 	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
     93 	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
     94 	n->fs_old_inodefmt = bswap32(o->fs_old_inodefmt);
     95 	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
     96 	n->fs_qbmask = bswap64(o->fs_qbmask);
     97 	n->fs_qfmask = bswap64(o->fs_qfmask);
     98 	n->fs_state = bswap32(o->fs_state);
     99 	n->fs_old_postblformat = bswap32(o->fs_old_postblformat);
    100 	n->fs_old_nrpos = bswap32(o->fs_old_nrpos);
    101 	n->fs_old_postbloff = bswap32(o->fs_old_postbloff);
    102 	n->fs_old_rotbloff = bswap32(o->fs_old_rotbloff);
    103 	n->fs_magic = bswap32(o->fs_magic);
    104 }
    105 
    106 void
    107 ffs_dinode1_swap(struct ufs1_dinode *o, struct ufs1_dinode *n)
    108 {
    109 
    110 	n->di_mode = bswap16(o->di_mode);
    111 	n->di_nlink = bswap16(o->di_nlink);
    112 	n->di_u.oldids[0] = bswap16(o->di_u.oldids[0]);
    113 	n->di_u.oldids[1] = bswap16(o->di_u.oldids[1]);
    114 	n->di_size = bswap64(o->di_size);
    115 	n->di_atime = bswap32(o->di_atime);
    116 	n->di_atimensec = bswap32(o->di_atimensec);
    117 	n->di_mtime = bswap32(o->di_mtime);
    118 	n->di_mtimensec = bswap32(o->di_mtimensec);
    119 	n->di_ctime = bswap32(o->di_ctime);
    120 	n->di_ctimensec = bswap32(o->di_ctimensec);
    121 	memcpy(n->di_db, o->di_db, (NDADDR + NIADDR) * sizeof(u_int32_t));
    122 	n->di_flags = bswap32(o->di_flags);
    123 	n->di_blocks = bswap32(o->di_blocks);
    124 	n->di_gen = bswap32(o->di_gen);
    125 	n->di_uid = bswap32(o->di_uid);
    126 	n->di_gid = bswap32(o->di_gid);
    127 }
    128 
    129 void
    130 ffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n)
    131 {
    132 	n->di_mode = bswap16(o->di_mode);
    133 	n->di_nlink = bswap16(o->di_nlink);
    134 	n->di_uid = bswap32(o->di_uid);
    135 	n->di_gid = bswap32(o->di_gid);
    136 	n->di_blksize = bswap32(o->di_blksize);
    137 	n->di_size = bswap64(o->di_size);
    138 	n->di_blocks = bswap64(o->di_blocks);
    139 	n->di_atime = bswap64(o->di_atime);
    140 	n->di_atimensec = bswap32(o->di_atimensec);
    141 	n->di_mtime = bswap64(o->di_mtime);
    142 	n->di_mtimensec = bswap32(o->di_mtimensec);
    143 	n->di_ctime = bswap64(o->di_ctime);
    144 	n->di_ctimensec = bswap32(o->di_ctimensec);
    145 	n->di_birthtime = bswap64(o->di_ctime);
    146 	n->di_birthnsec = bswap32(o->di_ctimensec);
    147 	n->di_gen = bswap32(o->di_gen);
    148 	n->di_kernflags = bswap32(o->di_kernflags);
    149 	n->di_flags = bswap32(o->di_flags);
    150 	n->di_extsize = bswap32(o->di_extsize);
    151 	memcpy(n->di_extb, o->di_extb, (NXADDR + NDADDR + NIADDR) * 8);
    152 }
    153 
    154 void
    155 ffs_csum_swap(struct csum *o, struct csum *n, int size)
    156 {
    157 	int i;
    158 	u_int32_t *oint, *nint;
    159 
    160 	oint = (u_int32_t*)o;
    161 	nint = (u_int32_t*)n;
    162 
    163 	for (i = 0; i < size / sizeof(u_int32_t); i++)
    164 		nint[i] = bswap32(oint[i]);
    165 }
    166 
    167 void
    168 ffs_csumtotal_swap(struct csum_total *o, struct csum_total *n)
    169 {
    170 	n->cs_ndir = bswap64(o->cs_ndir);
    171 	n->cs_nbfree = bswap64(o->cs_nbfree);
    172 	n->cs_nifree = bswap64(o->cs_nifree);
    173 	n->cs_nffree = bswap64(o->cs_nffree);
    174 	n->cs_numclusters = bswap64(o->cs_numclusters);
    175 }
    176 
    177 /*
    178  * Note that ffs_cg_swap may be called with o == n.
    179  */
    180 void
    181 ffs_cg_swap(struct cg *o, struct cg *n, struct fs *fs)
    182 {
    183 	int i;
    184 	u_int32_t *n32, *o32;
    185 	u_int16_t *n16, *o16;
    186 	int32_t btotoff, boff, clustersumoff;
    187 
    188 	n->cg_firstfield = bswap32(o->cg_firstfield);
    189 	n->cg_magic = bswap32(o->cg_magic);
    190 	n->cg_old_time = bswap32(o->cg_old_time);
    191 	n->cg_cgx = bswap32(o->cg_cgx);
    192 	n->cg_old_ncyl = bswap16(o->cg_old_ncyl);
    193 	n->cg_old_niblk = bswap16(o->cg_old_niblk);
    194 	n->cg_ndblk = bswap32(o->cg_ndblk);
    195 	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
    196 	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
    197 	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
    198 	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
    199 	n->cg_rotor = bswap32(o->cg_rotor);
    200 	n->cg_frotor = bswap32(o->cg_frotor);
    201 	n->cg_irotor = bswap32(o->cg_irotor);
    202 	n->cg_old_btotoff = bswap32(o->cg_old_btotoff);
    203 	n->cg_old_boff = bswap32(o->cg_old_boff);
    204 	n->cg_iusedoff = bswap32(o->cg_iusedoff);
    205 	n->cg_freeoff = bswap32(o->cg_freeoff);
    206 	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
    207 	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
    208 	n->cg_clusteroff = bswap32(o->cg_clusteroff);
    209 	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
    210 	n->cg_niblk = bswap32(o->cg_niblk);
    211 	n->cg_initediblk = bswap32(o->cg_initediblk);
    212 	n->cg_time = bswap64(o->cg_time);
    213 	for (i = 0; i < MAXFRAG; i++)
    214 		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
    215 
    216 	if (fs->fs_magic == FS_UFS2_MAGIC)
    217 		return;
    218 
    219 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) { /* old format */
    220 		struct ocg *on, *oo;
    221 		int j;
    222 		on = (struct ocg *)n;
    223 		oo = (struct ocg *)o;
    224 		for(i = 0; i < 8; i++) {
    225 			on->cg_frsum[i] = bswap32(oo->cg_frsum[i]);
    226 		}
    227 		for(i = 0; i < 32; i++) {
    228 			on->cg_btot[i] = bswap32(oo->cg_btot[i]);
    229 			for (j = 0; j < 8; j++)
    230 				on->cg_b[i][j] = bswap16(oo->cg_b[i][j]);
    231 		}
    232 		memmove(on->cg_iused, oo->cg_iused, 256);
    233 		on->cg_magic = bswap32(oo->cg_magic);
    234 	} else {  /* new format */
    235 		if (n->cg_magic == CG_MAGIC) {
    236 			btotoff = n->cg_old_btotoff;
    237 			boff = n->cg_old_boff;
    238 			clustersumoff = n->cg_clustersumoff;
    239 		} else {
    240 			btotoff = bswap32(n->cg_old_btotoff);
    241 			boff = bswap32(n->cg_old_boff);
    242 			clustersumoff = bswap32(n->cg_clustersumoff);
    243 		}
    244 		n32 = (u_int32_t *)((u_int8_t *)n + btotoff);
    245 		o32 = (u_int32_t *)((u_int8_t *)o + btotoff);
    246 		n16 = (u_int16_t *)((u_int8_t *)n + boff);
    247 		o16 = (u_int16_t *)((u_int8_t *)o + boff);
    248 
    249 		for (i = 0; i < fs->fs_old_cpg; i++)
    250 			n32[i] = bswap32(o32[i]);
    251 
    252 		for (i = 0; i < fs->fs_old_cpg * fs->fs_old_nrpos; i++)
    253 			n16[i] = bswap16(o16[i]);
    254 
    255 		n32 = (u_int32_t *)((u_int8_t *)n + clustersumoff);
    256 		o32 = (u_int32_t *)((u_int8_t *)o + clustersumoff);
    257 		for (i = 1; i < fs->fs_contigsumsize + 1; i++)
    258 			n32[i] = bswap32(o32[i]);
    259 	}
    260 }
    261