]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_byte_order.c
xnu-344.12.2.tar.gz
[apple/xnu.git] / bsd / ufs / ufs / ufs_byte_order.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright 1998 Apple Computer, Inc.
23 *
24 * UFS byte swapping routines to make a big endian file system useful on a
25 * little endian machine.
26 *
27 * HISTORY
28 *
29 * 16 Feb 1998 A. Ramesh at Apple
30 * MacOSX version created.
31 */
32
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/quota.h>
36 #include <ufs/ufs/ufs_byte_order.h>
37 #include <architecture/byte_order.h>
38
39 #define byte_swap_longlong(thing) ((thing) = NXSwapLongLong(thing))
40 #define byte_swap_int(thing) ((thing) = NXSwapLong(thing))
41 #define byte_swap_short(thing) ((thing) = NXSwapShort(thing))
42
43 void
44 byte_swap_longlongs(unsigned long long *array, int count)
45 {
46 register unsigned long long i;
47
48 for (i = 0; i < count; i++)
49 byte_swap_longlong(array[i]);
50 }
51
52 void
53 byte_swap_ints(int *array, int count)
54 {
55 register int i;
56
57 for (i = 0; i < count; i++)
58 byte_swap_int(array[i]);
59 }
60
61 void
62 byte_swap_shorts(short *array, int count)
63 {
64 register int i;
65
66 for (i = 0; i < count; i++)
67 byte_swap_short(array[i]);
68 }
69
70 void
71 byte_swap_sbin(struct fs *sb)
72 {
73 u_int16_t *usptr;
74 unsigned long size;
75
76 byte_swap_ints(((int32_t *)&sb->fs_firstfield), 52);
77 byte_swap_int(sb->fs_cgrotor);
78 byte_swap_int(sb->fs_cpc);
79 byte_swap_shorts((int16_t *)sb->fs_opostbl, 16 * 8);
80 byte_swap_ints((int32_t *)sb->fs_sparecon, 50);
81 byte_swap_ints((int32_t *)&sb->fs_contigsumsize, 3);
82 byte_swap_longlongs((u_int64_t *)&sb->fs_maxfilesize,3);
83 byte_swap_ints((int32_t *)&sb->fs_state, 6);
84
85 /* Got these magic numbers from mkfs.c in newfs */
86 if (sb->fs_nrpos != 8 || sb->fs_cpc > 16) {
87 usptr = (u_int16_t *)((u_int8_t *)(sb) + (sb)->fs_postbloff);
88 size = sb->fs_cpc * sb->fs_nrpos;
89 byte_swap_shorts(usptr,size); /* fs_postbloff */
90 }
91 }
92
93 void
94 byte_swap_sbout(struct fs *sb)
95 {
96 u_int16_t *usptr;
97 unsigned long size;
98 /* Got these magic numbers from mkfs.c in newfs */
99 if (sb->fs_nrpos != 8 || sb->fs_cpc > 16) {
100 usptr = (u_int16_t *)((u_int8_t *)(sb) + (sb)->fs_postbloff);
101 size = sb->fs_cpc * sb->fs_nrpos;
102 byte_swap_shorts(usptr,size); /* fs_postbloff */
103 }
104
105 byte_swap_ints(((int32_t *)&sb->fs_firstfield), 52);
106 byte_swap_int(sb->fs_cgrotor);
107 byte_swap_int(sb->fs_cpc);
108 byte_swap_shorts((int16_t *)sb->fs_opostbl, 16 * 8);
109 byte_swap_ints((int32_t *)sb->fs_sparecon, 50);
110 byte_swap_ints((int32_t *)&sb->fs_contigsumsize, 3);
111 byte_swap_longlongs((u_int64_t *)&sb->fs_maxfilesize,3);
112 byte_swap_ints((int32_t *)&sb->fs_state, 6);
113 }
114
115 void
116 byte_swap_csum(struct csum *cs)
117 {
118 byte_swap_ints((int *) cs, sizeof(struct csum) / sizeof(int32_t));
119 }
120
121 /* This is for the new 4.4 cylinder group block */
122 void
123 byte_swap_cgin(struct cg *cg, struct fs * fs)
124 {
125 int32_t * ulptr;
126 int16_t * usptr;
127 int size;
128
129 byte_swap_int(cg->cg_firstfield);
130 byte_swap_int(cg->cg_magic);
131 byte_swap_int(cg->cg_time);
132 byte_swap_int(cg->cg_cgx);
133 byte_swap_short(cg->cg_ncyl);
134 byte_swap_short(cg->cg_niblk);
135 byte_swap_int(cg->cg_ndblk);
136 byte_swap_csum(&cg->cg_cs);
137 byte_swap_int(cg->cg_rotor);
138 byte_swap_int(cg->cg_frotor);
139 byte_swap_int(cg->cg_irotor);
140 byte_swap_ints(cg->cg_frsum, MAXFRAG);
141 byte_swap_int(cg->cg_iusedoff);
142 byte_swap_int(cg->cg_freeoff);
143 byte_swap_int(cg->cg_nextfreeoff);
144 byte_swap_int(cg->cg_clusteroff);
145 byte_swap_int(cg->cg_nclusterblks);
146 byte_swap_ints(&cg->cg_sparecon, 13);
147
148 byte_swap_int(cg->cg_btotoff);
149 ulptr = ((int32_t *)((u_int8_t *)(cg) + (cg)->cg_btotoff));
150 size = fs->fs_cpg;
151 byte_swap_ints(ulptr, size); /*cg_btotoff*/
152
153 byte_swap_int(cg->cg_boff);
154 usptr = ((int16_t *)((u_int8_t *)(cg) + (cg)->cg_boff));
155 size = fs->fs_cpg * fs->fs_nrpos;
156 byte_swap_shorts(usptr,size); /*cg_boff*/
157
158 byte_swap_int(cg->cg_clustersumoff);
159
160 if ((unsigned int)fs->fs_contigsumsize > 0) {
161
162 ulptr = ((int32_t *)((u_int8_t *)(cg) + (cg)->cg_clustersumoff));
163 size = (fs->fs_contigsumsize + 1);
164 byte_swap_ints(ulptr, size); /*cg_clustersumoff*/
165 }
166
167 }
168
169 /* This is for the new 4.4 cylinder group block */
170 void
171 byte_swap_cgout(struct cg *cg, struct fs * fs)
172 {
173 int32_t * ulptr;
174 int16_t * usptr;
175 int size;
176
177 byte_swap_int(cg->cg_firstfield);
178 byte_swap_int(cg->cg_magic);
179 byte_swap_int(cg->cg_time);
180 byte_swap_int(cg->cg_cgx);
181 byte_swap_short(cg->cg_ncyl);
182 byte_swap_short(cg->cg_niblk);
183 byte_swap_int(cg->cg_ndblk);
184 byte_swap_csum(&cg->cg_cs);
185 byte_swap_int(cg->cg_rotor);
186 byte_swap_int(cg->cg_frotor);
187 byte_swap_int(cg->cg_irotor);
188 byte_swap_ints(cg->cg_frsum, MAXFRAG);
189 byte_swap_int(cg->cg_freeoff);
190 byte_swap_int(cg->cg_nextfreeoff);
191 byte_swap_int(cg->cg_nclusterblks);
192 byte_swap_ints(&cg->cg_sparecon, 13);
193
194 byte_swap_int(cg->cg_iusedoff);
195 byte_swap_int(cg->cg_clusteroff);
196 ulptr = ((int32_t *)((u_int8_t *)(cg) + (cg)->cg_btotoff));
197 size = fs->fs_cpg;
198 byte_swap_ints(ulptr, size); /*cg_btotoff*/
199 byte_swap_int(cg->cg_btotoff);
200
201 usptr = ((int16_t *)((u_int8_t *)(cg) + (cg)->cg_boff));
202 size = fs->fs_cpg * fs->fs_nrpos;
203 byte_swap_shorts(usptr,size); /*cg_boff*/
204 byte_swap_int(cg->cg_boff);
205
206 if ((unsigned int)fs->fs_contigsumsize > 0) {
207 ulptr = ((int32_t *)((u_int8_t *)(cg) + (cg)->cg_clustersumoff));
208 size = (fs->fs_contigsumsize + 1);
209 byte_swap_ints(ulptr, size); /*cg_clustersumoff*/
210
211 }
212 byte_swap_int(cg->cg_clustersumoff);
213
214 }
215
216 /* This value MUST correspond to the value set in the ffs_mounts */
217
218 #define RESYMLNKLEN 60
219
220 void
221 byte_swap_inode_in(struct dinode *di, struct inode *ip)
222 {
223 int i;
224
225 ip->i_mode = NXSwapShort(di->di_mode);
226 ip->i_nlink = NXSwapShort(di->di_nlink);
227 ip->i_oldids[0] = NXSwapShort(di->di_u.oldids[0]);
228 ip->i_oldids[1] = NXSwapShort(di->di_u.oldids[1]);
229 ip->i_size = NXSwapLongLong(di->di_size);
230 ip->i_atime = NXSwapLong(di->di_atime);
231 ip->i_atimensec = NXSwapLong(di->di_atimensec);
232 ip->i_mtime = NXSwapLong(di->di_mtime);
233 ip->i_mtimensec = NXSwapLong(di->di_mtimensec);
234 ip->i_ctime = NXSwapLong(di->di_ctime);
235 ip->i_ctimensec = NXSwapLong(di->di_ctimensec);
236 if (((ip->i_mode & IFMT) == IFLNK ) && (ip->i_size <= RESYMLNKLEN)) {
237 bcopy(&di->di_shortlink, &ip->i_shortlink, RESYMLNKLEN);
238 } else {
239 for (i=0; i < NDADDR; i++) /* direct blocks */
240 ip->i_db[i] = NXSwapLong(di->di_db[i]);
241 for (i=0; i < NIADDR; i++) /* indirect blocks */
242 ip->i_ib[i] = NXSwapLong(di->di_ib[i]);
243 }
244 ip->i_flags = NXSwapLong(di->di_flags);
245 ip->i_blocks = NXSwapLong(di->di_blocks);
246 ip->i_gen = NXSwapLong(di->di_gen);
247 ip->i_uid = NXSwapLong(di->di_uid);
248 ip->i_gid = NXSwapLong(di->di_gid);
249 ip->i_spare[0] = NXSwapLong(di->di_spare[0]);
250 ip->i_spare[1] = NXSwapLong(di->di_spare[1]);
251 }
252
253 void
254 byte_swap_inode_out(struct inode *ip, struct dinode *di)
255 {
256 int i;
257 int mode, inosize;
258
259 mode = (ip->i_mode & IFMT);
260 inosize = ip->i_size;
261
262 di->di_mode = NXSwapShort(ip->i_mode);
263 di->di_nlink = NXSwapShort(ip->i_nlink);
264 di->di_u.oldids[0] = NXSwapShort(ip->i_oldids[0]);
265 di->di_u.oldids[1] = NXSwapShort(ip->i_oldids[1]);
266 di->di_size = NXSwapLongLong(ip->i_size);
267 di->di_atime = NXSwapLong(ip->i_atime);
268 di->di_atimensec = NXSwapLong(ip->i_atimensec);
269 di->di_mtime = NXSwapLong(ip->i_mtime);
270 di->di_mtimensec = NXSwapLong(ip->i_mtimensec);
271 di->di_ctime = NXSwapLong(ip->i_ctime);
272 di->di_ctimensec = NXSwapLong(ip->i_ctimensec);
273 if ((mode == IFLNK) && (inosize <= RESYMLNKLEN)) {
274 bcopy( &ip->i_shortlink, &di->di_shortlink, RESYMLNKLEN);
275 } else {
276 for (i=0; i < NDADDR; i++) /* direct blocks */
277 di->di_db[i] = NXSwapLong(ip->i_db[i]);
278 for (i=0; i < NIADDR; i++) /* indirect blocks */
279 di->di_ib[i] = NXSwapLong(ip->i_ib[i]);
280 }
281 di->di_flags = NXSwapLong(ip->i_flags);
282 di->di_blocks = NXSwapLong(ip->i_blocks);
283 di->di_gen = NXSwapLong(ip->i_gen);
284 di->di_uid = NXSwapLong(ip->i_uid);
285 di->di_gid = NXSwapLong(ip->i_gid);
286 di->di_spare[0] = NXSwapLong(ip->i_spare[0]);
287 di->di_spare[1] = NXSwapLong(ip->i_spare[1]);
288 }
289
290 void
291 byte_swap_direct(struct direct *dirp)
292 {
293 byte_swap_int(dirp->d_ino);
294 byte_swap_short(dirp->d_reclen);
295 }
296
297 void
298 byte_swap_dir_block_in(char *addr, int count)
299 {
300 struct direct *ep = (struct direct *) addr;
301 int entryoffsetinblk = 0;
302
303 while (entryoffsetinblk < count) {
304 ep = (struct direct *) (entryoffsetinblk + addr);
305 byte_swap_int(ep->d_ino);
306 byte_swap_short(ep->d_reclen);
307 entryoffsetinblk += ep->d_reclen;
308 if (ep->d_reclen < 12) /* handle garbage in dirs */
309 break;
310 }
311 }
312
313 void
314 byte_swap_dir_out(char *addr, int count)
315 {
316 struct direct *ep = (struct direct *) addr;
317 int entryoffsetinblk = 0;
318 int reclen;
319
320 while (entryoffsetinblk < count) {
321 ep = (struct direct *) (entryoffsetinblk + addr);
322 reclen = ep->d_reclen;
323 entryoffsetinblk += reclen;
324 byte_swap_int(ep->d_ino);
325 byte_swap_short(ep->d_reclen);
326 if (reclen < 12)
327 break;
328 }
329 }
330
331 void
332 byte_swap_dir_block_out(struct buf *bp)
333 {
334 struct direct *ep = (struct direct *) bp->b_data;
335 int reclen, entryoffsetinblk = 0;
336
337 while (entryoffsetinblk < bp->b_bcount) {
338 ep = (struct direct *) (entryoffsetinblk + bp->b_data);
339 reclen = ep->d_reclen;
340 entryoffsetinblk += reclen;
341 byte_swap_int(ep->d_ino);
342 byte_swap_short(ep->d_reclen);
343 if (reclen < 12)
344 break;
345 }
346 }
347
348 void
349 byte_swap_dirtemplate_in(struct dirtemplate *dirt)
350 {
351 byte_swap_int(dirt->dot_ino);
352 byte_swap_short(dirt->dot_reclen);
353 byte_swap_int(dirt->dotdot_ino);
354 byte_swap_short(dirt->dotdot_reclen);
355 }
356
357 void
358 byte_swap_minidir_in(struct direct *dirp)
359 {
360 byte_swap_int(dirp->d_ino);
361 byte_swap_short(dirp->d_reclen);
362 }
363
364 #if 0
365 /* This is for the compatability (old) cylinder group block */
366 void
367 byte_swap_ocylgroup(struct cg *cg)
368 {
369 byte_swap_int(cg->cg_time);
370 byte_swap_int(cg->cg_cgx);
371 byte_swap_short(cg->cg_ncyl);
372 byte_swap_short(cg->cg_niblk);
373 byte_swap_int(cg->cg_ndblk);
374 byte_swap_csum(&cg->cg_cs);
375 byte_swap_int(cg->cg_rotor);
376 byte_swap_int(cg->cg_frotor);
377 byte_swap_int(cg->cg_irotor);
378 byte_swap_ints(&cg->cg_frsum, 8);
379 byte_swap_ints(&cg->cg_btot, 32);
380 byte_swap_shorts((short *)&cg->cg_b, 32 * 8);
381 byte_swap_int(cg->cg_magic);
382 }
383 #endif /* 0 */