]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_byte_order.c
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright 1998 Apple Computer, Inc.
24 * UFS byte swapping routines to make a big endian file system useful on a
25 * little endian machine.
29 * 16 Feb 1998 A. Ramesh at Apple
30 * MacOSX version created.
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>
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))
44 byte_swap_longlongs(unsigned long long *array
, int count
)
46 register unsigned long long i
;
48 for (i
= 0; i
< count
; i
++)
49 byte_swap_longlong(array
[i
]);
53 byte_swap_ints(int *array
, int count
)
57 for (i
= 0; i
< count
; i
++)
58 byte_swap_int(array
[i
]);
62 byte_swap_shorts(short *array
, int count
)
66 for (i
= 0; i
< count
; i
++)
67 byte_swap_short(array
[i
]);
71 byte_swap_sbin(struct fs
*sb
)
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
,
80 sizeof(sb
->fs_opostbl
) / sizeof(int16_t));
81 byte_swap_int(sb
->fs_avgfilesize
);
82 byte_swap_int(sb
->fs_avgfpdir
);
83 byte_swap_ints((int32_t *)sb
->fs_sparecon
,
84 sizeof(sb
->fs_sparecon
) / sizeof(int32_t));
85 byte_swap_ints((int32_t *)&sb
->fs_contigsumsize
, 3);
86 byte_swap_longlongs((u_int64_t
*)&sb
->fs_maxfilesize
,3);
87 byte_swap_ints((int32_t *)&sb
->fs_state
, 6);
89 /* Got these magic numbers from mkfs.c in newfs */
90 if (sb
->fs_nrpos
!= 8 || sb
->fs_cpc
> 16) {
91 usptr
= (u_int16_t
*)((u_int8_t
*)(sb
) + (sb
)->fs_postbloff
);
92 size
= sb
->fs_cpc
* sb
->fs_nrpos
;
93 byte_swap_shorts(usptr
,size
); /* fs_postbloff */
98 byte_swap_sbout(struct fs
*sb
)
102 /* Got these magic numbers from mkfs.c in newfs */
103 if (sb
->fs_nrpos
!= 8 || sb
->fs_cpc
> 16) {
104 usptr
= (u_int16_t
*)((u_int8_t
*)(sb
) + (sb
)->fs_postbloff
);
105 size
= sb
->fs_cpc
* sb
->fs_nrpos
;
106 byte_swap_shorts(usptr
,size
); /* fs_postbloff */
109 byte_swap_ints(((int32_t *)&sb
->fs_firstfield
), 52);
110 byte_swap_int(sb
->fs_cgrotor
);
111 byte_swap_int(sb
->fs_cpc
);
112 byte_swap_shorts((int16_t *)sb
->fs_opostbl
,
113 sizeof(sb
->fs_opostbl
) / sizeof(int16_t));
114 byte_swap_int(sb
->fs_avgfilesize
);
115 byte_swap_int(sb
->fs_avgfpdir
);
116 byte_swap_ints((int32_t *)sb
->fs_sparecon
,
117 sizeof(sb
->fs_sparecon
) / sizeof(int32_t));
118 byte_swap_ints((int32_t *)&sb
->fs_contigsumsize
, 3);
119 byte_swap_longlongs((u_int64_t
*)&sb
->fs_maxfilesize
,3);
120 byte_swap_ints((int32_t *)&sb
->fs_state
, 6);
124 byte_swap_csum(struct csum
*cs
)
126 byte_swap_ints((int *) cs
, sizeof(struct csum
) / sizeof(int32_t));
129 /* This is for the new 4.4 cylinder group block */
131 byte_swap_cgin(struct cg
*cg
, struct fs
* fs
)
137 byte_swap_int(cg
->cg_firstfield
);
138 byte_swap_int(cg
->cg_magic
);
139 byte_swap_int(cg
->cg_time
);
140 byte_swap_int(cg
->cg_cgx
);
141 byte_swap_short(cg
->cg_ncyl
);
142 byte_swap_short(cg
->cg_niblk
);
143 byte_swap_int(cg
->cg_ndblk
);
144 byte_swap_csum(&cg
->cg_cs
);
145 byte_swap_int(cg
->cg_rotor
);
146 byte_swap_int(cg
->cg_frotor
);
147 byte_swap_int(cg
->cg_irotor
);
148 byte_swap_ints(cg
->cg_frsum
, MAXFRAG
);
149 byte_swap_int(cg
->cg_iusedoff
);
150 byte_swap_int(cg
->cg_freeoff
);
151 byte_swap_int(cg
->cg_nextfreeoff
);
152 byte_swap_int(cg
->cg_clusteroff
);
153 byte_swap_int(cg
->cg_nclusterblks
);
154 byte_swap_ints((int *)&cg
->cg_sparecon
, 13);
156 byte_swap_int(cg
->cg_btotoff
);
157 ulptr
= ((int32_t *)((u_int8_t
*)(cg
) + (cg
)->cg_btotoff
));
159 byte_swap_ints(ulptr
, size
); /*cg_btotoff*/
161 byte_swap_int(cg
->cg_boff
);
162 usptr
= ((int16_t *)((u_int8_t
*)(cg
) + (cg
)->cg_boff
));
163 size
= fs
->fs_cpg
* fs
->fs_nrpos
;
164 byte_swap_shorts(usptr
,size
); /*cg_boff*/
166 byte_swap_int(cg
->cg_clustersumoff
);
168 if ((unsigned int)fs
->fs_contigsumsize
> 0) {
170 ulptr
= ((int32_t *)((u_int8_t
*)(cg
) + (cg
)->cg_clustersumoff
));
171 size
= (fs
->fs_contigsumsize
+ 1);
172 byte_swap_ints(ulptr
, size
); /*cg_clustersumoff*/
177 /* This is for the new 4.4 cylinder group block */
179 byte_swap_cgout(struct cg
*cg
, struct fs
* fs
)
185 byte_swap_int(cg
->cg_firstfield
);
186 byte_swap_int(cg
->cg_magic
);
187 byte_swap_int(cg
->cg_time
);
188 byte_swap_int(cg
->cg_cgx
);
189 byte_swap_short(cg
->cg_ncyl
);
190 byte_swap_short(cg
->cg_niblk
);
191 byte_swap_int(cg
->cg_ndblk
);
192 byte_swap_csum(&cg
->cg_cs
);
193 byte_swap_int(cg
->cg_rotor
);
194 byte_swap_int(cg
->cg_frotor
);
195 byte_swap_int(cg
->cg_irotor
);
196 byte_swap_ints(cg
->cg_frsum
, MAXFRAG
);
197 byte_swap_int(cg
->cg_freeoff
);
198 byte_swap_int(cg
->cg_nextfreeoff
);
199 byte_swap_int(cg
->cg_nclusterblks
);
200 byte_swap_ints((int *)&cg
->cg_sparecon
, 13);
202 byte_swap_int(cg
->cg_iusedoff
);
203 byte_swap_int(cg
->cg_clusteroff
);
204 ulptr
= ((int32_t *)((u_int8_t
*)(cg
) + (cg
)->cg_btotoff
));
206 byte_swap_ints(ulptr
, size
); /*cg_btotoff*/
207 byte_swap_int(cg
->cg_btotoff
);
209 usptr
= ((int16_t *)((u_int8_t
*)(cg
) + (cg
)->cg_boff
));
210 size
= fs
->fs_cpg
* fs
->fs_nrpos
;
211 byte_swap_shorts(usptr
,size
); /*cg_boff*/
212 byte_swap_int(cg
->cg_boff
);
214 if ((unsigned int)fs
->fs_contigsumsize
> 0) {
215 ulptr
= ((int32_t *)((u_int8_t
*)(cg
) + (cg
)->cg_clustersumoff
));
216 size
= (fs
->fs_contigsumsize
+ 1);
217 byte_swap_ints(ulptr
, size
); /*cg_clustersumoff*/
220 byte_swap_int(cg
->cg_clustersumoff
);
224 /* This value MUST correspond to the value set in the ffs_mounts */
226 #define RESYMLNKLEN 60
229 byte_swap_inode_in(struct dinode
*di
, struct inode
*ip
)
233 ip
->i_mode
= NXSwapShort(di
->di_mode
);
234 ip
->i_nlink
= NXSwapShort(di
->di_nlink
);
235 ip
->i_oldids
[0] = NXSwapShort(di
->di_u
.oldids
[0]);
236 ip
->i_oldids
[1] = NXSwapShort(di
->di_u
.oldids
[1]);
237 ip
->i_size
= NXSwapLongLong(di
->di_size
);
238 ip
->i_atime
= NXSwapLong(di
->di_atime
);
239 ip
->i_atimensec
= NXSwapLong(di
->di_atimensec
);
240 ip
->i_mtime
= NXSwapLong(di
->di_mtime
);
241 ip
->i_mtimensec
= NXSwapLong(di
->di_mtimensec
);
242 ip
->i_ctime
= NXSwapLong(di
->di_ctime
);
243 ip
->i_ctimensec
= NXSwapLong(di
->di_ctimensec
);
244 if (((ip
->i_mode
& IFMT
) == IFLNK
) && (ip
->i_size
<= RESYMLNKLEN
)) {
245 bcopy(&di
->di_shortlink
, &ip
->i_shortlink
, RESYMLNKLEN
);
247 for (i
=0; i
< NDADDR
; i
++) /* direct blocks */
248 ip
->i_db
[i
] = NXSwapLong(di
->di_db
[i
]);
249 for (i
=0; i
< NIADDR
; i
++) /* indirect blocks */
250 ip
->i_ib
[i
] = NXSwapLong(di
->di_ib
[i
]);
252 ip
->i_flags
= NXSwapLong(di
->di_flags
);
253 ip
->i_blocks
= NXSwapLong(di
->di_blocks
);
254 ip
->i_gen
= NXSwapLong(di
->di_gen
);
255 ip
->i_uid
= NXSwapLong(di
->di_uid
);
256 ip
->i_gid
= NXSwapLong(di
->di_gid
);
257 ip
->i_spare
[0] = NXSwapLong(di
->di_spare
[0]);
258 ip
->i_spare
[1] = NXSwapLong(di
->di_spare
[1]);
262 byte_swap_inode_out(struct inode
*ip
, struct dinode
*di
)
267 mode
= (ip
->i_mode
& IFMT
);
268 inosize
= ip
->i_size
;
270 di
->di_mode
= NXSwapShort(ip
->i_mode
);
271 di
->di_nlink
= NXSwapShort(ip
->i_nlink
);
272 di
->di_u
.oldids
[0] = NXSwapShort(ip
->i_oldids
[0]);
273 di
->di_u
.oldids
[1] = NXSwapShort(ip
->i_oldids
[1]);
274 di
->di_size
= NXSwapLongLong(ip
->i_size
);
275 di
->di_atime
= NXSwapLong(ip
->i_atime
);
276 di
->di_atimensec
= NXSwapLong(ip
->i_atimensec
);
277 di
->di_mtime
= NXSwapLong(ip
->i_mtime
);
278 di
->di_mtimensec
= NXSwapLong(ip
->i_mtimensec
);
279 di
->di_ctime
= NXSwapLong(ip
->i_ctime
);
280 di
->di_ctimensec
= NXSwapLong(ip
->i_ctimensec
);
281 if ((mode
== IFLNK
) && (inosize
<= RESYMLNKLEN
)) {
282 bcopy( &ip
->i_shortlink
, &di
->di_shortlink
, RESYMLNKLEN
);
284 for (i
=0; i
< NDADDR
; i
++) /* direct blocks */
285 di
->di_db
[i
] = NXSwapLong(ip
->i_db
[i
]);
286 for (i
=0; i
< NIADDR
; i
++) /* indirect blocks */
287 di
->di_ib
[i
] = NXSwapLong(ip
->i_ib
[i
]);
289 di
->di_flags
= NXSwapLong(ip
->i_flags
);
290 di
->di_blocks
= NXSwapLong(ip
->i_blocks
);
291 di
->di_gen
= NXSwapLong(ip
->i_gen
);
292 di
->di_uid
= NXSwapLong(ip
->i_uid
);
293 di
->di_gid
= NXSwapLong(ip
->i_gid
);
294 di
->di_spare
[0] = NXSwapLong(ip
->i_spare
[0]);
295 di
->di_spare
[1] = NXSwapLong(ip
->i_spare
[1]);
299 byte_swap_direct(struct direct
*dirp
)
301 byte_swap_int(dirp
->d_ino
);
302 byte_swap_short(dirp
->d_reclen
);
306 byte_swap_dir_block_in(char *addr
, int count
)
308 struct direct
*ep
= (struct direct
*) addr
;
309 int entryoffsetinblk
= 0;
311 while (entryoffsetinblk
< count
) {
312 ep
= (struct direct
*) (entryoffsetinblk
+ addr
);
313 byte_swap_int(ep
->d_ino
);
314 byte_swap_short(ep
->d_reclen
);
315 entryoffsetinblk
+= ep
->d_reclen
;
316 if (ep
->d_reclen
< 12) /* handle garbage in dirs */
322 byte_swap_dir_out(char *addr
, int count
)
324 struct direct
*ep
= (struct direct
*) addr
;
325 int entryoffsetinblk
= 0;
328 while (entryoffsetinblk
< count
) {
329 ep
= (struct direct
*) (entryoffsetinblk
+ addr
);
330 reclen
= ep
->d_reclen
;
331 entryoffsetinblk
+= reclen
;
332 byte_swap_int(ep
->d_ino
);
333 byte_swap_short(ep
->d_reclen
);
340 byte_swap_dir_block_out(struct buf
*bp
)
342 struct direct
*ep
= (struct direct
*) bp
->b_data
;
343 int reclen
, entryoffsetinblk
= 0;
345 while (entryoffsetinblk
< bp
->b_bcount
) {
346 ep
= (struct direct
*) (entryoffsetinblk
+ bp
->b_data
);
347 reclen
= ep
->d_reclen
;
348 entryoffsetinblk
+= reclen
;
349 byte_swap_int(ep
->d_ino
);
350 byte_swap_short(ep
->d_reclen
);
357 byte_swap_dirtemplate_in(struct dirtemplate
*dirt
)
359 byte_swap_int(dirt
->dot_ino
);
360 byte_swap_short(dirt
->dot_reclen
);
361 byte_swap_int(dirt
->dotdot_ino
);
362 byte_swap_short(dirt
->dotdot_reclen
);
366 byte_swap_minidir_in(struct direct
*dirp
)
368 byte_swap_int(dirp
->d_ino
);
369 byte_swap_short(dirp
->d_reclen
);
373 /* This is for the compatability (old) cylinder group block */
375 byte_swap_ocylgroup(struct cg
*cg
)
377 byte_swap_int(cg
->cg_time
);
378 byte_swap_int(cg
->cg_cgx
);
379 byte_swap_short(cg
->cg_ncyl
);
380 byte_swap_short(cg
->cg_niblk
);
381 byte_swap_int(cg
->cg_ndblk
);
382 byte_swap_csum(&cg
->cg_cs
);
383 byte_swap_int(cg
->cg_rotor
);
384 byte_swap_int(cg
->cg_frotor
);
385 byte_swap_int(cg
->cg_irotor
);
386 byte_swap_ints(&cg
->cg_frsum
, 8);
387 byte_swap_ints(&cg
->cg_btot
, 32);
388 byte_swap_shorts((short *)&cg
->cg_b
, 32 * 8);
389 byte_swap_int(cg
->cg_magic
);