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