Home | History | Annotate | Line # | Download | only in fsck_ffs
utilities.c revision 1.27
      1 /*	$NetBSD: utilities.c,v 1.27 2000/10/10 20:24:51 is Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1986, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
     40 #else
     41 __RCSID("$NetBSD: utilities.c,v 1.27 2000/10/10 20:24:51 is Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/time.h>
     47 
     48 #include <ufs/ufs/dinode.h>
     49 #include <ufs/ufs/dir.h>
     50 #include <ufs/ffs/fs.h>
     51 #include <ufs/ffs/ffs_extern.h>
     52 #include <ufs/ufs/ufs_bswap.h>
     53 
     54 #include <ctype.h>
     55 #include <err.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 
     61 #include "fsutil.h"
     62 #include "fsck.h"
     63 #include "extern.h"
     64 
     65 long	diskreads, totalreads;	/* Disk cache statistics */
     66 
     67 static void rwerror __P((char *, ufs_daddr_t));
     68 
     69 int
     70 ftypeok(dp)
     71 	struct dinode *dp;
     72 {
     73 	switch (iswap16(dp->di_mode) & IFMT) {
     74 
     75 	case IFDIR:
     76 	case IFREG:
     77 	case IFBLK:
     78 	case IFCHR:
     79 	case IFLNK:
     80 	case IFSOCK:
     81 	case IFIFO:
     82 		return (1);
     83 
     84 	default:
     85 		if (debug)
     86 			printf("bad file type 0%o\n", iswap16(dp->di_mode));
     87 		return (0);
     88 	}
     89 }
     90 
     91 int
     92 reply(question)
     93 	char *question;
     94 {
     95 	int persevere;
     96 	char c;
     97 
     98 	if (preen)
     99 		pfatal("INTERNAL ERROR: GOT TO reply()");
    100 	persevere = !strcmp(question, "CONTINUE");
    101 	printf("\n");
    102 	if (!persevere && (nflag || fswritefd < 0)) {
    103 		printf("%s? no\n\n", question);
    104 		resolved = 0;
    105 		return (0);
    106 	}
    107 	if (yflag || (persevere && nflag)) {
    108 		printf("%s? yes\n\n", question);
    109 		return (1);
    110 	}
    111 	do	{
    112 		printf("%s? [yn] ", question);
    113 		(void) fflush(stdout);
    114 		c = getc(stdin);
    115 		while (c != '\n' && getc(stdin) != '\n') {
    116 			if (feof(stdin)) {
    117 				resolved = 0;
    118 				return (0);
    119 			}
    120 		}
    121 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    122 	printf("\n");
    123 	if (c == 'y' || c == 'Y')
    124 		return (1);
    125 	resolved = 0;
    126 	return (0);
    127 }
    128 
    129 /*
    130  * Malloc buffers and set up cache.
    131  */
    132 void
    133 bufinit()
    134 {
    135 	struct bufarea *bp;
    136 	long bufcnt, i;
    137 	char *bufp;
    138 
    139 	pbp = pdirbp = (struct bufarea *)0;
    140 	bufp = malloc((unsigned int)sblock->fs_bsize);
    141 	if (bufp == 0)
    142 		errx(EEXIT, "cannot allocate buffer pool");
    143 	cgblk.b_un.b_buf = bufp;
    144 	initbarea(&cgblk);
    145 	bufhead.b_next = bufhead.b_prev = &bufhead;
    146 	bufcnt = MAXBUFSPACE / sblock->fs_bsize;
    147 	if (bufcnt < MINBUFS)
    148 		bufcnt = MINBUFS;
    149 	for (i = 0; i < bufcnt; i++) {
    150 		bp = (struct bufarea *)malloc(sizeof(struct bufarea));
    151 		bufp = malloc((unsigned int)sblock->fs_bsize);
    152 		if (bp == NULL || bufp == NULL) {
    153 			if (i >= MINBUFS)
    154 				break;
    155 			errx(EEXIT, "cannot allocate buffer pool");
    156 		}
    157 		bp->b_un.b_buf = bufp;
    158 		bp->b_prev = &bufhead;
    159 		bp->b_next = bufhead.b_next;
    160 		bufhead.b_next->b_prev = bp;
    161 		bufhead.b_next = bp;
    162 		initbarea(bp);
    163 	}
    164 	bufhead.b_size = i;	/* save number of buffers */
    165 }
    166 
    167 /*
    168  * Manage a cache of directory blocks.
    169  */
    170 struct bufarea *
    171 getdatablk(blkno, size)
    172 	ufs_daddr_t blkno;
    173 	long size;
    174 {
    175 	struct bufarea *bp;
    176 
    177 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
    178 		if (bp->b_bno == fsbtodb(sblock, blkno))
    179 			goto foundit;
    180 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
    181 		if ((bp->b_flags & B_INUSE) == 0)
    182 			break;
    183 	if (bp == &bufhead)
    184 		errx(EEXIT, "deadlocked buffer pool");
    185 	getblk(bp, blkno, size);
    186 	/* fall through */
    187 foundit:
    188 	totalreads++;
    189 	bp->b_prev->b_next = bp->b_next;
    190 	bp->b_next->b_prev = bp->b_prev;
    191 	bp->b_prev = &bufhead;
    192 	bp->b_next = bufhead.b_next;
    193 	bufhead.b_next->b_prev = bp;
    194 	bufhead.b_next = bp;
    195 	bp->b_flags |= B_INUSE;
    196 	return (bp);
    197 }
    198 
    199 void
    200 getblk(bp, blk, size)
    201 	struct bufarea *bp;
    202 	ufs_daddr_t blk;
    203 	long size;
    204 {
    205 	ufs_daddr_t dblk;
    206 
    207 	dblk = fsbtodb(sblock, blk);
    208 	if (bp->b_bno != dblk) {
    209 		flush(fswritefd, bp);
    210 		diskreads++;
    211 		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
    212 		bp->b_bno = dblk;
    213 		bp->b_size = size;
    214 	}
    215 }
    216 
    217 void
    218 flush(fd, bp)
    219 	int fd;
    220 	struct bufarea *bp;
    221 {
    222 	int i, j;
    223 
    224 	if (!bp->b_dirty)
    225 		return;
    226 	if (bp->b_errs != 0)
    227 		pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
    228 		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
    229 		    bp->b_bno);
    230 	bp->b_dirty = 0;
    231 	bp->b_errs = 0;
    232 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
    233 	if (bp != &sblk)
    234 		return;
    235 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
    236 		int size = sblock->fs_cssize - i < sblock->fs_bsize ?
    237 			sblock->fs_cssize - i : sblock->fs_bsize;
    238 		/*
    239 		 * The following routines assumes that struct csum is made of
    240 		 * u_int32_t's
    241 		 */
    242 		if (needswap) {
    243 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    244 			int k;
    245 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    246 				cd[k] = bswap32(cd[k]);
    247 		}
    248 		bwrite(fswritefd, (char *)sblock->fs_csp[j],
    249 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    250 		    sblock->fs_cssize - i < sblock->fs_bsize ?
    251 		    sblock->fs_cssize - i : sblock->fs_bsize);
    252 		if (needswap) {
    253 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    254 			int k;
    255 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    256 				cd[k] = bswap32(cd[k]);
    257 		}
    258 	}
    259 }
    260 
    261 static void
    262 rwerror(mesg, blk)
    263 	char *mesg;
    264 	ufs_daddr_t blk;
    265 {
    266 
    267 	if (preen == 0)
    268 		printf("\n");
    269 	pfatal("CANNOT %s: BLK %d", mesg, blk);
    270 	if (reply("CONTINUE") == 0)
    271 		exit(EEXIT);
    272 }
    273 
    274 void
    275 ckfini()
    276 {
    277 	struct bufarea *bp, *nbp;
    278 	int ofsmodified, cnt = 0;
    279 
    280 	if (fswritefd < 0) {
    281 		(void)close(fsreadfd);
    282 		return;
    283 	}
    284 	flush(fswritefd, &sblk);
    285 	if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
    286 	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
    287 		sblk.b_bno = SBOFF / dev_bsize;
    288 		sbdirty();
    289 		flush(fswritefd, &sblk);
    290 	}
    291 	flush(fswritefd, &cgblk);
    292 	free(cgblk.b_un.b_buf);
    293 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
    294 		cnt++;
    295 		flush(fswritefd, bp);
    296 		nbp = bp->b_prev;
    297 		free(bp->b_un.b_buf);
    298 		free((char *)bp);
    299 	}
    300 	if (bufhead.b_size != cnt)
    301 		errx(EEXIT, "Panic: lost %d buffers", bufhead.b_size - cnt);
    302 	pbp = pdirbp = (struct bufarea *)0;
    303 	if (markclean && (sblock->fs_clean & FS_ISCLEAN) == 0) {
    304 		/*
    305 		 * Mark the file system as clean, and sync the superblock.
    306 		 */
    307 		if (preen)
    308 			pwarn("MARKING FILE SYSTEM CLEAN\n");
    309 		else if (!reply("MARK FILE SYSTEM CLEAN"))
    310 			markclean = 0;
    311 		if (markclean) {
    312 			sblock->fs_clean = FS_ISCLEAN;
    313 			sbdirty();
    314 			ofsmodified = fsmodified;
    315 			flush(fswritefd, &sblk);
    316 #if LITE2BORKEN
    317 			fsmodified = ofsmodified;
    318 #endif
    319 			if (!preen)
    320 				printf(
    321 				    "\n***** FILE SYSTEM MARKED CLEAN *****\n");
    322 		}
    323 	}
    324 	if (debug)
    325 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
    326 		    totalreads, (int)(diskreads * 100 / totalreads));
    327 	(void)close(fsreadfd);
    328 	(void)close(fswritefd);
    329 }
    330 
    331 int
    332 bread(fd, buf, blk, size)
    333 	int fd;
    334 	char *buf;
    335 	ufs_daddr_t blk;
    336 	long size;
    337 {
    338 	char *cp;
    339 	int i, errs;
    340 	off_t offset;
    341 
    342 	offset = blk;
    343 	offset *= dev_bsize;
    344 	if (lseek(fd, offset, 0) < 0)
    345 		rwerror("SEEK", blk);
    346 	else if (read(fd, buf, (int)size) == size)
    347 		return (0);
    348 	rwerror("READ", blk);
    349 	if (lseek(fd, offset, 0) < 0)
    350 		rwerror("SEEK", blk);
    351 	errs = 0;
    352 	memset(buf, 0, (size_t)size);
    353 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
    354 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
    355 		if (read(fd, cp, (int)secsize) != secsize) {
    356 			(void)lseek(fd, offset + i + secsize, 0);
    357 			if (secsize != dev_bsize && dev_bsize != 1)
    358 				printf(" %ld (%ld),",
    359 				    (blk * dev_bsize + i) / secsize,
    360 				    blk + i / dev_bsize);
    361 			else
    362 				printf(" %ld,", blk + i / dev_bsize);
    363 			errs++;
    364 		}
    365 	}
    366 	printf("\n");
    367 	return (errs);
    368 }
    369 
    370 void
    371 bwrite(fd, buf, blk, size)
    372 	int fd;
    373 	char *buf;
    374 	ufs_daddr_t blk;
    375 	long size;
    376 {
    377 	int i;
    378 	char *cp;
    379 	off_t offset;
    380 
    381 	if (fd < 0)
    382 		return;
    383 	offset = blk;
    384 	offset *= dev_bsize;
    385 	if (lseek(fd, offset, 0) < 0)
    386 		rwerror("SEEK", blk);
    387 	else if (write(fd, buf, (int)size) == size) {
    388 		fsmodified = 1;
    389 		return;
    390 	}
    391 	rwerror("WRITE", blk);
    392 	if (lseek(fd, offset, 0) < 0)
    393 		rwerror("SEEK", blk);
    394 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
    395 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
    396 		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
    397 			(void)lseek(fd, offset + i + dev_bsize, 0);
    398 			printf(" %ld,", blk + i / dev_bsize);
    399 		}
    400 	printf("\n");
    401 	return;
    402 }
    403 
    404 /*
    405  * allocate a data block with the specified number of fragments
    406  */
    407 ufs_daddr_t
    408 allocblk(frags)
    409 	long frags;
    410 {
    411 	int i, j, k, cg, baseblk;
    412 	struct cg *cgp = cgrp;
    413 
    414 	if (frags <= 0 || frags > sblock->fs_frag)
    415 		return (0);
    416 	for (i = 0; i < maxfsblock - sblock->fs_frag; i += sblock->fs_frag) {
    417 		for (j = 0; j <= sblock->fs_frag - frags; j++) {
    418 			if (testbmap(i + j))
    419 				continue;
    420 			for (k = 1; k < frags; k++)
    421 				if (testbmap(i + j + k))
    422 					break;
    423 			if (k < frags) {
    424 				j += k;
    425 				continue;
    426 			}
    427 			cg = dtog(sblock, i + j);
    428 			getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
    429 			memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
    430 			if ((doswap && !needswap) || (!doswap && needswap))
    431 				swap_cg(cgblk.b_un.b_cg, cgp);
    432 			if (!cg_chkmagic(cgp, 0))
    433 				pfatal("CG %d: ALLOCBLK: BAD MAGIC NUMBER\n",
    434 				    cg);
    435 			baseblk = dtogd(sblock, i + j);
    436 			for (k = 0; k < frags; k++) {
    437 				setbmap(i + j + k);
    438 				clrbit(cg_blksfree(cgp, 0), baseblk + k);
    439 			}
    440 			n_blks += frags;
    441 			if (frags == sblock->fs_frag)
    442 				cgp->cg_cs.cs_nbfree--;
    443 			else
    444 				cgp->cg_cs.cs_nffree -= frags;
    445 			cgdirty();
    446 			return (i + j);
    447 		}
    448 	}
    449 	return (0);
    450 }
    451 
    452 /*
    453  * Free a previously allocated block
    454  */
    455 void
    456 freeblk(blkno, frags)
    457 	ufs_daddr_t blkno;
    458 	long frags;
    459 {
    460 	struct inodesc idesc;
    461 
    462 	idesc.id_blkno = blkno;
    463 	idesc.id_numfrags = frags;
    464 	(void)pass4check(&idesc);
    465 }
    466 
    467 /*
    468  * Find a pathname
    469  */
    470 void
    471 getpathname(namebuf, curdir, ino)
    472 	char *namebuf;
    473 	ino_t curdir, ino;
    474 {
    475 	int len;
    476 	char *cp;
    477 	struct inodesc idesc;
    478 	static int busy = 0;
    479 
    480 	if (curdir == ino && ino == ROOTINO) {
    481 		(void)strcpy(namebuf, "/");
    482 		return;
    483 	}
    484 	if (busy ||
    485 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
    486 		(void)strcpy(namebuf, "?");
    487 		return;
    488 	}
    489 	busy = 1;
    490 	memset(&idesc, 0, sizeof(struct inodesc));
    491 	idesc.id_type = DATA;
    492 	idesc.id_fix = IGNORE;
    493 	cp = &namebuf[MAXPATHLEN - 1];
    494 	*cp = '\0';
    495 	if (curdir != ino) {
    496 		idesc.id_parent = curdir;
    497 		goto namelookup;
    498 	}
    499 	while (ino != ROOTINO) {
    500 		idesc.id_number = ino;
    501 		idesc.id_func = findino;
    502 		idesc.id_name = "..";
    503 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
    504 			break;
    505 	namelookup:
    506 		idesc.id_number = idesc.id_parent;
    507 		idesc.id_parent = ino;
    508 		idesc.id_func = findname;
    509 		idesc.id_name = namebuf;
    510 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
    511 			break;
    512 		len = strlen(namebuf);
    513 		cp -= len;
    514 		memmove(cp, namebuf, (size_t)len);
    515 		*--cp = '/';
    516 		if (cp < &namebuf[MAXNAMLEN])
    517 			break;
    518 		ino = idesc.id_number;
    519 	}
    520 	busy = 0;
    521 	if (ino != ROOTINO)
    522 		*--cp = '?';
    523 	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
    524 }
    525 
    526 void
    527 catch(sig)
    528 	int sig;
    529 {
    530 	if (!doinglevel2) {
    531 		markclean = 0;
    532 		ckfini();
    533 	}
    534 	exit(12);
    535 }
    536 
    537 /*
    538  * When preening, allow a single quit to signal
    539  * a special exit after filesystem checks complete
    540  * so that reboot sequence may be interrupted.
    541  */
    542 void
    543 catchquit(sig)
    544 	int sig;
    545 {
    546 	extern int returntosingle;
    547 
    548 	printf("returning to single-user after filesystem check\n");
    549 	returntosingle = 1;
    550 	(void)signal(SIGQUIT, SIG_DFL);
    551 }
    552 
    553 /*
    554  * Ignore a single quit signal; wait and flush just in case.
    555  * Used by child processes in preen.
    556  */
    557 void
    558 voidquit(sig)
    559 	int sig;
    560 {
    561 
    562 	sleep(1);
    563 	(void)signal(SIGQUIT, SIG_IGN);
    564 	(void)signal(SIGQUIT, SIG_DFL);
    565 }
    566 
    567 /*
    568  * determine whether an inode should be fixed.
    569  */
    570 int
    571 dofix(idesc, msg)
    572 	struct inodesc *idesc;
    573 	char *msg;
    574 {
    575 
    576 	switch (idesc->id_fix) {
    577 
    578 	case DONTKNOW:
    579 		if (idesc->id_type == DATA)
    580 			direrror(idesc->id_number, msg);
    581 		else
    582 			pwarn("%s", msg);
    583 		if (preen) {
    584 			printf(" (SALVAGED)\n");
    585 			idesc->id_fix = FIX;
    586 			return (ALTERED);
    587 		}
    588 		if (reply("SALVAGE") == 0) {
    589 			idesc->id_fix = NOFIX;
    590 			return (0);
    591 		}
    592 		idesc->id_fix = FIX;
    593 		return (ALTERED);
    594 
    595 	case FIX:
    596 		return (ALTERED);
    597 
    598 	case NOFIX:
    599 	case IGNORE:
    600 		return (0);
    601 
    602 	default:
    603 		errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
    604 	}
    605 	/* NOTREACHED */
    606 	return (0);
    607 }
    608 
    609 void
    610 copyback_cg(blk)
    611 	struct bufarea *blk;
    612 {
    613 
    614 	memcpy(blk->b_un.b_cg, cgrp, sblock->fs_cgsize);
    615 	if (needswap)
    616 		swap_cg(cgrp, blk->b_un.b_cg);
    617 }
    618 
    619 void
    620 swap_cg(o, n)
    621 	struct cg *o, *n;
    622 {
    623 	int i;
    624 	u_int32_t *n32, *o32;
    625 	u_int16_t *n16, *o16;
    626 
    627 	n->cg_firstfield = bswap32(o->cg_firstfield);
    628 	n->cg_magic = bswap32(o->cg_magic);
    629 	n->cg_time = bswap32(o->cg_time);
    630 	n->cg_cgx = bswap32(o->cg_cgx);
    631 	n->cg_ncyl = bswap16(o->cg_ncyl);
    632 	n->cg_niblk = bswap16(o->cg_niblk);
    633 	n->cg_ndblk = bswap32(o->cg_ndblk);
    634 	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
    635 	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
    636 	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
    637 	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
    638 	n->cg_rotor = bswap32(o->cg_rotor);
    639 	n->cg_frotor = bswap32(o->cg_frotor);
    640 	n->cg_irotor = bswap32(o->cg_irotor);
    641 	n->cg_btotoff = bswap32(o->cg_btotoff);
    642 	n->cg_boff = bswap32(o->cg_boff);
    643 	n->cg_iusedoff = bswap32(o->cg_iusedoff);
    644 	n->cg_freeoff = bswap32(o->cg_freeoff);
    645 	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
    646 	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
    647 	n->cg_clusteroff = bswap32(o->cg_clusteroff);
    648 	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
    649 	for (i=0; i < MAXFRAG; i++)
    650 		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
    651 
    652 	if (sblock->fs_postblformat == FS_42POSTBLFMT) { /* old format */
    653 		struct ocg *on, *oo;
    654 		int j;
    655 		on = (struct ocg *)n;
    656 		oo = (struct ocg *)o;
    657 		for(i = 0; i < 8; i++) {
    658 			on->cg_frsum[i] = bswap32(oo->cg_frsum[i]);
    659 		}
    660 		for(i = 0; i < 32; i++) {
    661 			on->cg_btot[i] = bswap32(oo->cg_btot[i]);
    662 			for (j = 0; j < 8; j++)
    663 				on->cg_b[i][j] = bswap16(oo->cg_b[i][j]);
    664 		}
    665 		memmove(on->cg_iused, oo->cg_iused, 256);
    666 		on->cg_magic = bswap32(oo->cg_magic);
    667 	} else {  /* new format */
    668 		if (n->cg_magic == CG_MAGIC) {
    669 			n32 = (u_int32_t*)((u_int8_t*)n + n->cg_btotoff);
    670 			o32 = (u_int32_t*)((u_int8_t*)o + n->cg_btotoff);
    671 			n16 = (u_int16_t*)((u_int8_t*)n + n->cg_boff);
    672 			o16 = (u_int16_t*)((u_int8_t*)o + n->cg_boff);
    673 		} else {
    674 			n32 = (u_int32_t*)((u_int8_t*)n + o->cg_btotoff);
    675 			o32 = (u_int32_t*)((u_int8_t*)o + o->cg_btotoff);
    676 			n16 = (u_int16_t*)((u_int8_t*)n + o->cg_boff);
    677 			o16 = (u_int16_t*)((u_int8_t*)o + o->cg_boff);
    678 		}
    679 		for (i=0; i < sblock->fs_cpg; i++)
    680 			n32[i] = bswap32(o32[i]);
    681 
    682 		for (i=0; i < sblock->fs_cpg * sblock->fs_nrpos; i++)
    683 			n16[i] = bswap16(o16[i]);
    684 
    685 		if (n->cg_magic == CG_MAGIC) {
    686 			n32 = (u_int32_t*)((u_int8_t*)n + n->cg_clustersumoff);
    687 			o32 = (u_int32_t*)((u_int8_t*)o + n->cg_clustersumoff);
    688 		} else {
    689 			n32 = (u_int32_t*)((u_int8_t*)n + o->cg_clustersumoff);
    690 			o32 = (u_int32_t*)((u_int8_t*)o + o->cg_clustersumoff);
    691 		}
    692 		for (i = 0; i < sblock->fs_contigsumsize + 1; i++)
    693 			n32[i] = bswap32(o32[i]);
    694 	}
    695 }
    696