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{
70290597Sngie	size_t 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);
100290597Sngie
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);
138301784Sngie	memcpy(n->di_db, o->di_db, sizeof(n->di_db));
139301784Sngie	memcpy(n->di_ib, o->di_ib, sizeof(n->di_ib));
140185222Ssam	n->di_flags = bswap32(o->di_flags);
141185222Ssam	n->di_blocks = bswap32(o->di_blocks);
142185222Ssam	n->di_gen = bswap32(o->di_gen);
143185222Ssam	n->di_uid = bswap32(o->di_uid);
144185222Ssam	n->di_gid = bswap32(o->di_gid);
145185222Ssam}
146185222Ssam
147185222Ssamvoid
148185222Ssamffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n)
149185222Ssam{
150185222Ssam	n->di_mode = bswap16(o->di_mode);
151185222Ssam	n->di_nlink = bswap16(o->di_nlink);
152185222Ssam	n->di_uid = bswap32(o->di_uid);
153185222Ssam	n->di_gid = bswap32(o->di_gid);
154185222Ssam	n->di_blksize = bswap32(o->di_blksize);
155185222Ssam	n->di_size = bswap64(o->di_size);
156185222Ssam	n->di_blocks = bswap64(o->di_blocks);
157185222Ssam	n->di_atime = bswap64(o->di_atime);
158185222Ssam	n->di_atimensec = bswap32(o->di_atimensec);
159185222Ssam	n->di_mtime = bswap64(o->di_mtime);
160185222Ssam	n->di_mtimensec = bswap32(o->di_mtimensec);
161185222Ssam	n->di_ctime = bswap64(o->di_ctime);
162185222Ssam	n->di_ctimensec = bswap32(o->di_ctimensec);
163185222Ssam	n->di_birthtime = bswap64(o->di_ctime);
164185222Ssam	n->di_birthnsec = bswap32(o->di_ctimensec);
165185222Ssam	n->di_gen = bswap32(o->di_gen);
166185222Ssam	n->di_kernflags = bswap32(o->di_kernflags);
167185222Ssam	n->di_flags = bswap32(o->di_flags);
168185222Ssam	n->di_extsize = bswap32(o->di_extsize);
169301784Sngie	memcpy(n->di_extb, o->di_extb, sizeof(n->di_extb));
170301784Sngie	memcpy(n->di_db, o->di_db, sizeof(n->di_db));
171301784Sngie	memcpy(n->di_ib, o->di_ib, sizeof(n->di_ib));
172185222Ssam}
173185222Ssam
174185222Ssamvoid
175185222Ssamffs_csum_swap(struct csum *o, struct csum *n, int size)
176185222Ssam{
177290597Sngie	size_t i;
178185222Ssam	u_int32_t *oint, *nint;
179290597Sngie
180185222Ssam	oint = (u_int32_t*)o;
181185222Ssam	nint = (u_int32_t*)n;
182185222Ssam
183185222Ssam	for (i = 0; i < size / sizeof(u_int32_t); i++)
184185222Ssam		nint[i] = bswap32(oint[i]);
185185222Ssam}
186185222Ssam
187185222Ssamvoid
188185222Ssamffs_csumtotal_swap(struct csum_total *o, struct csum_total *n)
189185222Ssam{
190185222Ssam	n->cs_ndir = bswap64(o->cs_ndir);
191185222Ssam	n->cs_nbfree = bswap64(o->cs_nbfree);
192185222Ssam	n->cs_nifree = bswap64(o->cs_nifree);
193185222Ssam	n->cs_nffree = bswap64(o->cs_nffree);
194185222Ssam}
195185222Ssam
196185222Ssam/*
197185222Ssam * Note that ffs_cg_swap may be called with o == n.
198185222Ssam */
199185222Ssamvoid
200185222Ssamffs_cg_swap(struct cg *o, struct cg *n, struct fs *fs)
201185222Ssam{
202185222Ssam	int i;
203185222Ssam	u_int32_t *n32, *o32;
204185222Ssam	u_int16_t *n16, *o16;
205185222Ssam	int32_t btotoff, boff, clustersumoff;
206185222Ssam
207185222Ssam	n->cg_firstfield = bswap32(o->cg_firstfield);
208185222Ssam	n->cg_magic = bswap32(o->cg_magic);
209185222Ssam	n->cg_old_time = bswap32(o->cg_old_time);
210185222Ssam	n->cg_cgx = bswap32(o->cg_cgx);
211185222Ssam	n->cg_old_ncyl = bswap16(o->cg_old_ncyl);
212185222Ssam	n->cg_old_niblk = bswap16(o->cg_old_niblk);
213185222Ssam	n->cg_ndblk = bswap32(o->cg_ndblk);
214185222Ssam	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
215185222Ssam	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
216185222Ssam	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
217185222Ssam	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
218185222Ssam	n->cg_rotor = bswap32(o->cg_rotor);
219185222Ssam	n->cg_frotor = bswap32(o->cg_frotor);
220185222Ssam	n->cg_irotor = bswap32(o->cg_irotor);
221185222Ssam	for (i = 0; i < MAXFRAG; i++)
222185222Ssam		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
223185222Ssam
224186261Ssam	n->cg_old_btotoff = bswap32(o->cg_old_btotoff);
225186261Ssam	n->cg_old_boff = bswap32(o->cg_old_boff);
226186261Ssam	n->cg_iusedoff = bswap32(o->cg_iusedoff);
227186261Ssam	n->cg_freeoff = bswap32(o->cg_freeoff);
228186261Ssam	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
229186261Ssam	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
230186261Ssam	n->cg_clusteroff = bswap32(o->cg_clusteroff);
231186261Ssam	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
232186261Ssam	n->cg_niblk = bswap32(o->cg_niblk);
233186261Ssam	n->cg_initediblk = bswap32(o->cg_initediblk);
234186261Ssam	n->cg_time = bswap64(o->cg_time);
235185222Ssam
236186261Ssam	if (fs->fs_magic == FS_UFS2_MAGIC)
237186261Ssam		return;
238185222Ssam
239186261Ssam	if (n->cg_magic == CG_MAGIC) {
240186261Ssam		btotoff = n->cg_old_btotoff;
241186261Ssam		boff = n->cg_old_boff;
242186261Ssam		clustersumoff = n->cg_clustersumoff;
243186261Ssam	} else {
244186261Ssam		btotoff = bswap32(n->cg_old_btotoff);
245186261Ssam		boff = bswap32(n->cg_old_boff);
246186261Ssam		clustersumoff = bswap32(n->cg_clustersumoff);
247186261Ssam	}
248186261Ssam	n32 = (u_int32_t *)((u_int8_t *)n + btotoff);
249186261Ssam	o32 = (u_int32_t *)((u_int8_t *)o + btotoff);
250186261Ssam	n16 = (u_int16_t *)((u_int8_t *)n + boff);
251186261Ssam	o16 = (u_int16_t *)((u_int8_t *)o + boff);
252185222Ssam
253186261Ssam	for (i = 0; i < fs->fs_old_cpg; i++)
254186261Ssam		n32[i] = bswap32(o32[i]);
255186261Ssam
256186261Ssam	for (i = 0; i < fs->fs_old_cpg * fs->fs_old_nrpos; i++)
257186261Ssam		n16[i] = bswap16(o16[i]);
258185222Ssam
259186261Ssam	n32 = (u_int32_t *)((u_int8_t *)n + clustersumoff);
260186261Ssam	o32 = (u_int32_t *)((u_int8_t *)o + clustersumoff);
261186261Ssam	for (i = 1; i < fs->fs_contigsumsize + 1; i++)
262186261Ssam		n32[i] = bswap32(o32[i]);
263185222Ssam}
264