Home | History | Annotate | Line # | Download | only in ffs
ffs_bswap.c revision 1.10.2.5
      1  1.10.2.5  nathanw /*	$NetBSD: ffs_bswap.c,v 1.10.2.5 2002/02/28 04:15:26 nathanw Exp $	*/
      2       1.1   bouyer 
      3       1.1   bouyer /*
      4       1.1   bouyer  * Copyright (c) 1998 Manuel Bouyer.
      5       1.1   bouyer  *
      6       1.1   bouyer  * Redistribution and use in source and binary forms, with or without
      7       1.1   bouyer  * modification, are permitted provided that the following conditions
      8       1.1   bouyer  * are met:
      9       1.1   bouyer  * 1. Redistributions of source code must retain the above copyright
     10       1.1   bouyer  *    notice, this list of conditions and the following disclaimer.
     11       1.1   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1   bouyer  *    notice, this list of conditions and the following disclaimer in the
     13       1.1   bouyer  *    documentation and/or other materials provided with the distribution.
     14       1.1   bouyer  * 3. All advertising materials mentioning features or use of this software
     15       1.1   bouyer  *    must display the following acknowledgement:
     16       1.1   bouyer  *	This product includes software developed by the University of
     17       1.1   bouyer  *	California, Berkeley and its contributors.
     18       1.1   bouyer  * 4. Neither the name of the University nor the names of its contributors
     19       1.1   bouyer  *    may be used to endorse or promote products derived from this software
     20       1.1   bouyer  *    without specific prior written permission.
     21       1.1   bouyer  *
     22       1.8   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23       1.8   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24       1.8   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25       1.8   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26       1.8   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27       1.8   bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28       1.8   bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29       1.8   bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30       1.8   bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31       1.8   bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1   bouyer  *
     33       1.1   bouyer  */
     34       1.1   bouyer 
     35  1.10.2.3  nathanw #include <sys/cdefs.h>
     36  1.10.2.5  nathanw #if defined(__KERNEL_RCSID)
     37  1.10.2.5  nathanw __KERNEL_RCSID(0, "$NetBSD: ffs_bswap.c,v 1.10.2.5 2002/02/28 04:15:26 nathanw Exp $");
     38  1.10.2.5  nathanw #endif
     39  1.10.2.5  nathanw 
     40  1.10.2.5  nathanw #if HAVE_CONFIG_H
     41  1.10.2.5  nathanw #include "config.h"
     42  1.10.2.5  nathanw #endif
     43  1.10.2.3  nathanw 
     44       1.2    ragge #include <sys/param.h>
     45  1.10.2.1  nathanw #if defined(_KERNEL)
     46       1.1   bouyer #include <sys/systm.h>
     47  1.10.2.1  nathanw #endif
     48       1.9    enami 
     49       1.1   bouyer #include <ufs/ufs/dinode.h>
     50       1.1   bouyer #include <ufs/ufs/ufs_bswap.h>
     51       1.1   bouyer #include <ufs/ffs/fs.h>
     52       1.1   bouyer #include <ufs/ffs/ffs_extern.h>
     53       1.6  thorpej 
     54       1.6  thorpej #if !defined(_KERNEL)
     55  1.10.2.3  nathanw #include <stddef.h>
     56  1.10.2.1  nathanw #include <stdio.h>
     57  1.10.2.1  nathanw #include <stdlib.h>
     58       1.6  thorpej #include <string.h>
     59  1.10.2.1  nathanw #define panic(x)	printf("%s\n", (x)), abort()
     60       1.6  thorpej #endif
     61       1.1   bouyer 
     62       1.1   bouyer void
     63  1.10.2.1  nathanw ffs_sb_swap(struct fs *o, struct fs *n)
     64       1.1   bouyer {
     65  1.10.2.1  nathanw 	int i, needswap, len;
     66       1.1   bouyer 	u_int32_t *o32, *n32;
     67       1.1   bouyer 	u_int16_t *o16, *n16;
     68  1.10.2.1  nathanw 	u_int32_t postbloff, postblfmt;
     69  1.10.2.1  nathanw 
     70  1.10.2.1  nathanw 	if (o->fs_magic == FS_MAGIC) {
     71  1.10.2.1  nathanw 		needswap = 0;
     72  1.10.2.1  nathanw 	} else if (o->fs_magic == bswap32(FS_MAGIC)) {
     73  1.10.2.1  nathanw 		needswap = 1;
     74  1.10.2.1  nathanw 	} else {
     75  1.10.2.1  nathanw 		panic("ffs_sb_swap: can't determine magic");
     76  1.10.2.1  nathanw 	}
     77  1.10.2.1  nathanw 	postbloff = ufs_rw32(o->fs_postbloff, needswap);
     78  1.10.2.1  nathanw 	postblfmt = ufs_rw32(o->fs_postblformat, needswap);
     79  1.10.2.3  nathanw 		/* compute these before swapping, in case o == n */
     80  1.10.2.3  nathanw 	o16 = (postblfmt == FS_42POSTBLFMT) ? o->fs_opostbl[0] :
     81  1.10.2.3  nathanw 	    (int16_t *)((u_int8_t *)o + postbloff);
     82  1.10.2.3  nathanw 	n16 = (postblfmt == FS_42POSTBLFMT) ? n->fs_opostbl[0] :
     83  1.10.2.3  nathanw 	    (int16_t *)((u_int8_t *)n + postbloff);
     84  1.10.2.3  nathanw 	len = postblfmt == FS_42POSTBLFMT ?
     85  1.10.2.3  nathanw 	    sizeof(o->fs_opostbl) / sizeof(o->fs_opostbl[0][0]) :
     86  1.10.2.3  nathanw 	    ufs_rw32(o->fs_cpc, needswap) * ufs_rw32(o->fs_nrpos, needswap);
     87  1.10.2.1  nathanw 
     88       1.9    enami 	/*
     89  1.10.2.3  nathanw 	 * In order to avoid a lot of lines, as the first N fields (52)
     90  1.10.2.3  nathanw 	 * of the superblock up to fs_fmod are u_int32_t, we just loop
     91  1.10.2.3  nathanw 	 * here to convert them.
     92       1.1   bouyer 	 */
     93       1.1   bouyer 	o32 = (u_int32_t *)o;
     94       1.1   bouyer 	n32 = (u_int32_t *)n;
     95  1.10.2.3  nathanw 	for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
     96       1.1   bouyer 		n32[i] = bswap32(o32[i]);
     97       1.9    enami 
     98  1.10.2.3  nathanw 			/* fs_cgrotor is now unused */
     99       1.1   bouyer 	n->fs_cpc = bswap32(o->fs_cpc);
    100  1.10.2.3  nathanw 			/* fs_opostbl - may be done below */
    101  1.10.2.3  nathanw 			/* fs_snapinum[20] - ignore for now */
    102  1.10.2.2  nathanw 	n->fs_avgfilesize = bswap32(o->fs_avgfilesize);
    103  1.10.2.2  nathanw 	n->fs_avgfpdir = bswap32(o->fs_avgfpdir);
    104  1.10.2.3  nathanw 			/* fs_sparecon[28] - ignore for now */
    105  1.10.2.4  nathanw 	n->fs_pendingblocks = bswap32(o->fs_pendingblocks);
    106  1.10.2.4  nathanw 	n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
    107       1.1   bouyer 	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
    108       1.1   bouyer 	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
    109       1.1   bouyer 	n->fs_inodefmt = bswap32(o->fs_inodefmt);
    110       1.1   bouyer 	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
    111       1.1   bouyer 	n->fs_qbmask = bswap64(o->fs_qbmask);
    112       1.1   bouyer 	n->fs_qfmask = bswap64(o->fs_qfmask);
    113       1.1   bouyer 	n->fs_state = bswap32(o->fs_state);
    114       1.1   bouyer 	n->fs_postblformat = bswap32(o->fs_postblformat);
    115       1.1   bouyer 	n->fs_nrpos = bswap32(o->fs_nrpos);
    116       1.1   bouyer 	n->fs_postbloff = bswap32(o->fs_postbloff);
    117       1.1   bouyer 	n->fs_rotbloff = bswap32(o->fs_rotbloff);
    118       1.1   bouyer 	n->fs_magic = bswap32(o->fs_magic);
    119  1.10.2.3  nathanw 			/* byteswap the postbl */
    120  1.10.2.1  nathanw 	for (i = 0; i < len; i++)
    121       1.1   bouyer 		n16[i] = bswap16(o16[i]);
    122       1.1   bouyer }
    123       1.1   bouyer 
    124       1.1   bouyer void
    125  1.10.2.1  nathanw ffs_dinode_swap(struct dinode *o, struct dinode *n)
    126       1.1   bouyer {
    127       1.9    enami 
    128       1.1   bouyer 	n->di_mode = bswap16(o->di_mode);
    129       1.1   bouyer 	n->di_nlink = bswap16(o->di_nlink);
    130       1.1   bouyer 	n->di_u.oldids[0] = bswap16(o->di_u.oldids[0]);
    131       1.1   bouyer 	n->di_u.oldids[1] = bswap16(o->di_u.oldids[1]);
    132       1.1   bouyer 	n->di_size = bswap64(o->di_size);
    133       1.1   bouyer 	n->di_atime = bswap32(o->di_atime);
    134       1.1   bouyer 	n->di_atimensec = bswap32(o->di_atimensec);
    135       1.1   bouyer 	n->di_mtime = bswap32(o->di_mtime);
    136       1.1   bouyer 	n->di_mtimensec = bswap32(o->di_mtimensec);
    137       1.1   bouyer 	n->di_ctime = bswap32(o->di_ctime);
    138       1.1   bouyer 	n->di_ctimensec = bswap32(o->di_ctimensec);
    139       1.5    perry 	memcpy(n->di_db, o->di_db, (NDADDR + NIADDR) * sizeof(u_int32_t));
    140       1.1   bouyer 	n->di_flags = bswap32(o->di_flags);
    141       1.1   bouyer 	n->di_blocks = bswap32(o->di_blocks);
    142       1.1   bouyer 	n->di_gen = bswap32(o->di_gen);
    143       1.1   bouyer 	n->di_uid = bswap32(o->di_uid);
    144       1.1   bouyer 	n->di_gid = bswap32(o->di_gid);
    145       1.1   bouyer }
    146       1.1   bouyer 
    147       1.1   bouyer void
    148  1.10.2.1  nathanw ffs_csum_swap(struct csum *o, struct csum *n, int size)
    149       1.1   bouyer {
    150       1.1   bouyer 	int i;
    151       1.1   bouyer 	u_int32_t *oint, *nint;
    152       1.1   bouyer 
    153       1.1   bouyer 	oint = (u_int32_t*)o;
    154       1.1   bouyer 	nint = (u_int32_t*)n;
    155       1.1   bouyer 
    156       1.1   bouyer 	for (i = 0; i < size / sizeof(u_int32_t); i++)
    157       1.1   bouyer 		nint[i] = bswap32(oint[i]);
    158       1.1   bouyer }
    159