2005-12-11 15:16:03 +03:00
|
|
|
/* $NetBSD: ffs_bswap.c,v 1.32 2005/12/11 12:25:25 christos Exp $ */
|
1998-03-18 18:57:26 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1998 Manuel Bouyer.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
2003-10-05 21:48:49 +04:00
|
|
|
* This product includes software developed by Manuel Bouyer.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
1998-03-18 18:57:26 +03:00
|
|
|
*
|
2000-05-15 12:51:55 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1998-03-18 18:57:26 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-10-27 03:12:41 +03:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
2002-01-31 22:19:22 +03:00
|
|
|
#endif
|
|
|
|
|
2003-10-27 03:12:41 +03:00
|
|
|
#include <sys/cdefs.h>
|
2005-12-11 15:16:03 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ffs_bswap.c,v 1.32 2005/12/11 12:25:25 christos Exp $");
|
2001-10-30 04:11:53 +03:00
|
|
|
|
1998-03-18 18:57:26 +03:00
|
|
|
#include <sys/param.h>
|
2001-08-17 06:18:46 +04:00
|
|
|
#if defined(_KERNEL)
|
1998-06-08 21:59:08 +04:00
|
|
|
#include <sys/systm.h>
|
2001-08-17 06:18:46 +04:00
|
|
|
#endif
|
2000-12-23 17:09:52 +03:00
|
|
|
|
1998-03-18 18:57:26 +03:00
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/ufs_bswap.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
|
|
|
#include <ufs/ffs/ffs_extern.h>
|
|
|
|
|
1999-09-14 08:50:54 +04:00
|
|
|
#if !defined(_KERNEL)
|
2001-10-29 14:26:35 +03:00
|
|
|
#include <stddef.h>
|
2001-08-17 06:18:46 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1999-09-14 08:50:54 +04:00
|
|
|
#include <string.h>
|
2001-08-17 06:18:46 +04:00
|
|
|
#define panic(x) printf("%s\n", (x)), abort()
|
1999-09-14 08:50:54 +04:00
|
|
|
#endif
|
|
|
|
|
1998-03-18 18:57:26 +03:00
|
|
|
void
|
2001-08-17 06:18:46 +04:00
|
|
|
ffs_sb_swap(struct fs *o, struct fs *n)
|
1998-03-18 18:57:26 +03:00
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
int i;
|
1998-03-18 18:57:26 +03:00
|
|
|
u_int32_t *o32, *n32;
|
2001-08-17 06:18:46 +04:00
|
|
|
|
2000-12-23 17:09:52 +03:00
|
|
|
/*
|
2001-10-29 14:26:35 +03:00
|
|
|
* In order to avoid a lot of lines, as the first N fields (52)
|
|
|
|
* of the superblock up to fs_fmod are u_int32_t, we just loop
|
|
|
|
* here to convert them.
|
1998-03-18 18:57:26 +03:00
|
|
|
*/
|
|
|
|
o32 = (u_int32_t *)o;
|
|
|
|
n32 = (u_int32_t *)n;
|
2001-10-29 14:26:35 +03:00
|
|
|
for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
|
1998-03-18 18:57:26 +03:00
|
|
|
n32[i] = bswap32(o32[i]);
|
2000-12-23 17:09:52 +03:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
n->fs_swuid = bswap64(o->fs_swuid);
|
2003-12-30 06:30:43 +03:00
|
|
|
n->fs_cgrotor = bswap32(o->fs_cgrotor); /* Unused */
|
2003-04-02 14:39:19 +04:00
|
|
|
n->fs_old_cpc = bswap32(o->fs_old_cpc);
|
2003-12-31 21:40:23 +03:00
|
|
|
|
|
|
|
/* These fields overlap with a possible location for the
|
|
|
|
* historic FS_DYNAMICPOSTBLFMT postbl table, and with the
|
|
|
|
* first half of the historic FS_42POSTBLFMT postbl table.
|
|
|
|
*/
|
2003-04-02 14:39:19 +04:00
|
|
|
n->fs_maxbsize = bswap32(o->fs_maxbsize);
|
|
|
|
n->fs_sblockloc = bswap64(o->fs_sblockloc);
|
|
|
|
ffs_csumtotal_swap(&o->fs_cstotal, &n->fs_cstotal);
|
|
|
|
n->fs_time = bswap64(o->fs_time);
|
|
|
|
n->fs_size = bswap64(o->fs_size);
|
|
|
|
n->fs_dsize = bswap64(o->fs_dsize);
|
|
|
|
n->fs_csaddr = bswap64(o->fs_csaddr);
|
2003-04-16 18:20:25 +04:00
|
|
|
n->fs_pendingblocks = bswap64(o->fs_pendingblocks);
|
|
|
|
n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2003-12-31 21:40:23 +03:00
|
|
|
/* These fields overlap with the second half of the
|
|
|
|
* historic FS_42POSTBLFMT postbl table
|
|
|
|
*/
|
2004-05-25 18:54:55 +04:00
|
|
|
for (i = 0; i < FSMAXSNAP; i++)
|
|
|
|
n->fs_snapinum[i] = bswap32(o->fs_snapinum[i]);
|
2003-12-31 21:40:23 +03:00
|
|
|
n->fs_avgfilesize = bswap32(o->fs_avgfilesize);
|
|
|
|
n->fs_avgfpdir = bswap32(o->fs_avgfpdir);
|
|
|
|
/* fs_sparecon[28] - ignore for now */
|
2003-12-31 21:32:47 +03:00
|
|
|
n->fs_flags = bswap32(o->fs_flags);
|
1998-03-18 18:57:26 +03:00
|
|
|
n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
|
|
|
|
n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
|
2003-04-02 14:39:19 +04:00
|
|
|
n->fs_old_inodefmt = bswap32(o->fs_old_inodefmt);
|
1998-03-18 18:57:26 +03:00
|
|
|
n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
|
|
|
|
n->fs_qbmask = bswap64(o->fs_qbmask);
|
|
|
|
n->fs_qfmask = bswap64(o->fs_qfmask);
|
|
|
|
n->fs_state = bswap32(o->fs_state);
|
2003-04-02 14:39:19 +04:00
|
|
|
n->fs_old_postblformat = bswap32(o->fs_old_postblformat);
|
|
|
|
n->fs_old_nrpos = bswap32(o->fs_old_nrpos);
|
|
|
|
n->fs_old_postbloff = bswap32(o->fs_old_postbloff);
|
|
|
|
n->fs_old_rotbloff = bswap32(o->fs_old_rotbloff);
|
2003-12-31 21:40:23 +03:00
|
|
|
|
1998-03-18 18:57:26 +03:00
|
|
|
n->fs_magic = bswap32(o->fs_magic);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-04-02 14:39:19 +04:00
|
|
|
ffs_dinode1_swap(struct ufs1_dinode *o, struct ufs1_dinode *n)
|
1998-03-18 18:57:26 +03:00
|
|
|
{
|
2000-12-23 17:09:52 +03:00
|
|
|
|
1998-03-18 18:57:26 +03:00
|
|
|
n->di_mode = bswap16(o->di_mode);
|
|
|
|
n->di_nlink = bswap16(o->di_nlink);
|
|
|
|
n->di_u.oldids[0] = bswap16(o->di_u.oldids[0]);
|
|
|
|
n->di_u.oldids[1] = bswap16(o->di_u.oldids[1]);
|
|
|
|
n->di_size = bswap64(o->di_size);
|
|
|
|
n->di_atime = bswap32(o->di_atime);
|
|
|
|
n->di_atimensec = bswap32(o->di_atimensec);
|
|
|
|
n->di_mtime = bswap32(o->di_mtime);
|
|
|
|
n->di_mtimensec = bswap32(o->di_mtimensec);
|
|
|
|
n->di_ctime = bswap32(o->di_ctime);
|
|
|
|
n->di_ctimensec = bswap32(o->di_ctimensec);
|
1998-08-10 00:15:38 +04:00
|
|
|
memcpy(n->di_db, o->di_db, (NDADDR + NIADDR) * sizeof(u_int32_t));
|
1998-03-18 18:57:26 +03:00
|
|
|
n->di_flags = bswap32(o->di_flags);
|
|
|
|
n->di_blocks = bswap32(o->di_blocks);
|
|
|
|
n->di_gen = bswap32(o->di_gen);
|
|
|
|
n->di_uid = bswap32(o->di_uid);
|
|
|
|
n->di_gid = bswap32(o->di_gid);
|
|
|
|
}
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
void
|
|
|
|
ffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n)
|
|
|
|
{
|
|
|
|
n->di_mode = bswap16(o->di_mode);
|
|
|
|
n->di_nlink = bswap16(o->di_nlink);
|
|
|
|
n->di_uid = bswap32(o->di_uid);
|
|
|
|
n->di_gid = bswap32(o->di_gid);
|
|
|
|
n->di_blksize = bswap32(o->di_blksize);
|
|
|
|
n->di_size = bswap64(o->di_size);
|
|
|
|
n->di_blocks = bswap64(o->di_blocks);
|
|
|
|
n->di_atime = bswap64(o->di_atime);
|
|
|
|
n->di_atimensec = bswap32(o->di_atimensec);
|
|
|
|
n->di_mtime = bswap64(o->di_mtime);
|
|
|
|
n->di_mtimensec = bswap32(o->di_mtimensec);
|
|
|
|
n->di_ctime = bswap64(o->di_ctime);
|
|
|
|
n->di_ctimensec = bswap32(o->di_ctimensec);
|
2005-06-02 14:08:36 +04:00
|
|
|
n->di_birthtime = bswap64(o->di_birthtime);
|
|
|
|
n->di_birthnsec = bswap32(o->di_birthnsec);
|
2003-04-02 14:39:19 +04:00
|
|
|
n->di_gen = bswap32(o->di_gen);
|
|
|
|
n->di_kernflags = bswap32(o->di_kernflags);
|
|
|
|
n->di_flags = bswap32(o->di_flags);
|
|
|
|
n->di_extsize = bswap32(o->di_extsize);
|
|
|
|
memcpy(n->di_extb, o->di_extb, (NXADDR + NDADDR + NIADDR) * 8);
|
|
|
|
}
|
|
|
|
|
1998-03-18 18:57:26 +03:00
|
|
|
void
|
2001-08-17 06:18:46 +04:00
|
|
|
ffs_csum_swap(struct csum *o, struct csum *n, int size)
|
1998-03-18 18:57:26 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u_int32_t *oint, *nint;
|
2005-02-27 01:31:44 +03:00
|
|
|
|
1998-03-18 18:57:26 +03:00
|
|
|
oint = (u_int32_t*)o;
|
|
|
|
nint = (u_int32_t*)n;
|
|
|
|
|
|
|
|
for (i = 0; i < size / sizeof(u_int32_t); i++)
|
|
|
|
nint[i] = bswap32(oint[i]);
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
ffs_csumtotal_swap(struct csum_total *o, struct csum_total *n)
|
|
|
|
{
|
|
|
|
n->cs_ndir = bswap64(o->cs_ndir);
|
|
|
|
n->cs_nbfree = bswap64(o->cs_nbfree);
|
|
|
|
n->cs_nifree = bswap64(o->cs_nifree);
|
|
|
|
n->cs_nffree = bswap64(o->cs_nffree);
|
|
|
|
}
|
|
|
|
|
2003-04-11 14:20:35 +04:00
|
|
|
/*
|
|
|
|
* Note that ffs_cg_swap may be called with o == n.
|
|
|
|
*/
|
2003-04-02 14:39:19 +04:00
|
|
|
void
|
|
|
|
ffs_cg_swap(struct cg *o, struct cg *n, struct fs *fs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u_int32_t *n32, *o32;
|
|
|
|
u_int16_t *n16, *o16;
|
2003-04-11 14:20:35 +04:00
|
|
|
int32_t btotoff, boff, clustersumoff;
|
2003-04-02 14:39:19 +04:00
|
|
|
|
|
|
|
n->cg_firstfield = bswap32(o->cg_firstfield);
|
|
|
|
n->cg_magic = bswap32(o->cg_magic);
|
|
|
|
n->cg_old_time = bswap32(o->cg_old_time);
|
|
|
|
n->cg_cgx = bswap32(o->cg_cgx);
|
|
|
|
n->cg_old_ncyl = bswap16(o->cg_old_ncyl);
|
|
|
|
n->cg_old_niblk = bswap16(o->cg_old_niblk);
|
|
|
|
n->cg_ndblk = bswap32(o->cg_ndblk);
|
|
|
|
n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
|
|
|
|
n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
|
|
|
|
n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
|
|
|
|
n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
|
|
|
|
n->cg_rotor = bswap32(o->cg_rotor);
|
|
|
|
n->cg_frotor = bswap32(o->cg_frotor);
|
|
|
|
n->cg_irotor = bswap32(o->cg_irotor);
|
2003-04-11 14:20:35 +04:00
|
|
|
for (i = 0; i < MAXFRAG; i++)
|
2003-04-02 14:39:19 +04:00
|
|
|
n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2003-12-30 06:30:43 +03:00
|
|
|
if ((fs->fs_magic != FS_UFS2_MAGIC) &&
|
|
|
|
(fs->fs_old_postblformat == FS_42POSTBLFMT)) { /* old format */
|
2003-04-02 14:39:19 +04:00
|
|
|
struct ocg *on, *oo;
|
|
|
|
int j;
|
|
|
|
on = (struct ocg *)n;
|
|
|
|
oo = (struct ocg *)o;
|
2003-12-30 06:30:43 +03:00
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
2003-04-02 14:39:19 +04:00
|
|
|
on->cg_btot[i] = bswap32(oo->cg_btot[i]);
|
|
|
|
for (j = 0; j < 8; j++)
|
|
|
|
on->cg_b[i][j] = bswap16(oo->cg_b[i][j]);
|
|
|
|
}
|
|
|
|
memmove(on->cg_iused, oo->cg_iused, 256);
|
|
|
|
on->cg_magic = bswap32(oo->cg_magic);
|
|
|
|
} else { /* new format */
|
2003-12-30 06:30:43 +03:00
|
|
|
|
|
|
|
n->cg_old_btotoff = bswap32(o->cg_old_btotoff);
|
|
|
|
n->cg_old_boff = bswap32(o->cg_old_boff);
|
|
|
|
n->cg_iusedoff = bswap32(o->cg_iusedoff);
|
|
|
|
n->cg_freeoff = bswap32(o->cg_freeoff);
|
|
|
|
n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
|
|
|
|
n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
|
|
|
|
n->cg_clusteroff = bswap32(o->cg_clusteroff);
|
|
|
|
n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
|
|
|
|
n->cg_niblk = bswap32(o->cg_niblk);
|
|
|
|
n->cg_initediblk = bswap32(o->cg_initediblk);
|
|
|
|
n->cg_time = bswap64(o->cg_time);
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
if (n->cg_magic == CG_MAGIC) {
|
2003-04-11 14:20:35 +04:00
|
|
|
btotoff = n->cg_old_btotoff;
|
|
|
|
boff = n->cg_old_boff;
|
|
|
|
clustersumoff = n->cg_clustersumoff;
|
2003-04-02 14:39:19 +04:00
|
|
|
} else {
|
2003-04-11 14:20:35 +04:00
|
|
|
btotoff = bswap32(n->cg_old_btotoff);
|
|
|
|
boff = bswap32(n->cg_old_boff);
|
|
|
|
clustersumoff = bswap32(n->cg_clustersumoff);
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
2005-06-03 05:14:07 +04:00
|
|
|
|
|
|
|
n32 = (u_int32_t *)((u_int8_t *)n + clustersumoff);
|
|
|
|
o32 = (u_int32_t *)((u_int8_t *)o + clustersumoff);
|
|
|
|
for (i = 1; i < fs->fs_contigsumsize + 1; i++)
|
|
|
|
n32[i] = bswap32(o32[i]);
|
|
|
|
|
|
|
|
if (fs->fs_magic == FS_UFS2_MAGIC)
|
|
|
|
return;
|
|
|
|
|
2003-04-11 14:20:35 +04:00
|
|
|
n32 = (u_int32_t *)((u_int8_t *)n + btotoff);
|
|
|
|
o32 = (u_int32_t *)((u_int8_t *)o + btotoff);
|
|
|
|
n16 = (u_int16_t *)((u_int8_t *)n + boff);
|
|
|
|
o16 = (u_int16_t *)((u_int8_t *)o + boff);
|
|
|
|
|
|
|
|
for (i = 0; i < fs->fs_old_cpg; i++)
|
2003-04-02 14:39:19 +04:00
|
|
|
n32[i] = bswap32(o32[i]);
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2003-04-11 14:20:35 +04:00
|
|
|
for (i = 0; i < fs->fs_old_cpg * fs->fs_old_nrpos; i++)
|
2003-04-02 14:39:19 +04:00
|
|
|
n16[i] = bswap16(o16[i]);
|
|
|
|
}
|
|
|
|
}
|