Home | History | Annotate | Line # | Download | only in ffs
ffs_bswap.c revision 1.16
      1 /*	$NetBSD: ffs_bswap.c,v 1.16 2001/12/18 10:57:21 fvdl 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 __KERNEL_RCSID(0, "$NetBSD: ffs_bswap.c,v 1.16 2001/12/18 10:57:21 fvdl Exp $");
     37 
     38 #include <sys/param.h>
     39 #if defined(_KERNEL)
     40 #include <sys/systm.h>
     41 #endif
     42 
     43 #include <ufs/ufs/dinode.h>
     44 #include <ufs/ufs/ufs_bswap.h>
     45 #include <ufs/ffs/fs.h>
     46 #include <ufs/ffs/ffs_extern.h>
     47 
     48 #if !defined(_KERNEL)
     49 #include <stddef.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #define panic(x)	printf("%s\n", (x)), abort()
     54 #endif
     55 
     56 void
     57 ffs_sb_swap(struct fs *o, struct fs *n)
     58 {
     59 	int i, needswap, len;
     60 	u_int32_t *o32, *n32;
     61 	u_int16_t *o16, *n16;
     62 	u_int32_t postbloff, postblfmt;
     63 
     64 	if (o->fs_magic == FS_MAGIC) {
     65 		needswap = 0;
     66 	} else if (o->fs_magic == bswap32(FS_MAGIC)) {
     67 		needswap = 1;
     68 	} else {
     69 		panic("ffs_sb_swap: can't determine magic");
     70 	}
     71 	postbloff = ufs_rw32(o->fs_postbloff, needswap);
     72 	postblfmt = ufs_rw32(o->fs_postblformat, needswap);
     73 		/* compute these before swapping, in case o == n */
     74 	o16 = (postblfmt == FS_42POSTBLFMT) ? o->fs_opostbl[0] :
     75 	    (int16_t *)((u_int8_t *)o + postbloff);
     76 	n16 = (postblfmt == FS_42POSTBLFMT) ? n->fs_opostbl[0] :
     77 	    (int16_t *)((u_int8_t *)n + postbloff);
     78 	len = postblfmt == FS_42POSTBLFMT ?
     79 	    sizeof(o->fs_opostbl) / sizeof(o->fs_opostbl[0][0]) :
     80 	    ufs_rw32(o->fs_cpc, needswap) * ufs_rw32(o->fs_nrpos, needswap);
     81 
     82 	/*
     83 	 * In order to avoid a lot of lines, as the first N fields (52)
     84 	 * of the superblock up to fs_fmod are u_int32_t, we just loop
     85 	 * here to convert them.
     86 	 */
     87 	o32 = (u_int32_t *)o;
     88 	n32 = (u_int32_t *)n;
     89 	for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
     90 		n32[i] = bswap32(o32[i]);
     91 
     92 			/* fs_cgrotor is now unused */
     93 	n->fs_cpc = bswap32(o->fs_cpc);
     94 			/* fs_opostbl - may be done below */
     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_pendingblocks = bswap32(o->fs_pendingblocks);
    100 	n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
    101 	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
    102 	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
    103 	n->fs_inodefmt = bswap32(o->fs_inodefmt);
    104 	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
    105 	n->fs_qbmask = bswap64(o->fs_qbmask);
    106 	n->fs_qfmask = bswap64(o->fs_qfmask);
    107 	n->fs_state = bswap32(o->fs_state);
    108 	n->fs_postblformat = bswap32(o->fs_postblformat);
    109 	n->fs_nrpos = bswap32(o->fs_nrpos);
    110 	n->fs_postbloff = bswap32(o->fs_postbloff);
    111 	n->fs_rotbloff = bswap32(o->fs_rotbloff);
    112 	n->fs_magic = bswap32(o->fs_magic);
    113 			/* byteswap the postbl */
    114 	for (i = 0; i < len; i++)
    115 		n16[i] = bswap16(o16[i]);
    116 }
    117 
    118 void
    119 ffs_dinode_swap(struct dinode *o, struct dinode *n)
    120 {
    121 
    122 	n->di_mode = bswap16(o->di_mode);
    123 	n->di_nlink = bswap16(o->di_nlink);
    124 	n->di_u.oldids[0] = bswap16(o->di_u.oldids[0]);
    125 	n->di_u.oldids[1] = bswap16(o->di_u.oldids[1]);
    126 	n->di_size = bswap64(o->di_size);
    127 	n->di_atime = bswap32(o->di_atime);
    128 	n->di_atimensec = bswap32(o->di_atimensec);
    129 	n->di_mtime = bswap32(o->di_mtime);
    130 	n->di_mtimensec = bswap32(o->di_mtimensec);
    131 	n->di_ctime = bswap32(o->di_ctime);
    132 	n->di_ctimensec = bswap32(o->di_ctimensec);
    133 	memcpy(n->di_db, o->di_db, (NDADDR + NIADDR) * sizeof(u_int32_t));
    134 	n->di_flags = bswap32(o->di_flags);
    135 	n->di_blocks = bswap32(o->di_blocks);
    136 	n->di_gen = bswap32(o->di_gen);
    137 	n->di_uid = bswap32(o->di_uid);
    138 	n->di_gid = bswap32(o->di_gid);
    139 }
    140 
    141 void
    142 ffs_csum_swap(struct csum *o, struct csum *n, int size)
    143 {
    144 	int i;
    145 	u_int32_t *oint, *nint;
    146 
    147 	oint = (u_int32_t*)o;
    148 	nint = (u_int32_t*)n;
    149 
    150 	for (i = 0; i < size / sizeof(u_int32_t); i++)
    151 		nint[i] = bswap32(oint[i]);
    152 }
    153