]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_byte_order.c
3dd15cdef55b91124701d6900773af135577e6fc
[apple/xnu.git] / bsd / ufs / ufs / ufs_byte_order.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright 1998 Apple Computer, Inc.
31 *
32 * UFS byte swapping routines to make a big endian file system useful on a
33 * little endian machine.
34 *
35 * HISTORY
36 *
37 * 16 Feb 1998 A. Ramesh at Apple
38 * MacOSX version created.
39 */
40
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 <libkern/OSByteOrder.h>
46
47 #define byte_swap_longlong(thing) ((thing) = OSSwapInt64(thing))
48 #define byte_swap_int(thing) ((thing) = OSSwapInt32(thing))
49 #define byte_swap_short(thing) ((thing) = OSSwapInt16(thing))
50
51 void
52 byte_swap_longlongs(unsigned long long *array, int count)
53 {
54 register unsigned long long i;
55
56 for (i = 0; i < count; i++)
57 byte_swap_longlong(array[i]);
58 }
59
60 void
61 byte_swap_ints(int *array, int count)
62 {
63 register int i;
64
65 for (i = 0; i < count; i++)
66 byte_swap_int(array[i]);
67 }
68
69 void
70 byte_swap_shorts(short *array, int count)
71 {
72 register int i;
73
74 for (i = 0; i < count; i++)
75 byte_swap_short(array[i]);
76 }
77
78 void
79 byte_swap_sbin(struct fs *sb)
80 {
81 u_int16_t *usptr;
82 unsigned long size;
83
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);
96
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 */
102 }
103 }
104
105 void
106 byte_swap_sbout(struct fs *sb)
107 {
108 u_int16_t *usptr;
109 unsigned long size;
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 */
115 }
116
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);
129 }
130
131 void
132 byte_swap_csum(struct csum *cs)
133 {
134 byte_swap_ints((int *) cs, sizeof(struct csum) / sizeof(int32_t));
135 }
136
137 /* This is for the new 4.4 cylinder group block */
138 void
139 byte_swap_cgin(struct cg *cg, struct fs * fs)
140 {
141 int32_t * ulptr;
142 int16_t * usptr;
143 int size;
144
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);
163
164 byte_swap_int(cg->cg_btotoff);
165 ulptr = ((int32_t *)((u_int8_t *)(cg) + (cg)->cg_btotoff));
166 size = fs->fs_cpg;
167 byte_swap_ints(ulptr, size); /*cg_btotoff*/
168
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*/
173
174 byte_swap_int(cg->cg_clustersumoff);
175
176 if ((unsigned int)fs->fs_contigsumsize > 0) {
177
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*/
181 }
182
183 }
184
185 /* This is for the new 4.4 cylinder group block */
186 void
187 byte_swap_cgout(struct cg *cg, struct fs * fs)
188 {
189 int32_t * ulptr;
190 int16_t * usptr;
191 int size;
192
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);
209
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));
213 size = fs->fs_cpg;
214 byte_swap_ints(ulptr, size); /*cg_btotoff*/
215 byte_swap_int(cg->cg_btotoff);
216
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);
221
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*/
226
227 }
228 byte_swap_int(cg->cg_clustersumoff);
229
230 }
231
232 /* This value MUST correspond to the value set in the ffs_mounts */
233
234 #define RESYMLNKLEN 60
235
236 void
237 byte_swap_inode_in(struct dinode *di, struct inode *ip)
238 {
239 int i;
240
241 ip->i_mode = OSSwapInt16(di->di_mode);
242 ip->i_nlink = OSSwapInt16(di->di_nlink);
243 ip->i_oldids[0] = OSSwapInt16(di->di_u.oldids[0]);
244 ip->i_oldids[1] = OSSwapInt16(di->di_u.oldids[1]);
245 ip->i_size = OSSwapInt64(di->di_size);
246 ip->i_atime = OSSwapInt32(di->di_atime);
247 ip->i_atimensec = OSSwapInt32(di->di_atimensec);
248 ip->i_mtime = OSSwapInt32(di->di_mtime);
249 ip->i_mtimensec = OSSwapInt32(di->di_mtimensec);
250 ip->i_ctime = OSSwapInt32(di->di_ctime);
251 ip->i_ctimensec = OSSwapInt32(di->di_ctimensec);
252 if (((ip->i_mode & IFMT) == IFLNK ) && (ip->i_size <= RESYMLNKLEN)) {
253 bcopy(&di->di_shortlink, &ip->i_shortlink, RESYMLNKLEN);
254 } else {
255 for (i=0; i < NDADDR; i++) /* direct blocks */
256 ip->i_db[i] = OSSwapInt32(di->di_db[i]);
257 for (i=0; i < NIADDR; i++) /* indirect blocks */
258 ip->i_ib[i] = OSSwapInt32(di->di_ib[i]);
259 }
260 ip->i_flags = OSSwapInt32(di->di_flags);
261 ip->i_blocks = OSSwapInt32(di->di_blocks);
262 ip->i_gen = OSSwapInt32(di->di_gen);
263 ip->i_uid = OSSwapInt32(di->di_uid);
264 ip->i_gid = OSSwapInt32(di->di_gid);
265 ip->i_spare[0] = OSSwapInt32(di->di_spare[0]);
266 ip->i_spare[1] = OSSwapInt32(di->di_spare[1]);
267 }
268
269 void
270 byte_swap_inode_out(struct inode *ip, struct dinode *di)
271 {
272 int i;
273 int mode, inosize;
274
275 mode = (ip->i_mode & IFMT);
276 inosize = ip->i_size;
277
278 di->di_mode = OSSwapInt16(ip->i_mode);
279 di->di_nlink = OSSwapInt16(ip->i_nlink);
280 di->di_u.oldids[0] = OSSwapInt16(ip->i_oldids[0]);
281 di->di_u.oldids[1] = OSSwapInt16(ip->i_oldids[1]);
282 di->di_size = OSSwapInt64(ip->i_size);
283 di->di_atime = OSSwapInt32(ip->i_atime);
284 di->di_atimensec = OSSwapInt32(ip->i_atimensec);
285 di->di_mtime = OSSwapInt32(ip->i_mtime);
286 di->di_mtimensec = OSSwapInt32(ip->i_mtimensec);
287 di->di_ctime = OSSwapInt32(ip->i_ctime);
288 di->di_ctimensec = OSSwapInt32(ip->i_ctimensec);
289 if ((mode == IFLNK) && (inosize <= RESYMLNKLEN)) {
290 bcopy( &ip->i_shortlink, &di->di_shortlink, RESYMLNKLEN);
291 } else {
292 for (i=0; i < NDADDR; i++) /* direct blocks */
293 di->di_db[i] = OSSwapInt32(ip->i_db[i]);
294 for (i=0; i < NIADDR; i++) /* indirect blocks */
295 di->di_ib[i] = OSSwapInt32(ip->i_ib[i]);
296 }
297 di->di_flags = OSSwapInt32(ip->i_flags);
298 di->di_blocks = OSSwapInt32(ip->i_blocks);
299 di->di_gen = OSSwapInt32(ip->i_gen);
300 di->di_uid = OSSwapInt32(ip->i_uid);
301 di->di_gid = OSSwapInt32(ip->i_gid);
302 di->di_spare[0] = OSSwapInt32(ip->i_spare[0]);
303 di->di_spare[1] = OSSwapInt32(ip->i_spare[1]);
304 }
305
306 void
307 byte_swap_direct(struct direct *dirp)
308 {
309 byte_swap_int(dirp->d_ino);
310 byte_swap_short(dirp->d_reclen);
311 }
312
313 void
314 byte_swap_dir_block_in(char *addr, int count)
315 {
316 struct direct *ep = (struct direct *) addr;
317 int entryoffsetinblk = 0;
318
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 */
325 break;
326 }
327 }
328
329 void
330 byte_swap_dir_out(char *addr, int count)
331 {
332 struct direct *ep = (struct direct *) addr;
333 int entryoffsetinblk = 0;
334 int reclen;
335
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);
342 if (reclen < 12)
343 break;
344 }
345 }
346
347 void
348 byte_swap_dir_block_out(struct buf *bp)
349 {
350 struct direct *ep = (struct direct *) buf_dataptr(bp);
351 int reclen, entryoffsetinblk = 0;
352
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);
359 if (reclen < 12)
360 break;
361 }
362 }
363
364 void
365 byte_swap_dirtemplate_in(struct dirtemplate *dirt)
366 {
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);
371 }
372
373 void
374 byte_swap_minidir_in(struct direct *dirp)
375 {
376 byte_swap_int(dirp->d_ino);
377 byte_swap_short(dirp->d_reclen);
378 }
379
380 #if 0
381 /* This is for the compatability (old) cylinder group block */
382 void
383 byte_swap_ocylgroup(struct cg *cg)
384 {
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);
398 }
399 #endif /* 0 */