]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_byte_order.c
xnu-517.7.7.tar.gz
[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_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,
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);
88
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 */
94 }
95 }
96
97 void
98 byte_swap_sbout(struct fs *sb)
99 {
100 u_int16_t *usptr;
101 unsigned long size;
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 */
107 }
108
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);
121 }
122
123 void
124 byte_swap_csum(struct csum *cs)
125 {
126 byte_swap_ints((int *) cs, sizeof(struct csum) / sizeof(int32_t));
127 }
128
129 /* This is for the new 4.4 cylinder group block */
130 void
131 byte_swap_cgin(struct cg *cg, struct fs * fs)
132 {
133 int32_t * ulptr;
134 int16_t * usptr;
135 int size;
136
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);
155
156 byte_swap_int(cg->cg_btotoff);
157 ulptr = ((int32_t *)((u_int8_t *)(cg) + (cg)->cg_btotoff));
158 size = fs->fs_cpg;
159 byte_swap_ints(ulptr, size); /*cg_btotoff*/
160
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*/
165
166 byte_swap_int(cg->cg_clustersumoff);
167
168 if ((unsigned int)fs->fs_contigsumsize > 0) {
169
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*/
173 }
174
175 }
176
177 /* This is for the new 4.4 cylinder group block */
178 void
179 byte_swap_cgout(struct cg *cg, struct fs * fs)
180 {
181 int32_t * ulptr;
182 int16_t * usptr;
183 int size;
184
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);
201
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));
205 size = fs->fs_cpg;
206 byte_swap_ints(ulptr, size); /*cg_btotoff*/
207 byte_swap_int(cg->cg_btotoff);
208
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);
213
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*/
218
219 }
220 byte_swap_int(cg->cg_clustersumoff);
221
222 }
223
224 /* This value MUST correspond to the value set in the ffs_mounts */
225
226 #define RESYMLNKLEN 60
227
228 void
229 byte_swap_inode_in(struct dinode *di, struct inode *ip)
230 {
231 int i;
232
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);
246 } else {
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]);
251 }
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]);
259 }
260
261 void
262 byte_swap_inode_out(struct inode *ip, struct dinode *di)
263 {
264 int i;
265 int mode, inosize;
266
267 mode = (ip->i_mode & IFMT);
268 inosize = ip->i_size;
269
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);
283 } else {
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]);
288 }
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]);
296 }
297
298 void
299 byte_swap_direct(struct direct *dirp)
300 {
301 byte_swap_int(dirp->d_ino);
302 byte_swap_short(dirp->d_reclen);
303 }
304
305 void
306 byte_swap_dir_block_in(char *addr, int count)
307 {
308 struct direct *ep = (struct direct *) addr;
309 int entryoffsetinblk = 0;
310
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 */
317 break;
318 }
319 }
320
321 void
322 byte_swap_dir_out(char *addr, int count)
323 {
324 struct direct *ep = (struct direct *) addr;
325 int entryoffsetinblk = 0;
326 int reclen;
327
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);
334 if (reclen < 12)
335 break;
336 }
337 }
338
339 void
340 byte_swap_dir_block_out(struct buf *bp)
341 {
342 struct direct *ep = (struct direct *) bp->b_data;
343 int reclen, entryoffsetinblk = 0;
344
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);
351 if (reclen < 12)
352 break;
353 }
354 }
355
356 void
357 byte_swap_dirtemplate_in(struct dirtemplate *dirt)
358 {
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);
363 }
364
365 void
366 byte_swap_minidir_in(struct direct *dirp)
367 {
368 byte_swap_int(dirp->d_ino);
369 byte_swap_short(dirp->d_reclen);
370 }
371
372 #if 0
373 /* This is for the compatability (old) cylinder group block */
374 void
375 byte_swap_ocylgroup(struct cg *cg)
376 {
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);
390 }
391 #endif /* 0 */