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