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