Home | History | Annotate | Line # | Download | only in fsck_ffs
utilities.c revision 1.29
      1 /*	$NetBSD: utilities.c,v 1.29 2001/01/09 09:25:32 mycroft 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.29 2001/01/09 09:25:32 mycroft 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 			int k;
    244 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    245 
    246 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    247 				cd[k] = bswap32(cd[k]);
    248 		}
    249 		bwrite(fswritefd, (char *)sblock->fs_csp[j],
    250 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    251 		    size);
    252 		if (needswap) {
    253 			int k;
    254 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    255 
    256 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    257 				cd[k] = bswap32(cd[k]);
    258 		}
    259 	}
    260 }
    261 
    262 static void
    263 rwerror(mesg, blk)
    264 	char *mesg;
    265 	ufs_daddr_t blk;
    266 {
    267 
    268 	if (preen == 0)
    269 		printf("\n");
    270 	pfatal("CANNOT %s: BLK %d", mesg, blk);
    271 	if (reply("CONTINUE") == 0)
    272 		exit(EEXIT);
    273 }
    274 
    275 void
    276 ckfini()
    277 {
    278 	struct bufarea *bp, *nbp;
    279 	int ofsmodified, cnt = 0;
    280 
    281 	if (fswritefd < 0) {
    282 		(void)close(fsreadfd);
    283 		return;
    284 	}
    285 	flush(fswritefd, &sblk);
    286 	if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
    287 	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
    288 		sblk.b_bno = SBOFF / dev_bsize;
    289 		sbdirty();
    290 		flush(fswritefd, &sblk);
    291 	}
    292 	flush(fswritefd, &cgblk);
    293 	free(cgblk.b_un.b_buf);
    294 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
    295 		cnt++;
    296 		flush(fswritefd, bp);
    297 		nbp = bp->b_prev;
    298 		free(bp->b_un.b_buf);
    299 		free((char *)bp);
    300 	}
    301 	if (bufhead.b_size != cnt)
    302 		errx(EEXIT, "Panic: lost %d buffers", bufhead.b_size - cnt);
    303 	pbp = pdirbp = (struct bufarea *)0;
    304 	if (markclean && (sblock->fs_clean & FS_ISCLEAN) == 0) {
    305 		/*
    306 		 * Mark the file system as clean, and sync the superblock.
    307 		 */
    308 		if (preen)
    309 			pwarn("MARKING FILE SYSTEM CLEAN\n");
    310 		else if (!reply("MARK FILE SYSTEM CLEAN"))
    311 			markclean = 0;
    312 		if (markclean) {
    313 			sblock->fs_clean = FS_ISCLEAN;
    314 			sbdirty();
    315 			ofsmodified = fsmodified;
    316 			flush(fswritefd, &sblk);
    317 #if LITE2BORKEN
    318 			fsmodified = ofsmodified;
    319 #endif
    320 			if (!preen)
    321 				printf(
    322 				    "\n***** FILE SYSTEM MARKED CLEAN *****\n");
    323 		}
    324 	}
    325 	if (debug)
    326 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
    327 		    totalreads, (int)(diskreads * 100 / totalreads));
    328 	(void)close(fsreadfd);
    329 	(void)close(fswritefd);
    330 }
    331 
    332 int
    333 bread(fd, buf, blk, size)
    334 	int fd;
    335 	char *buf;
    336 	ufs_daddr_t blk;
    337 	long size;
    338 {
    339 	char *cp;
    340 	int i, errs;
    341 	off_t offset;
    342 
    343 	offset = blk;
    344 	offset *= dev_bsize;
    345 	if (lseek(fd, offset, 0) < 0)
    346 		rwerror("SEEK", blk);
    347 	else if (read(fd, buf, (int)size) == size)
    348 		return (0);
    349 	rwerror("READ", blk);
    350 	if (lseek(fd, offset, 0) < 0)
    351 		rwerror("SEEK", blk);
    352 	errs = 0;
    353 	memset(buf, 0, (size_t)size);
    354 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
    355 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
    356 		if (read(fd, cp, (int)secsize) != secsize) {
    357 			(void)lseek(fd, offset + i + secsize, 0);
    358 			if (secsize != dev_bsize && dev_bsize != 1)
    359 				printf(" %ld (%ld),",
    360 				    (blk * dev_bsize + i) / secsize,
    361 				    blk + i / dev_bsize);
    362 			else
    363 				printf(" %ld,", blk + i / dev_bsize);
    364 			errs++;
    365 		}
    366 	}
    367 	printf("\n");
    368 	return (errs);
    369 }
    370 
    371 void
    372 bwrite(fd, buf, blk, size)
    373 	int fd;
    374 	char *buf;
    375 	ufs_daddr_t blk;
    376 	long size;
    377 {
    378 	int i;
    379 	char *cp;
    380 	off_t offset;
    381 
    382 	if (fd < 0)
    383 		return;
    384 	offset = blk;
    385 	offset *= dev_bsize;
    386 	if (lseek(fd, offset, 0) < 0)
    387 		rwerror("SEEK", blk);
    388 	else if (write(fd, buf, (int)size) == size) {
    389 		fsmodified = 1;
    390 		return;
    391 	}
    392 	rwerror("WRITE", blk);
    393 	if (lseek(fd, offset, 0) < 0)
    394 		rwerror("SEEK", blk);
    395 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
    396 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
    397 		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
    398 			(void)lseek(fd, offset + i + dev_bsize, 0);
    399 			printf(" %ld,", blk + i / dev_bsize);
    400 		}
    401 	printf("\n");
    402 	return;
    403 }
    404 
    405 /*
    406  * allocate a data block with the specified number of fragments
    407  */
    408 ufs_daddr_t
    409 allocblk(frags)
    410 	long frags;
    411 {
    412 	int i, j, k, cg, baseblk;
    413 	struct cg *cgp = cgrp;
    414 
    415 	if (frags <= 0 || frags > sblock->fs_frag)
    416 		return (0);
    417 	for (i = 0; i < maxfsblock - sblock->fs_frag; i += sblock->fs_frag) {
    418 		for (j = 0; j <= sblock->fs_frag - frags; j++) {
    419 			if (testbmap(i + j))
    420 				continue;
    421 			for (k = 1; k < frags; k++)
    422 				if (testbmap(i + j + k))
    423 					break;
    424 			if (k < frags) {
    425 				j += k;
    426 				continue;
    427 			}
    428 			cg = dtog(sblock, i + j);
    429 			getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
    430 			memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
    431 			if ((doswap && !needswap) || (!doswap && needswap))
    432 				swap_cg(cgblk.b_un.b_cg, cgp);
    433 			if (!cg_chkmagic(cgp, 0))
    434 				pfatal("CG %d: ALLOCBLK: BAD MAGIC NUMBER\n",
    435 				    cg);
    436 			baseblk = dtogd(sblock, i + j);
    437 			for (k = 0; k < frags; k++) {
    438 				setbmap(i + j + k);
    439 				clrbit(cg_blksfree(cgp, 0), baseblk + k);
    440 			}
    441 			n_blks += frags;
    442 			if (frags == sblock->fs_frag)
    443 				cgp->cg_cs.cs_nbfree--;
    444 			else
    445 				cgp->cg_cs.cs_nffree -= frags;
    446 			cgdirty();
    447 			return (i + j);
    448 		}
    449 	}
    450 	return (0);
    451 }
    452 
    453 /*
    454  * Free a previously allocated block
    455  */
    456 void
    457 freeblk(blkno, frags)
    458 	ufs_daddr_t blkno;
    459 	long frags;
    460 {
    461 	struct inodesc idesc;
    462 
    463 	idesc.id_blkno = blkno;
    464 	idesc.id_numfrags = frags;
    465 	(void)pass4check(&idesc);
    466 }
    467 
    468 /*
    469  * Find a pathname
    470  */
    471 void
    472 getpathname(namebuf, curdir, ino)
    473 	char *namebuf;
    474 	ino_t curdir, ino;
    475 {
    476 	int len;
    477 	char *cp;
    478 	struct inodesc idesc;
    479 	static int busy = 0;
    480 
    481 	if (curdir == ino && ino == ROOTINO) {
    482 		(void)strcpy(namebuf, "/");
    483 		return;
    484 	}
    485 	if (busy ||
    486 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
    487 		(void)strcpy(namebuf, "?");
    488 		return;
    489 	}
    490 	busy = 1;
    491 	memset(&idesc, 0, sizeof(struct inodesc));
    492 	idesc.id_type = DATA;
    493 	idesc.id_fix = IGNORE;
    494 	cp = &namebuf[MAXPATHLEN - 1];
    495 	*cp = '\0';
    496 	if (curdir != ino) {
    497 		idesc.id_parent = curdir;
    498 		goto namelookup;
    499 	}
    500 	while (ino != ROOTINO) {
    501 		idesc.id_number = ino;
    502 		idesc.id_func = findino;
    503 		idesc.id_name = "..";
    504 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
    505 			break;
    506 	namelookup:
    507 		idesc.id_number = idesc.id_parent;
    508 		idesc.id_parent = ino;
    509 		idesc.id_func = findname;
    510 		idesc.id_name = namebuf;
    511 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
    512 			break;
    513 		len = strlen(namebuf);
    514 		cp -= len;
    515 		memmove(cp, namebuf, (size_t)len);
    516 		*--cp = '/';
    517 		if (cp < &namebuf[MAXNAMLEN])
    518 			break;
    519 		ino = idesc.id_number;
    520 	}
    521 	busy = 0;
    522 	if (ino != ROOTINO)
    523 		*--cp = '?';
    524 	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
    525 }
    526 
    527 void
    528 catch(sig)
    529 	int sig;
    530 {
    531 	if (!doinglevel2) {
    532 		markclean = 0;
    533 		ckfini();
    534 	}
    535 	exit(12);
    536 }
    537 
    538 /*
    539  * When preening, allow a single quit to signal
    540  * a special exit after filesystem checks complete
    541  * so that reboot sequence may be interrupted.
    542  */
    543 void
    544 catchquit(sig)
    545 	int sig;
    546 {
    547 	extern int returntosingle;
    548 
    549 	printf("returning to single-user after filesystem check\n");
    550 	returntosingle = 1;
    551 	(void)signal(SIGQUIT, SIG_DFL);
    552 }
    553 
    554 /*
    555  * Ignore a single quit signal; wait and flush just in case.
    556  * Used by child processes in preen.
    557  */
    558 void
    559 voidquit(sig)
    560 	int sig;
    561 {
    562 
    563 	sleep(1);
    564 	(void)signal(SIGQUIT, SIG_IGN);
    565 	(void)signal(SIGQUIT, SIG_DFL);
    566 }
    567 
    568 /*
    569  * determine whether an inode should be fixed.
    570  */
    571 int
    572 dofix(idesc, msg)
    573 	struct inodesc *idesc;
    574 	char *msg;
    575 {
    576 
    577 	switch (idesc->id_fix) {
    578 
    579 	case DONTKNOW:
    580 		if (idesc->id_type == DATA)
    581 			direrror(idesc->id_number, msg);
    582 		else
    583 			pwarn("%s", msg);
    584 		if (preen) {
    585 			printf(" (SALVAGED)\n");
    586 			idesc->id_fix = FIX;
    587 			return (ALTERED);
    588 		}
    589 		if (reply("SALVAGE") == 0) {
    590 			idesc->id_fix = NOFIX;
    591 			return (0);
    592 		}
    593 		idesc->id_fix = FIX;
    594 		return (ALTERED);
    595 
    596 	case FIX:
    597 		return (ALTERED);
    598 
    599 	case NOFIX:
    600 	case IGNORE:
    601 		return (0);
    602 
    603 	default:
    604 		errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
    605 	}
    606 	/* NOTREACHED */
    607 	return (0);
    608 }
    609 
    610 void
    611 copyback_cg(blk)
    612 	struct bufarea *blk;
    613 {
    614 
    615 	memcpy(blk->b_un.b_cg, cgrp, sblock->fs_cgsize);
    616 	if (needswap)
    617 		swap_cg(cgrp, blk->b_un.b_cg);
    618 }
    619 
    620 void
    621 swap_cg(o, n)
    622 	struct cg *o, *n;
    623 {
    624 	int i;
    625 	u_int32_t *n32, *o32;
    626 	u_int16_t *n16, *o16;
    627 
    628 	n->cg_firstfield = bswap32(o->cg_firstfield);
    629 	n->cg_magic = bswap32(o->cg_magic);
    630 	n->cg_time = bswap32(o->cg_time);
    631 	n->cg_cgx = bswap32(o->cg_cgx);
    632 	n->cg_ncyl = bswap16(o->cg_ncyl);
    633 	n->cg_niblk = bswap16(o->cg_niblk);
    634 	n->cg_ndblk = bswap32(o->cg_ndblk);
    635 	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
    636 	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
    637 	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
    638 	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
    639 	n->cg_rotor = bswap32(o->cg_rotor);
    640 	n->cg_frotor = bswap32(o->cg_frotor);
    641 	n->cg_irotor = bswap32(o->cg_irotor);
    642 	n->cg_btotoff = bswap32(o->cg_btotoff);
    643 	n->cg_boff = bswap32(o->cg_boff);
    644 	n->cg_iusedoff = bswap32(o->cg_iusedoff);
    645 	n->cg_freeoff = bswap32(o->cg_freeoff);
    646 	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
    647 	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
    648 	n->cg_clusteroff = bswap32(o->cg_clusteroff);
    649 	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
    650 	for (i=0; i < MAXFRAG; i++)
    651 		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
    652 
    653 	if (sblock->fs_postblformat == FS_42POSTBLFMT) { /* old format */
    654 		struct ocg *on, *oo;
    655 		int j;
    656 		on = (struct ocg *)n;
    657 		oo = (struct ocg *)o;
    658 		for(i = 0; i < 8; i++) {
    659 			on->cg_frsum[i] = bswap32(oo->cg_frsum[i]);
    660 		}
    661 		for(i = 0; i < 32; i++) {
    662 			on->cg_btot[i] = bswap32(oo->cg_btot[i]);
    663 			for (j = 0; j < 8; j++)
    664 				on->cg_b[i][j] = bswap16(oo->cg_b[i][j]);
    665 		}
    666 		memmove(on->cg_iused, oo->cg_iused, 256);
    667 		on->cg_magic = bswap32(oo->cg_magic);
    668 	} else {  /* new format */
    669 		if (n->cg_magic == CG_MAGIC) {
    670 			n32 = (u_int32_t*)((u_int8_t*)n + n->cg_btotoff);
    671 			o32 = (u_int32_t*)((u_int8_t*)o + n->cg_btotoff);
    672 			n16 = (u_int16_t*)((u_int8_t*)n + n->cg_boff);
    673 			o16 = (u_int16_t*)((u_int8_t*)o + n->cg_boff);
    674 		} else {
    675 			n32 = (u_int32_t*)((u_int8_t*)n + o->cg_btotoff);
    676 			o32 = (u_int32_t*)((u_int8_t*)o + o->cg_btotoff);
    677 			n16 = (u_int16_t*)((u_int8_t*)n + o->cg_boff);
    678 			o16 = (u_int16_t*)((u_int8_t*)o + o->cg_boff);
    679 		}
    680 		for (i=0; i < sblock->fs_cpg; i++)
    681 			n32[i] = bswap32(o32[i]);
    682 
    683 		for (i=0; i < sblock->fs_cpg * sblock->fs_nrpos; i++)
    684 			n16[i] = bswap16(o16[i]);
    685 
    686 		if (n->cg_magic == CG_MAGIC) {
    687 			n32 = (u_int32_t*)((u_int8_t*)n + n->cg_clustersumoff);
    688 			o32 = (u_int32_t*)((u_int8_t*)o + n->cg_clustersumoff);
    689 		} else {
    690 			n32 = (u_int32_t*)((u_int8_t*)n + o->cg_clustersumoff);
    691 			o32 = (u_int32_t*)((u_int8_t*)o + o->cg_clustersumoff);
    692 		}
    693 		for (i = 1; i < sblock->fs_contigsumsize + 1; i++)
    694 			n32[i] = bswap32(o32[i]);
    695 	}
    696 }
    697