]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_byte_order.c
2 * Copyright (c) 2000 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
, 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);
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 */
94 byte_swap_sbout(struct fs
*sb
)
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 */
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);
116 byte_swap_csum(struct csum
*cs
)
118 byte_swap_ints((int *) cs
, sizeof(struct csum
) / sizeof(int32_t));
121 /* This is for the new 4.4 cylinder group block */
123 byte_swap_cgin(struct cg
*cg
, struct fs
* fs
)
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);
148 byte_swap_int(cg
->cg_btotoff
);
149 ulptr
= ((int32_t *)((u_int8_t
*)(cg
) + (cg
)->cg_btotoff
));
151 byte_swap_ints(ulptr
, size
); /*cg_btotoff*/
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*/
158 byte_swap_int(cg
->cg_clustersumoff
);
160 if ((unsigned int)fs
->fs_contigsumsize
> 0) {
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*/
169 /* This is for the new 4.4 cylinder group block */
171 byte_swap_cgout(struct cg
*cg
, struct fs
* fs
)
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);
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
));
198 byte_swap_ints(ulptr
, size
); /*cg_btotoff*/
199 byte_swap_int(cg
->cg_btotoff
);
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
);
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*/
212 byte_swap_int(cg
->cg_clustersumoff
);
216 /* This value MUST correspond to the value set in the ffs_mounts */
218 #define RESYMLNKLEN 60
221 byte_swap_inode_in(struct dinode
*di
, struct inode
*ip
)
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
);
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
]);
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]);
254 byte_swap_inode_out(struct inode
*ip
, struct dinode
*di
)
259 mode
= (ip
->i_mode
& IFMT
);
260 inosize
= ip
->i_size
;
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
);
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
]);
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]);
291 byte_swap_direct(struct direct
*dirp
)
293 byte_swap_int(dirp
->d_ino
);
294 byte_swap_short(dirp
->d_reclen
);
298 byte_swap_dir_block_in(char *addr
, int count
)
300 struct direct
*ep
= (struct direct
*) addr
;
301 int entryoffsetinblk
= 0;
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 */
314 byte_swap_dir_out(char *addr
, int count
)
316 struct direct
*ep
= (struct direct
*) addr
;
317 int entryoffsetinblk
= 0;
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
);
332 byte_swap_dir_block_out(struct buf
*bp
)
334 struct direct
*ep
= (struct direct
*) bp
->b_data
;
335 int reclen
, entryoffsetinblk
= 0;
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
);
349 byte_swap_dirtemplate_in(struct dirtemplate
*dirt
)
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
);
358 byte_swap_minidir_in(struct direct
*dirp
)
360 byte_swap_int(dirp
->d_ino
);
361 byte_swap_short(dirp
->d_reclen
);
365 /* This is for the compatability (old) cylinder group block */
367 byte_swap_ocylgroup(struct cg
*cg
)
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
);