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