Home | History | Annotate | Line # | Download | only in fsck_ffs
utilities.c revision 1.30
      1 /*	$NetBSD: utilities.c,v 1.30 2001/02/04 21:25:54 christos 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.30 2001/02/04 21:25:54 christos 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 extern int returntosingle;
     70 
     71 int
     72 ftypeok(dp)
     73 	struct dinode *dp;
     74 {
     75 	switch (iswap16(dp->di_mode) & IFMT) {
     76 
     77 	case IFDIR:
     78 	case IFREG:
     79 	case IFBLK:
     80 	case IFCHR:
     81 	case IFLNK:
     82 	case IFSOCK:
     83 	case IFIFO:
     84 		return (1);
     85 
     86 	default:
     87 		if (debug)
     88 			printf("bad file type 0%o\n", iswap16(dp->di_mode));
     89 		return (0);
     90 	}
     91 }
     92 
     93 int
     94 reply(question)
     95 	char *question;
     96 {
     97 	int persevere;
     98 	char c;
     99 
    100 	if (preen)
    101 		pfatal("INTERNAL ERROR: GOT TO reply()");
    102 	persevere = !strcmp(question, "CONTINUE");
    103 	printf("\n");
    104 	if (!persevere && (nflag || fswritefd < 0)) {
    105 		printf("%s? no\n\n", question);
    106 		resolved = 0;
    107 		return (0);
    108 	}
    109 	if (yflag || (persevere && nflag)) {
    110 		printf("%s? yes\n\n", question);
    111 		return (1);
    112 	}
    113 	do	{
    114 		printf("%s? [yn] ", question);
    115 		(void) fflush(stdout);
    116 		c = getc(stdin);
    117 		while (c != '\n' && getc(stdin) != '\n') {
    118 			if (feof(stdin)) {
    119 				resolved = 0;
    120 				return (0);
    121 			}
    122 		}
    123 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    124 	printf("\n");
    125 	if (c == 'y' || c == 'Y')
    126 		return (1);
    127 	resolved = 0;
    128 	return (0);
    129 }
    130 
    131 /*
    132  * Malloc buffers and set up cache.
    133  */
    134 void
    135 bufinit()
    136 {
    137 	struct bufarea *bp;
    138 	long bufcnt, i;
    139 	char *bufp;
    140 
    141 	pbp = pdirbp = (struct bufarea *)0;
    142 	bufp = malloc((unsigned int)sblock->fs_bsize);
    143 	if (bufp == 0)
    144 		errx(EEXIT, "cannot allocate buffer pool");
    145 	cgblk.b_un.b_buf = bufp;
    146 	initbarea(&cgblk);
    147 	bufhead.b_next = bufhead.b_prev = &bufhead;
    148 	bufcnt = MAXBUFSPACE / sblock->fs_bsize;
    149 	if (bufcnt < MINBUFS)
    150 		bufcnt = MINBUFS;
    151 	for (i = 0; i < bufcnt; i++) {
    152 		bp = (struct bufarea *)malloc(sizeof(struct bufarea));
    153 		bufp = malloc((unsigned int)sblock->fs_bsize);
    154 		if (bp == NULL || bufp == NULL) {
    155 			if (i >= MINBUFS)
    156 				break;
    157 			errx(EEXIT, "cannot allocate buffer pool");
    158 		}
    159 		bp->b_un.b_buf = bufp;
    160 		bp->b_prev = &bufhead;
    161 		bp->b_next = bufhead.b_next;
    162 		bufhead.b_next->b_prev = bp;
    163 		bufhead.b_next = bp;
    164 		initbarea(bp);
    165 	}
    166 	bufhead.b_size = i;	/* save number of buffers */
    167 }
    168 
    169 /*
    170  * Manage a cache of directory blocks.
    171  */
    172 struct bufarea *
    173 getdatablk(blkno, size)
    174 	ufs_daddr_t blkno;
    175 	long size;
    176 {
    177 	struct bufarea *bp;
    178 
    179 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
    180 		if (bp->b_bno == fsbtodb(sblock, blkno))
    181 			goto foundit;
    182 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
    183 		if ((bp->b_flags & B_INUSE) == 0)
    184 			break;
    185 	if (bp == &bufhead)
    186 		errx(EEXIT, "deadlocked buffer pool");
    187 	getblk(bp, blkno, size);
    188 	/* fall through */
    189 foundit:
    190 	totalreads++;
    191 	bp->b_prev->b_next = bp->b_next;
    192 	bp->b_next->b_prev = bp->b_prev;
    193 	bp->b_prev = &bufhead;
    194 	bp->b_next = bufhead.b_next;
    195 	bufhead.b_next->b_prev = bp;
    196 	bufhead.b_next = bp;
    197 	bp->b_flags |= B_INUSE;
    198 	return (bp);
    199 }
    200 
    201 void
    202 getblk(bp, blk, size)
    203 	struct bufarea *bp;
    204 	ufs_daddr_t blk;
    205 	long size;
    206 {
    207 	ufs_daddr_t dblk;
    208 
    209 	dblk = fsbtodb(sblock, blk);
    210 	if (bp->b_bno != dblk) {
    211 		flush(fswritefd, bp);
    212 		diskreads++;
    213 		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
    214 		bp->b_bno = dblk;
    215 		bp->b_size = size;
    216 	}
    217 }
    218 
    219 void
    220 flush(fd, bp)
    221 	int fd;
    222 	struct bufarea *bp;
    223 {
    224 	int i, j;
    225 
    226 	if (!bp->b_dirty)
    227 		return;
    228 	if (bp->b_errs != 0)
    229 		pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
    230 		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
    231 		    bp->b_bno);
    232 	bp->b_dirty = 0;
    233 	bp->b_errs = 0;
    234 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
    235 	if (bp != &sblk)
    236 		return;
    237 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
    238 		int size = sblock->fs_cssize - i < sblock->fs_bsize ?
    239 			sblock->fs_cssize - i : sblock->fs_bsize;
    240 		/*
    241 		 * The following routines assumes that struct csum is made of
    242 		 * u_int32_t's
    243 		 */
    244 		if (needswap) {
    245 			int k;
    246 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    247 
    248 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    249 				cd[k] = bswap32(cd[k]);
    250 		}
    251 		bwrite(fswritefd, (char *)sblock->fs_csp[j],
    252 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    253 		    size);
    254 		if (needswap) {
    255 			int k;
    256 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    257 
    258 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    259 				cd[k] = bswap32(cd[k]);
    260 		}
    261 	}
    262 }
    263 
    264 static void
    265 rwerror(mesg, blk)
    266 	char *mesg;
    267 	ufs_daddr_t blk;
    268 {
    269 
    270 	if (preen == 0)
    271 		printf("\n");
    272 	pfatal("CANNOT %s: BLK %d", mesg, blk);
    273 	if (reply("CONTINUE") == 0)
    274 		exit(EEXIT);
    275 }
    276 
    277 void
    278 ckfini()
    279 {
    280 	struct bufarea *bp, *nbp;
    281 	int ofsmodified, cnt = 0;
    282 
    283 	if (fswritefd < 0) {
    284 		(void)close(fsreadfd);
    285 		return;
    286 	}
    287 	flush(fswritefd, &sblk);
    288 	if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
    289 	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
    290 		sblk.b_bno = SBOFF / dev_bsize;
    291 		sbdirty();
    292 		flush(fswritefd, &sblk);
    293 	}
    294 	flush(fswritefd, &cgblk);
    295 	free(cgblk.b_un.b_buf);
    296 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
    297 		cnt++;
    298 		flush(fswritefd, bp);
    299 		nbp = bp->b_prev;
    300 		free(bp->b_un.b_buf);
    301 		free((char *)bp);
    302 	}
    303 	if (bufhead.b_size != cnt)
    304 		errx(EEXIT, "Panic: lost %d buffers", bufhead.b_size - cnt);
    305 	pbp = pdirbp = (struct bufarea *)0;
    306 	if (markclean && (sblock->fs_clean & FS_ISCLEAN) == 0) {
    307 		/*
    308 		 * Mark the file system as clean, and sync the superblock.
    309 		 */
    310 		if (preen)
    311 			pwarn("MARKING FILE SYSTEM CLEAN\n");
    312 		else if (!reply("MARK FILE SYSTEM CLEAN"))
    313 			markclean = 0;
    314 		if (markclean) {
    315 			sblock->fs_clean = FS_ISCLEAN;
    316 			sbdirty();
    317 			ofsmodified = fsmodified;
    318 			flush(fswritefd, &sblk);
    319 #if LITE2BORKEN
    320 			fsmodified = ofsmodified;
    321 #endif
    322 			if (!preen)
    323 				printf(
    324 				    "\n***** FILE SYSTEM MARKED CLEAN *****\n");
    325 		}
    326 	}
    327 	if (debug)
    328 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
    329 		    totalreads, (int)(diskreads * 100 / totalreads));
    330 	(void)close(fsreadfd);
    331 	(void)close(fswritefd);
    332 }
    333 
    334 int
    335 bread(fd, buf, blk, size)
    336 	int fd;
    337 	char *buf;
    338 	ufs_daddr_t blk;
    339 	long size;
    340 {
    341 	char *cp;
    342 	int i, errs;
    343 	off_t offset;
    344 
    345 	offset = blk;
    346 	offset *= dev_bsize;
    347 	if (lseek(fd, offset, 0) < 0)
    348 		rwerror("SEEK", blk);
    349 	else if (read(fd, buf, (int)size) == size)
    350 		return (0);
    351 	rwerror("READ", blk);
    352 	if (lseek(fd, offset, 0) < 0)
    353 		rwerror("SEEK", blk);
    354 	errs = 0;
    355 	memset(buf, 0, (size_t)size);
    356 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
    357 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
    358 		if (read(fd, cp, (int)secsize) != secsize) {
    359 			(void)lseek(fd, offset + i + secsize, 0);
    360 			if (secsize != dev_bsize && dev_bsize != 1)
    361 				printf(" %ld (%ld),",
    362 				    (blk * dev_bsize + i) / secsize,
    363 				    blk + i / dev_bsize);
    364 			else
    365 				printf(" %ld,", blk + i / dev_bsize);
    366 			errs++;
    367 		}
    368 	}
    369 	printf("\n");
    370 	return (errs);
    371 }
    372 
    373 void
    374 bwrite(fd, buf, blk, size)
    375 	int fd;
    376 	char *buf;
    377 	ufs_daddr_t blk;
    378 	long size;
    379 {
    380 	int i;
    381 	char *cp;
    382 	off_t offset;
    383 
    384 	if (fd < 0)
    385 		return;
    386 	offset = blk;
    387 	offset *= dev_bsize;
    388 	if (lseek(fd, offset, 0) < 0)
    389 		rwerror("SEEK", blk);
    390 	else if (write(fd, buf, (int)size) == size) {
    391 		fsmodified = 1;
    392 		return;
    393 	}
    394 	rwerror("WRITE", blk);
    395 	if (lseek(fd, offset, 0) < 0)
    396 		rwerror("SEEK", blk);
    397 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
    398 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
    399 		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
    400 			(void)lseek(fd, offset + i + dev_bsize, 0);
    401 			printf(" %ld,", blk + i / dev_bsize);
    402 		}
    403 	printf("\n");
    404 	return;
    405 }
    406 
    407 /*
    408  * allocate a data block with the specified number of fragments
    409  */
    410 ufs_daddr_t
    411 allocblk(frags)
    412 	long frags;
    413 {
    414 	int i, j, k, cg, baseblk;
    415 	struct cg *cgp = cgrp;
    416 
    417 	if (frags <= 0 || frags > sblock->fs_frag)
    418 		return (0);
    419 	for (i = 0; i < maxfsblock - sblock->fs_frag; i += sblock->fs_frag) {
    420 		for (j = 0; j <= sblock->fs_frag - frags; j++) {
    421 			if (testbmap(i + j))
    422 				continue;
    423 			for (k = 1; k < frags; k++)
    424 				if (testbmap(i + j + k))
    425 					break;
    426 			if (k < frags) {
    427 				j += k;
    428 				continue;
    429 			}
    430 			cg = dtog(sblock, i + j);
    431 			getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
    432 			memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
    433 			if ((doswap && !needswap) || (!doswap && needswap))
    434 				swap_cg(cgblk.b_un.b_cg, cgp);
    435 			if (!cg_chkmagic(cgp, 0))
    436 				pfatal("CG %d: ALLOCBLK: BAD MAGIC NUMBER\n",
    437 				    cg);
    438 			baseblk = dtogd(sblock, i + j);
    439 			for (k = 0; k < frags; k++) {
    440 				setbmap(i + j + k);
    441 				clrbit(cg_blksfree(cgp, 0), baseblk + k);
    442 			}
    443 			n_blks += frags;
    444 			if (frags == sblock->fs_frag)
    445 				cgp->cg_cs.cs_nbfree--;
    446 			else
    447 				cgp->cg_cs.cs_nffree -= frags;
    448 			cgdirty();
    449 			return (i + j);
    450 		}
    451 	}
    452 	return (0);
    453 }
    454 
    455 /*
    456  * Free a previously allocated block
    457  */
    458 void
    459 freeblk(blkno, frags)
    460 	ufs_daddr_t blkno;
    461 	long frags;
    462 {
    463 	struct inodesc idesc;
    464 
    465 	idesc.id_blkno = blkno;
    466 	idesc.id_numfrags = frags;
    467 	(void)pass4check(&idesc);
    468 }
    469 
    470 /*
    471  * Find a pathname
    472  */
    473 void
    474 getpathname(namebuf, curdir, ino)
    475 	char *namebuf;
    476 	ino_t curdir, ino;
    477 {
    478 	int len;
    479 	char *cp;
    480 	struct inodesc idesc;
    481 	static int busy = 0;
    482 
    483 	if (curdir == ino && ino == ROOTINO) {
    484 		(void)strcpy(namebuf, "/");
    485 		return;
    486 	}
    487 	if (busy ||
    488 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
    489 		(void)strcpy(namebuf, "?");
    490 		return;
    491 	}
    492 	busy = 1;
    493 	memset(&idesc, 0, sizeof(struct inodesc));
    494 	idesc.id_type = DATA;
    495 	idesc.id_fix = IGNORE;
    496 	cp = &namebuf[MAXPATHLEN - 1];
    497 	*cp = '\0';
    498 	if (curdir != ino) {
    499 		idesc.id_parent = curdir;
    500 		goto namelookup;
    501 	}
    502 	while (ino != ROOTINO) {
    503 		idesc.id_number = ino;
    504 		idesc.id_func = findino;
    505 		idesc.id_name = "..";
    506 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
    507 			break;
    508 	namelookup:
    509 		idesc.id_number = idesc.id_parent;
    510 		idesc.id_parent = ino;
    511 		idesc.id_func = findname;
    512 		idesc.id_name = namebuf;
    513 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
    514 			break;
    515 		len = strlen(namebuf);
    516 		cp -= len;
    517 		memmove(cp, namebuf, (size_t)len);
    518 		*--cp = '/';
    519 		if (cp < &namebuf[MAXNAMLEN])
    520 			break;
    521 		ino = idesc.id_number;
    522 	}
    523 	busy = 0;
    524 	if (ino != ROOTINO)
    525 		*--cp = '?';
    526 	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
    527 }
    528 
    529 void
    530 catch(sig)
    531 	int sig;
    532 {
    533 	if (!doinglevel2) {
    534 		markclean = 0;
    535 		ckfini();
    536 	}
    537 	exit(12);
    538 }
    539 
    540 /*
    541  * When preening, allow a single quit to signal
    542  * a special exit after filesystem checks complete
    543  * so that reboot sequence may be interrupted.
    544  */
    545 void
    546 catchquit(sig)
    547 	int sig;
    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