1185222Ssam/*	$NetBSD: ffs_bswap.c,v 1.28 2004/05/25 14:54:59 hannken Exp $	*/
2185222Ssam
3185222Ssam/*
4185222Ssam * Copyright (c) 1998 Manuel Bouyer.
5185222Ssam *
6185222Ssam * Redistribution and use in source and binary forms, with or without
7185222Ssam * modification, are permitted provided that the following conditions
8185222Ssam * are met:
9185222Ssam * 1. Redistributions of source code must retain the above copyright
10185222Ssam *    notice, this list of conditions and the following disclaimer.
11185222Ssam * 2. Redistributions in binary form must reproduce the above copyright
12185222Ssam *    notice, this list of conditions and the following disclaimer in the
13185222Ssam *    documentation and/or other materials provided with the distribution.
14185222Ssam * 3. All advertising materials mentioning features or use of this software
15185222Ssam *    must display the following acknowledgement:
16185222Ssam *	This product includes software developed by Manuel Bouyer.
17185222Ssam * 4. The name of the author may not be used to endorse or promote products
18185222Ssam *    derived from this software without specific prior written permission.
19185222Ssam *
20185222Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21185222Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22185222Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23185222Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24185222Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25185222Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26185222Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27185222Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28185222Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29185222Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30185222Ssam *
31185222Ssam */
32185222Ssam
33185222Ssam#include <sys/cdefs.h>
34186261Ssam__FBSDID("$FreeBSD$");
35185222Ssam
36185222Ssam#include <sys/param.h>
37185222Ssam#if defined(_KERNEL)
38185222Ssam#include <sys/systm.h>
39185222Ssam#endif
40185222Ssam
41185222Ssam#include <ufs/ufs/dinode.h>
42186261Ssam#include "ffs/ufs_bswap.h"
43185222Ssam#include <ufs/ffs/fs.h>
44185222Ssam
45185222Ssam#if !defined(_KERNEL)
46185222Ssam#include <stddef.h>
47185222Ssam#include <stdio.h>
48185222Ssam#include <stdlib.h>
49185222Ssam#include <string.h>
50185222Ssam#define panic(x)	printf("%s\n", (x)), abort()
51185222Ssam#endif
52185222Ssam
53186261Ssam#define	fs_old_postbloff	fs_spare5[0]
54186261Ssam#define	fs_old_rotbloff		fs_spare5[1]
55186261Ssam#define	fs_old_postbl_start	fs_maxbsize
56186261Ssam#define	fs_old_headswitch	fs_id[0]
57186261Ssam#define	fs_old_trkseek	fs_id[1]
58186261Ssam#define	fs_old_csmask	fs_spare1[0]
59186261Ssam#define	fs_old_csshift	fs_spare1[1]
60186261Ssam
61186261Ssam#define	FS_42POSTBLFMT		-1	/* 4.2BSD rotational table format */
62186261Ssam#define	FS_DYNAMICPOSTBLFMT	1	/* dynamic rotational table format */
63186261Ssam
64186261Ssamvoid ffs_csum_swap(struct csum *o, struct csum *n, int size);
65186261Ssamvoid ffs_csumtotal_swap(struct csum_total *o, struct csum_total *n);
66186261Ssam
67185222Ssamvoid
68185222Ssamffs_sb_swap(struct fs *o, struct fs *n)
69185222Ssam{
70185222Ssam	int i;
71185222Ssam	u_int32_t *o32, *n32;
72185222Ssam
73185222Ssam	/*
74185222Ssam	 * In order to avoid a lot of lines, as the first N fields (52)
75185222Ssam	 * of the superblock up to fs_fmod are u_int32_t, we just loop
76185222Ssam	 * here to convert them.
77185222Ssam	 */
78185222Ssam	o32 = (u_int32_t *)o;
79185222Ssam	n32 = (u_int32_t *)n;
80185222Ssam	for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
81185222Ssam		n32[i] = bswap32(o32[i]);
82185222Ssam
83185222Ssam	n->fs_swuid = bswap64(o->fs_swuid);
84185222Ssam	n->fs_cgrotor = bswap32(o->fs_cgrotor); /* Unused */
85185222Ssam	n->fs_old_cpc = bswap32(o->fs_old_cpc);
86185222Ssam
87185222Ssam	/* These fields overlap with a possible location for the
88185222Ssam	 * historic FS_DYNAMICPOSTBLFMT postbl table, and with the
89185222Ssam	 * first half of the historic FS_42POSTBLFMT postbl table.
90185222Ssam	 */
91185222Ssam	n->fs_maxbsize = bswap32(o->fs_maxbsize);
92185222Ssam	n->fs_sblockloc = bswap64(o->fs_sblockloc);
93185222Ssam	ffs_csumtotal_swap(&o->fs_cstotal, &n->fs_cstotal);
94185222Ssam	n->fs_time = bswap64(o->fs_time);
95185222Ssam	n->fs_size = bswap64(o->fs_size);
96185222Ssam	n->fs_dsize = bswap64(o->fs_dsize);
97185222Ssam	n->fs_csaddr = bswap64(o->fs_csaddr);
98185222Ssam	n->fs_pendingblocks = bswap64(o->fs_pendingblocks);
99185222Ssam	n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
100185222Ssam
101185222Ssam	/* These fields overlap with the second half of the
102185222Ssam	 * historic FS_42POSTBLFMT postbl table
103185222Ssam	 */
104185222Ssam	for (i = 0; i < FSMAXSNAP; i++)
105185222Ssam		n->fs_snapinum[i] = bswap32(o->fs_snapinum[i]);
106185222Ssam	n->fs_avgfilesize = bswap32(o->fs_avgfilesize);
107185222Ssam	n->fs_avgfpdir = bswap32(o->fs_avgfpdir);
108185222Ssam	/* fs_sparecon[28] - ignore for now */
109185222Ssam	n->fs_flags = bswap32(o->fs_flags);
110185222Ssam	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
111185222Ssam	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
112185222Ssam	n->fs_old_inodefmt = bswap32(o->fs_old_inodefmt);
113185222Ssam	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
114185222Ssam	n->fs_qbmask = bswap64(o->fs_qbmask);
115185222Ssam	n->fs_qfmask = bswap64(o->fs_qfmask);
116185222Ssam	n->fs_state = bswap32(o->fs_state);
117185222Ssam	n->fs_old_postblformat = bswap32(o->fs_old_postblformat);
118185222Ssam	n->fs_old_nrpos = bswap32(o->fs_old_nrpos);
119185222Ssam	n->fs_old_postbloff = bswap32(o->fs_old_postbloff);
120185222Ssam	n->fs_old_rotbloff = bswap32(o->fs_old_rotbloff);
121185222Ssam
122185222Ssam	n->fs_magic = bswap32(o->fs_magic);
123185222Ssam}
124185222Ssam
125185222Ssamvoid
126185222Ssamffs_dinode1_swap(struct ufs1_dinode *o, struct ufs1_dinode *n)
127185222Ssam{
128185222Ssam
129185222Ssam	n->di_mode = bswap16(o->di_mode);
130185222Ssam	n->di_nlink = bswap16(o->di_nlink);
131185222Ssam	n->di_size = bswap64(o->di_size);
132185222Ssam	n->di_atime = bswap32(o->di_atime);
133185222Ssam	n->di_atimensec = bswap32(o->di_atimensec);
134185222Ssam	n->di_mtime = bswap32(o->di_mtime);
135185222Ssam	n->di_mtimensec = bswap32(o->di_mtimensec);
136185222Ssam	n->di_ctime = bswap32(o->di_ctime);
137185222Ssam	n->di_ctimensec = bswap32(o->di_ctimensec);
138185222Ssam	memcpy(n->di_db, o->di_db, (NDADDR + NIADDR) * sizeof(u_int32_t));
139185222Ssam	n->di_flags = bswap32(o->di_flags);
140185222Ssam	n->di_blocks = bswap32(o->di_blocks);
141185222Ssam	n->di_gen = bswap32(o->di_gen);
142185222Ssam	n->di_uid = bswap32(o->di_uid);
143185222Ssam	n->di_gid = bswap32(o->di_gid);
144185222Ssam}
145185222Ssam
146185222Ssamvoid
147185222Ssamffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n)
148185222Ssam{
149185222Ssam	n->di_mode = bswap16(o->di_mode);
150185222Ssam	n->di_nlink = bswap16(o->di_nlink);
151185222Ssam	n->di_uid = bswap32(o->di_uid);
152185222Ssam	n->di_gid = bswap32(o->di_gid);
153185222Ssam	n->di_blksize = bswap32(o->di_blksize);
154185222Ssam	n->di_size = bswap64(o->di_size);
155185222Ssam	n->di_blocks = bswap64(o->di_blocks);
156185222Ssam	n->di_atime = bswap64(o->di_atime);
157185222Ssam	n->di_atimensec = bswap32(o->di_atimensec);
158185222Ssam	n->di_mtime = bswap64(o->di_mtime);
159185222Ssam	n->di_mtimensec = bswap32(o->di_mtimensec);
160185222Ssam	n->di_ctime = bswap64(o->di_ctime);
161185222Ssam	n->di_ctimensec = bswap32(o->di_ctimensec);
162185222Ssam	n->di_birthtime = bswap64(o->di_ctime);
163185222Ssam	n->di_birthnsec = bswap32(o->di_ctimensec);
164185222Ssam	n->di_gen = bswap32(o->di_gen);
165185222Ssam	n->di_kernflags = bswap32(o->di_kernflags);
166185222Ssam	n->di_flags = bswap32(o->di_flags);
167185222Ssam	n->di_extsize = bswap32(o->di_extsize);
168185222Ssam	memcpy(n->di_extb, o->di_extb, (NXADDR + NDADDR + NIADDR) * 8);
169185222Ssam}
170185222Ssam
171185222Ssamvoid
172185222Ssamffs_csum_swap(struct csum *o, struct csum *n, int size)
173185222Ssam{
174185222Ssam	int i;
175185222Ssam	u_int32_t *oint, *nint;
176185222Ssam
177185222Ssam	oint = (u_int32_t*)o;
178185222Ssam	nint = (u_int32_t*)n;
179185222Ssam
180185222Ssam	for (i = 0; i < size / sizeof(u_int32_t); i++)
181185222Ssam		nint[i] = bswap32(oint[i]);
182185222Ssam}
183185222Ssam
184185222Ssamvoid
185185222Ssamffs_csumtotal_swap(struct csum_total *o, struct csum_total *n)
186185222Ssam{
187185222Ssam	n->cs_ndir = bswap64(o->cs_ndir);
188185222Ssam	n->cs_nbfree = bswap64(o->cs_nbfree);
189185222Ssam	n->cs_nifree = bswap64(o->cs_nifree);
190185222Ssam	n->cs_nffree = bswap64(o->cs_nffree);
191185222Ssam}
192185222Ssam
193185222Ssam/*
194185222Ssam * Note that ffs_cg_swap may be called with o == n.
195185222Ssam */
196185222Ssamvoid
197185222Ssamffs_cg_swap(struct cg *o, struct cg *n, struct fs *fs)
198185222Ssam{
199185222Ssam	int i;
200185222Ssam	u_int32_t *n32, *o32;
201185222Ssam	u_int16_t *n16, *o16;
202185222Ssam	int32_t btotoff, boff, clustersumoff;
203185222Ssam
204185222Ssam	n->cg_firstfield = bswap32(o->cg_firstfield);
205185222Ssam	n->cg_magic = bswap32(o->cg_magic);
206185222Ssam	n->cg_old_time = bswap32(o->cg_old_time);
207185222Ssam	n->cg_cgx = bswap32(o->cg_cgx);
208185222Ssam	n->cg_old_ncyl = bswap16(o->cg_old_ncyl);
209185222Ssam	n->cg_old_niblk = bswap16(o->cg_old_niblk);
210185222Ssam	n->cg_ndblk = bswap32(o->cg_ndblk);
211185222Ssam	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
212185222Ssam	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
213185222Ssam	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
214185222Ssam	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
215185222Ssam	n->cg_rotor = bswap32(o->cg_rotor);
216185222Ssam	n->cg_frotor = bswap32(o->cg_frotor);
217185222Ssam	n->cg_irotor = bswap32(o->cg_irotor);
218185222Ssam	for (i = 0; i < MAXFRAG; i++)
219185222Ssam		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
220185222Ssam
221186261Ssam	n->cg_old_btotoff = bswap32(o->cg_old_btotoff);
222186261Ssam	n->cg_old_boff = bswap32(o->cg_old_boff);
223186261Ssam	n->cg_iusedoff = bswap32(o->cg_iusedoff);
224186261Ssam	n->cg_freeoff = bswap32(o->cg_freeoff);
225186261Ssam	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
226186261Ssam	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
227186261Ssam	n->cg_clusteroff = bswap32(o->cg_clusteroff);
228186261Ssam	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
229186261Ssam	n->cg_niblk = bswap32(o->cg_niblk);
230186261Ssam	n->cg_initediblk = bswap32(o->cg_initediblk);
231186261Ssam	n->cg_time = bswap64(o->cg_time);
232185222Ssam
233186261Ssam	if (fs->fs_magic == FS_UFS2_MAGIC)
234186261Ssam		return;
235185222Ssam
236186261Ssam	if (n->cg_magic == CG_MAGIC) {
237186261Ssam		btotoff = n->cg_old_btotoff;
238186261Ssam		boff = n->cg_old_boff;
239186261Ssam		clustersumoff = n->cg_clustersumoff;
240186261Ssam	} else {
241186261Ssam		btotoff = bswap32(n->cg_old_btotoff);
242186261Ssam		boff = bswap32(n->cg_old_boff);
243186261Ssam		clustersumoff = bswap32(n->cg_clustersumoff);
244186261Ssam	}
245186261Ssam	n32 = (u_int32_t *)((u_int8_t *)n + btotoff);
246186261Ssam	o32 = (u_int32_t *)((u_int8_t *)o + btotoff);
247186261Ssam	n16 = (u_int16_t *)((u_int8_t *)n + boff);
248186261Ssam	o16 = (u_int16_t *)((u_int8_t *)o + boff);
249185222Ssam
250186261Ssam	for (i = 0; i < fs->fs_old_cpg; i++)
251186261Ssam		n32[i] = bswap32(o32[i]);
252186261Ssam
253186261Ssam	for (i = 0; i < fs->fs_old_cpg * fs->fs_old_nrpos; i++)
254186261Ssam		n16[i] = bswap16(o16[i]);
255185222Ssam
256186261Ssam	n32 = (u_int32_t *)((u_int8_t *)n + clustersumoff);
257186261Ssam	o32 = (u_int32_t *)((u_int8_t *)o + clustersumoff);
258186261Ssam	for (i = 1; i < fs->fs_contigsumsize + 1; i++)
259186261Ssam		n32[i] = bswap32(o32[i]);
260185222Ssam}
261