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