]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_byte_order.c
d152ffdccdf5158f0fa4abd3bfe0a5abdee8fc32
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright 1998 Apple Computer, Inc.
32 * UFS byte swapping routines to make a big endian file system useful on a
33 * little endian machine.
37 * 16 Feb 1998 A. Ramesh at Apple
38 * MacOSX version created.
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/quota.h>
44 #include <ufs/ufs/ufs_byte_order.h>
45 #include <architecture/byte_order.h>
47 #define byte_swap_longlong(thing) ((thing) = NXSwapLongLong(thing))
48 #define byte_swap_int(thing) ((thing) = NXSwapLong(thing))
49 #define byte_swap_short(thing) ((thing) = NXSwapShort(thing))
52 byte_swap_longlongs(unsigned long long *array
, int count
)
54 register unsigned long long i
;
56 for (i
= 0; i
< count
; i
++)
57 byte_swap_longlong(array
[i
]);
61 byte_swap_ints(int *array
, int count
)
65 for (i
= 0; i
< count
; i
++)
66 byte_swap_int(array
[i
]);
70 byte_swap_shorts(short *array
, int count
)
74 for (i
= 0; i
< count
; i
++)
75 byte_swap_short(array
[i
]);
79 byte_swap_sbin(struct fs
*sb
)
84 byte_swap_ints(((int32_t *)&sb
->fs_firstfield
), 52);
85 byte_swap_int(sb
->fs_cgrotor
);
86 byte_swap_int(sb
->fs_cpc
);
87 byte_swap_shorts((int16_t *)sb
->fs_opostbl
,
88 sizeof(sb
->fs_opostbl
) / sizeof(int16_t));
89 byte_swap_int(sb
->fs_avgfilesize
);
90 byte_swap_int(sb
->fs_avgfpdir
);
91 byte_swap_ints((int32_t *)sb
->fs_sparecon
,
92 sizeof(sb
->fs_sparecon
) / sizeof(int32_t));
93 byte_swap_ints((int32_t *)&sb
->fs_contigsumsize
, 3);
94 byte_swap_longlongs((u_int64_t
*)&sb
->fs_maxfilesize
,3);
95 byte_swap_ints((int32_t *)&sb
->fs_state
, 6);
97 /* Got these magic numbers from mkfs.c in newfs */
98 if (sb
->fs_nrpos
!= 8 || sb
->fs_cpc
> 16) {
99 usptr
= (u_int16_t
*)((u_int8_t
*)(sb
) + (sb
)->fs_postbloff
);
100 size
= sb
->fs_cpc
* sb
->fs_nrpos
;
101 byte_swap_shorts(usptr
,size
); /* fs_postbloff */
106 byte_swap_sbout(struct fs
*sb
)
110 /* Got these magic numbers from mkfs.c in newfs */
111 if (sb
->fs_nrpos
!= 8 || sb
->fs_cpc
> 16) {
112 usptr
= (u_int16_t
*)((u_int8_t
*)(sb
) + (sb
)->fs_postbloff
);
113 size
= sb
->fs_cpc
* sb
->fs_nrpos
;
114 byte_swap_shorts(usptr
,size
); /* fs_postbloff */
117 byte_swap_ints(((int32_t *)&sb
->fs_firstfield
), 52);
118 byte_swap_int(sb
->fs_cgrotor
);
119 byte_swap_int(sb
->fs_cpc
);
120 byte_swap_shorts((int16_t *)sb
->fs_opostbl
,
121 sizeof(sb
->fs_opostbl
) / sizeof(int16_t));
122 byte_swap_int(sb
->fs_avgfilesize
);
123 byte_swap_int(sb
->fs_avgfpdir
);
124 byte_swap_ints((int32_t *)sb
->fs_sparecon
,
125 sizeof(sb
->fs_sparecon
) / sizeof(int32_t));
126 byte_swap_ints((int32_t *)&sb
->fs_contigsumsize
, 3);
127 byte_swap_longlongs((u_int64_t
*)&sb
->fs_maxfilesize
,3);
128 byte_swap_ints((int32_t *)&sb
->fs_state
, 6);
132 byte_swap_csum(struct csum
*cs
)
134 byte_swap_ints((int *) cs
, sizeof(struct csum
) / sizeof(int32_t));
137 /* This is for the new 4.4 cylinder group block */
139 byte_swap_cgin(struct cg
*cg
, struct fs
* fs
)
145 byte_swap_int(cg
->cg_firstfield
);
146 byte_swap_int(cg
->cg_magic
);
147 byte_swap_int(cg
->cg_time
);
148 byte_swap_int(cg
->cg_cgx
);
149 byte_swap_short(cg
->cg_ncyl
);
150 byte_swap_short(cg
->cg_niblk
);
151 byte_swap_int(cg
->cg_ndblk
);
152 byte_swap_csum(&cg
->cg_cs
);
153 byte_swap_int(cg
->cg_rotor
);
154 byte_swap_int(cg
->cg_frotor
);
155 byte_swap_int(cg
->cg_irotor
);
156 byte_swap_ints(cg
->cg_frsum
, MAXFRAG
);
157 byte_swap_int(cg
->cg_iusedoff
);
158 byte_swap_int(cg
->cg_freeoff
);
159 byte_swap_int(cg
->cg_nextfreeoff
);
160 byte_swap_int(cg
->cg_clusteroff
);
161 byte_swap_int(cg
->cg_nclusterblks
);
162 byte_swap_ints((int *)&cg
->cg_sparecon
, 13);
164 byte_swap_int(cg
->cg_btotoff
);
165 ulptr
= ((int32_t *)((u_int8_t
*)(cg
) + (cg
)->cg_btotoff
));
167 byte_swap_ints(ulptr
, size
); /*cg_btotoff*/
169 byte_swap_int(cg
->cg_boff
);
170 usptr
= ((int16_t *)((u_int8_t
*)(cg
) + (cg
)->cg_boff
));
171 size
= fs
->fs_cpg
* fs
->fs_nrpos
;
172 byte_swap_shorts(usptr
,size
); /*cg_boff*/
174 byte_swap_int(cg
->cg_clustersumoff
);
176 if ((unsigned int)fs
->fs_contigsumsize
> 0) {
178 ulptr
= ((int32_t *)((u_int8_t
*)(cg
) + (cg
)->cg_clustersumoff
));
179 size
= (fs
->fs_contigsumsize
+ 1);
180 byte_swap_ints(ulptr
, size
); /*cg_clustersumoff*/
185 /* This is for the new 4.4 cylinder group block */
187 byte_swap_cgout(struct cg
*cg
, struct fs
* fs
)
193 byte_swap_int(cg
->cg_firstfield
);
194 byte_swap_int(cg
->cg_magic
);
195 byte_swap_int(cg
->cg_time
);
196 byte_swap_int(cg
->cg_cgx
);
197 byte_swap_short(cg
->cg_ncyl
);
198 byte_swap_short(cg
->cg_niblk
);
199 byte_swap_int(cg
->cg_ndblk
);
200 byte_swap_csum(&cg
->cg_cs
);
201 byte_swap_int(cg
->cg_rotor
);
202 byte_swap_int(cg
->cg_frotor
);
203 byte_swap_int(cg
->cg_irotor
);
204 byte_swap_ints(cg
->cg_frsum
, MAXFRAG
);
205 byte_swap_int(cg
->cg_freeoff
);
206 byte_swap_int(cg
->cg_nextfreeoff
);
207 byte_swap_int(cg
->cg_nclusterblks
);
208 byte_swap_ints((int *)&cg
->cg_sparecon
, 13);
210 byte_swap_int(cg
->cg_iusedoff
);
211 byte_swap_int(cg
->cg_clusteroff
);
212 ulptr
= ((int32_t *)((u_int8_t
*)(cg
) + (cg
)->cg_btotoff
));
214 byte_swap_ints(ulptr
, size
); /*cg_btotoff*/
215 byte_swap_int(cg
->cg_btotoff
);
217 usptr
= ((int16_t *)((u_int8_t
*)(cg
) + (cg
)->cg_boff
));
218 size
= fs
->fs_cpg
* fs
->fs_nrpos
;
219 byte_swap_shorts(usptr
,size
); /*cg_boff*/
220 byte_swap_int(cg
->cg_boff
);
222 if ((unsigned int)fs
->fs_contigsumsize
> 0) {
223 ulptr
= ((int32_t *)((u_int8_t
*)(cg
) + (cg
)->cg_clustersumoff
));
224 size
= (fs
->fs_contigsumsize
+ 1);
225 byte_swap_ints(ulptr
, size
); /*cg_clustersumoff*/
228 byte_swap_int(cg
->cg_clustersumoff
);
232 /* This value MUST correspond to the value set in the ffs_mounts */
234 #define RESYMLNKLEN 60
237 byte_swap_inode_in(struct dinode
*di
, struct inode
*ip
)
241 ip
->i_mode
= NXSwapShort(di
->di_mode
);
242 ip
->i_nlink
= NXSwapShort(di
->di_nlink
);
243 ip
->i_oldids
[0] = NXSwapShort(di
->di_u
.oldids
[0]);
244 ip
->i_oldids
[1] = NXSwapShort(di
->di_u
.oldids
[1]);
245 ip
->i_size
= NXSwapLongLong(di
->di_size
);
246 ip
->i_atime
= NXSwapLong(di
->di_atime
);
247 ip
->i_atimensec
= NXSwapLong(di
->di_atimensec
);
248 ip
->i_mtime
= NXSwapLong(di
->di_mtime
);
249 ip
->i_mtimensec
= NXSwapLong(di
->di_mtimensec
);
250 ip
->i_ctime
= NXSwapLong(di
->di_ctime
);
251 ip
->i_ctimensec
= NXSwapLong(di
->di_ctimensec
);
252 if (((ip
->i_mode
& IFMT
) == IFLNK
) && (ip
->i_size
<= RESYMLNKLEN
)) {
253 bcopy(&di
->di_shortlink
, &ip
->i_shortlink
, RESYMLNKLEN
);
255 for (i
=0; i
< NDADDR
; i
++) /* direct blocks */
256 ip
->i_db
[i
] = NXSwapLong(di
->di_db
[i
]);
257 for (i
=0; i
< NIADDR
; i
++) /* indirect blocks */
258 ip
->i_ib
[i
] = NXSwapLong(di
->di_ib
[i
]);
260 ip
->i_flags
= NXSwapLong(di
->di_flags
);
261 ip
->i_blocks
= NXSwapLong(di
->di_blocks
);
262 ip
->i_gen
= NXSwapLong(di
->di_gen
);
263 ip
->i_uid
= NXSwapLong(di
->di_uid
);
264 ip
->i_gid
= NXSwapLong(di
->di_gid
);
265 ip
->i_spare
[0] = NXSwapLong(di
->di_spare
[0]);
266 ip
->i_spare
[1] = NXSwapLong(di
->di_spare
[1]);
270 byte_swap_inode_out(struct inode
*ip
, struct dinode
*di
)
275 mode
= (ip
->i_mode
& IFMT
);
276 inosize
= ip
->i_size
;
278 di
->di_mode
= NXSwapShort(ip
->i_mode
);
279 di
->di_nlink
= NXSwapShort(ip
->i_nlink
);
280 di
->di_u
.oldids
[0] = NXSwapShort(ip
->i_oldids
[0]);
281 di
->di_u
.oldids
[1] = NXSwapShort(ip
->i_oldids
[1]);
282 di
->di_size
= NXSwapLongLong(ip
->i_size
);
283 di
->di_atime
= NXSwapLong(ip
->i_atime
);
284 di
->di_atimensec
= NXSwapLong(ip
->i_atimensec
);
285 di
->di_mtime
= NXSwapLong(ip
->i_mtime
);
286 di
->di_mtimensec
= NXSwapLong(ip
->i_mtimensec
);
287 di
->di_ctime
= NXSwapLong(ip
->i_ctime
);
288 di
->di_ctimensec
= NXSwapLong(ip
->i_ctimensec
);
289 if ((mode
== IFLNK
) && (inosize
<= RESYMLNKLEN
)) {
290 bcopy( &ip
->i_shortlink
, &di
->di_shortlink
, RESYMLNKLEN
);
292 for (i
=0; i
< NDADDR
; i
++) /* direct blocks */
293 di
->di_db
[i
] = NXSwapLong(ip
->i_db
[i
]);
294 for (i
=0; i
< NIADDR
; i
++) /* indirect blocks */
295 di
->di_ib
[i
] = NXSwapLong(ip
->i_ib
[i
]);
297 di
->di_flags
= NXSwapLong(ip
->i_flags
);
298 di
->di_blocks
= NXSwapLong(ip
->i_blocks
);
299 di
->di_gen
= NXSwapLong(ip
->i_gen
);
300 di
->di_uid
= NXSwapLong(ip
->i_uid
);
301 di
->di_gid
= NXSwapLong(ip
->i_gid
);
302 di
->di_spare
[0] = NXSwapLong(ip
->i_spare
[0]);
303 di
->di_spare
[1] = NXSwapLong(ip
->i_spare
[1]);
307 byte_swap_direct(struct direct
*dirp
)
309 byte_swap_int(dirp
->d_ino
);
310 byte_swap_short(dirp
->d_reclen
);
314 byte_swap_dir_block_in(char *addr
, int count
)
316 struct direct
*ep
= (struct direct
*) addr
;
317 int entryoffsetinblk
= 0;
319 while (entryoffsetinblk
< count
) {
320 ep
= (struct direct
*) (entryoffsetinblk
+ addr
);
321 byte_swap_int(ep
->d_ino
);
322 byte_swap_short(ep
->d_reclen
);
323 entryoffsetinblk
+= ep
->d_reclen
;
324 if (ep
->d_reclen
< 12) /* handle garbage in dirs */
330 byte_swap_dir_out(char *addr
, int count
)
332 struct direct
*ep
= (struct direct
*) addr
;
333 int entryoffsetinblk
= 0;
336 while (entryoffsetinblk
< count
) {
337 ep
= (struct direct
*) (entryoffsetinblk
+ addr
);
338 reclen
= ep
->d_reclen
;
339 entryoffsetinblk
+= reclen
;
340 byte_swap_int(ep
->d_ino
);
341 byte_swap_short(ep
->d_reclen
);
348 byte_swap_dir_block_out(struct buf
*bp
)
350 struct direct
*ep
= (struct direct
*) buf_dataptr(bp
);
351 int reclen
, entryoffsetinblk
= 0;
353 while (entryoffsetinblk
< buf_count(bp
)) {
354 ep
= (struct direct
*) (entryoffsetinblk
+ buf_dataptr(bp
));
355 reclen
= ep
->d_reclen
;
356 entryoffsetinblk
+= reclen
;
357 byte_swap_int(ep
->d_ino
);
358 byte_swap_short(ep
->d_reclen
);
365 byte_swap_dirtemplate_in(struct dirtemplate
*dirt
)
367 byte_swap_int(dirt
->dot_ino
);
368 byte_swap_short(dirt
->dot_reclen
);
369 byte_swap_int(dirt
->dotdot_ino
);
370 byte_swap_short(dirt
->dotdot_reclen
);
374 byte_swap_minidir_in(struct direct
*dirp
)
376 byte_swap_int(dirp
->d_ino
);
377 byte_swap_short(dirp
->d_reclen
);
381 /* This is for the compatability (old) cylinder group block */
383 byte_swap_ocylgroup(struct cg
*cg
)
385 byte_swap_int(cg
->cg_time
);
386 byte_swap_int(cg
->cg_cgx
);
387 byte_swap_short(cg
->cg_ncyl
);
388 byte_swap_short(cg
->cg_niblk
);
389 byte_swap_int(cg
->cg_ndblk
);
390 byte_swap_csum(&cg
->cg_cs
);
391 byte_swap_int(cg
->cg_rotor
);
392 byte_swap_int(cg
->cg_frotor
);
393 byte_swap_int(cg
->cg_irotor
);
394 byte_swap_ints(&cg
->cg_frsum
, 8);
395 byte_swap_ints(&cg
->cg_btot
, 32);
396 byte_swap_shorts((short *)&cg
->cg_b
, 32 * 8);
397 byte_swap_int(cg
->cg_magic
);