Home | History | Annotate | Line # | Download | only in fsck_ffs
utilities.c revision 1.55
      1 /*	$NetBSD: utilities.c,v 1.55 2008/02/23 21:41:48 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
     36 #else
     37 __RCSID("$NetBSD: utilities.c,v 1.55 2008/02/23 21:41:48 christos Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/time.h>
     43 
     44 #include <ufs/ufs/dinode.h>
     45 #include <ufs/ufs/dir.h>
     46 #include <ufs/ffs/fs.h>
     47 #include <ufs/ffs/ffs_extern.h>
     48 #include <ufs/ufs/ufs_bswap.h>
     49 
     50 #include <ctype.h>
     51 #include <err.h>
     52 #include <errno.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 #include <signal.h>
     58 
     59 #include "fsutil.h"
     60 #include "fsck.h"
     61 #include "extern.h"
     62 #include "exitvalues.h"
     63 
     64 long	diskreads, totalreads;	/* Disk cache statistics */
     65 
     66 static void rwerror(const char *, daddr_t);
     67 
     68 extern int returntosingle;
     69 
     70 int
     71 ftypeok(union dinode *dp)
     72 {
     73 	switch (iswap16(DIP(dp, 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(DIP(dp, mode)));
     87 		return (0);
     88 	}
     89 }
     90 
     91 int
     92 reply(const char *question)
     93 {
     94 	int persevere;
     95 	char c;
     96 
     97 	if (preen)
     98 		pfatal("INTERNAL ERROR: GOT TO reply()");
     99 	persevere = !strcmp(question, "CONTINUE");
    100 	printf("\n");
    101 	if (!persevere && (nflag || fswritefd < 0)) {
    102 		printf("%s? no\n\n", question);
    103 		resolved = 0;
    104 		return (0);
    105 	}
    106 	if (yflag || (persevere && nflag)) {
    107 		printf("%s? yes\n\n", question);
    108 		return (1);
    109 	}
    110 	do	{
    111 		printf("%s? [yn] ", question);
    112 		(void) fflush(stdout);
    113 		c = getc(stdin);
    114 		while (c != '\n' && getc(stdin) != '\n') {
    115 			if (feof(stdin)) {
    116 				resolved = 0;
    117 				return (0);
    118 			}
    119 		}
    120 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    121 	printf("\n");
    122 	if (c == 'y' || c == 'Y')
    123 		return (1);
    124 	resolved = 0;
    125 	return (0);
    126 }
    127 
    128 /*
    129  * Malloc buffers and set up cache.
    130  */
    131 void
    132 bufinit(void)
    133 {
    134 	struct bufarea *bp;
    135 	long bufcnt, i;
    136 	char *bufp;
    137 
    138 	pbp = pdirbp = (struct bufarea *)0;
    139 	bufp = malloc((unsigned int)sblock->fs_bsize);
    140 	if (bufp == 0)
    141 		errexit("cannot allocate buffer pool");
    142 	cgblk.b_un.b_buf = bufp;
    143 	initbarea(&cgblk);
    144 	bufp = malloc((unsigned int)APPLEUFS_LABEL_SIZE);
    145 	if (bufp == 0)
    146 		errexit("cannot allocate buffer pool");
    147 	appleufsblk.b_un.b_buf = bufp;
    148 	initbarea(&appleufsblk);
    149 	bufhead.b_next = bufhead.b_prev = &bufhead;
    150 	bufcnt = MAXBUFSPACE / sblock->fs_bsize;
    151 	if (bufcnt < MINBUFS)
    152 		bufcnt = MINBUFS;
    153 	for (i = 0; i < bufcnt; i++) {
    154 		bp = malloc(sizeof(struct bufarea));
    155 		bufp = malloc((unsigned int)sblock->fs_bsize);
    156 		if (bp == NULL || bufp == NULL) {
    157 			if (i >= MINBUFS) {
    158 				if (bp)
    159 					free(bp);
    160 				if (bufp)
    161 					free(bufp);
    162 				break;
    163 			}
    164 			errexit("cannot allocate buffer pool");
    165 		}
    166 		bp->b_un.b_buf = bufp;
    167 		bp->b_prev = &bufhead;
    168 		bp->b_next = bufhead.b_next;
    169 		bufhead.b_next->b_prev = bp;
    170 		bufhead.b_next = bp;
    171 		initbarea(bp);
    172 	}
    173 	bufhead.b_size = i;	/* save number of buffers */
    174 }
    175 
    176 /*
    177  * Manage a cache of directory blocks.
    178  */
    179 struct bufarea *
    180 getdatablk(daddr_t blkno, long size)
    181 {
    182 	struct bufarea *bp;
    183 
    184 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
    185 		if (bp->b_bno == fsbtodb(sblock, blkno))
    186 			goto foundit;
    187 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
    188 		if ((bp->b_flags & B_INUSE) == 0)
    189 			break;
    190 	if (bp == &bufhead)
    191 		errexit("deadlocked buffer pool");
    192 	/* fall through */
    193 foundit:
    194 	getblk(bp, blkno, size);
    195 	bp->b_prev->b_next = bp->b_next;
    196 	bp->b_next->b_prev = bp->b_prev;
    197 	bp->b_prev = &bufhead;
    198 	bp->b_next = bufhead.b_next;
    199 	bufhead.b_next->b_prev = bp;
    200 	bufhead.b_next = bp;
    201 	bp->b_flags |= B_INUSE;
    202 	return (bp);
    203 }
    204 
    205 void
    206 getblk(struct bufarea *bp, daddr_t blk, long size)
    207 {
    208 	daddr_t dblk;
    209 
    210 	dblk = fsbtodb(sblock, blk);
    211 	totalreads++;
    212 	if (bp->b_bno != dblk) {
    213 		flush(fswritefd, bp);
    214 		diskreads++;
    215 		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
    216 		bp->b_bno = dblk;
    217 		bp->b_size = size;
    218 	}
    219 }
    220 
    221 void
    222 flush(int fd, struct bufarea *bp)
    223 {
    224 	int i, j;
    225 	struct csum *ccsp;
    226 
    227 	if (!bp->b_dirty)
    228 		return;
    229 	if (bp->b_errs != 0)
    230 		pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
    231 		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
    232 		    (long long)bp->b_bno);
    233 	bp->b_dirty = 0;
    234 	bp->b_errs = 0;
    235 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
    236 	if (bp != &sblk)
    237 		return;
    238 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
    239 		int size = sblock->fs_cssize - i < sblock->fs_bsize ?
    240 			sblock->fs_cssize - i : sblock->fs_bsize;
    241 		ccsp = (struct csum *)((char *)sblock->fs_csp + i);
    242 		if (needswap)
    243 			ffs_csum_swap(ccsp, ccsp, size);
    244 		bwrite(fswritefd, (char *)ccsp,
    245 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    246 		    size);
    247 		if (needswap)
    248 			ffs_csum_swap(ccsp, ccsp, size);
    249 	}
    250 }
    251 
    252 static void
    253 rwerror(const char *mesg, daddr_t blk)
    254 {
    255 
    256 	if (preen == 0)
    257 		printf("\n");
    258 	pfatal("CANNOT %s: BLK %lld", mesg, (long long)blk);
    259 	if (reply("CONTINUE") == 0)
    260 		exit(FSCK_EXIT_CHECK_FAILED);
    261 }
    262 
    263 void
    264 ckfini(void)
    265 {
    266 	struct bufarea *bp, *nbp;
    267 	int ofsmodified, cnt = 0;
    268 
    269 	if (fswritefd < 0) {
    270 		(void)close(fsreadfd);
    271 		return;
    272 	}
    273 	flush(fswritefd, &sblk);
    274 	if (havesb && bflag != 0 &&
    275 	    (preen || reply("UPDATE STANDARD SUPERBLOCK"))) {
    276 		if (preen)
    277 			pwarn("UPDATING STANDARD SUPERBLOCK\n");
    278 		if (!is_ufs2 && (sblock->fs_old_flags & FS_FLAGS_UPDATED) == 0)
    279 			sblk.b_bno = SBLOCK_UFS1 / dev_bsize;
    280 		else
    281 			sblk.b_bno = sblock->fs_sblockloc / dev_bsize;
    282 		sbdirty();
    283 		flush(fswritefd, &sblk);
    284 	}
    285 	flush(fswritefd, &appleufsblk);
    286 	free(appleufsblk.b_un.b_buf);
    287 	flush(fswritefd, &cgblk);
    288 	free(cgblk.b_un.b_buf);
    289 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
    290 		cnt++;
    291 		flush(fswritefd, bp);
    292 		nbp = bp->b_prev;
    293 		free(bp->b_un.b_buf);
    294 		free((char *)bp);
    295 	}
    296 	if (bufhead.b_size != cnt)
    297 		errexit("Panic: lost %d buffers", bufhead.b_size - cnt);
    298 	pbp = pdirbp = (struct bufarea *)0;
    299 	if (markclean && (sblock->fs_clean & FS_ISCLEAN) == 0) {
    300 		/*
    301 		 * Mark the file system as clean, and sync the superblock.
    302 		 */
    303 		if (preen)
    304 			pwarn("MARKING FILE SYSTEM CLEAN\n");
    305 		else if (!reply("MARK FILE SYSTEM CLEAN"))
    306 			markclean = 0;
    307 		if (markclean) {
    308 			sblock->fs_clean = FS_ISCLEAN;
    309 			sblock->fs_pendingblocks = 0;
    310 			sblock->fs_pendinginodes = 0;
    311 			sbdirty();
    312 			ofsmodified = fsmodified;
    313 			flush(fswritefd, &sblk);
    314 #if LITE2BORKEN
    315 			fsmodified = ofsmodified;
    316 #endif
    317 			if (!preen)
    318 				printf(
    319 				    "\n***** FILE SYSTEM MARKED CLEAN *****\n");
    320 		}
    321 	}
    322 	if (debug)
    323 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
    324 		    totalreads, (int)(diskreads * 100 / totalreads));
    325 	(void)close(fsreadfd);
    326 	(void)close(fswritefd);
    327 }
    328 
    329 int
    330 bread(int fd, char *buf, daddr_t blk, long size)
    331 {
    332 	char *cp;
    333 	int i, errs;
    334 	off_t offset;
    335 
    336 	offset = blk;
    337 	offset *= dev_bsize;
    338 	if (pread(fd, buf, (int)size, offset) == size)
    339 		return (0);
    340 	rwerror("READ", blk);
    341 	errs = 0;
    342 	memset(buf, 0, (size_t)size);
    343 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
    344 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
    345 		if (pread(fd, cp, (int)secsize, offset + i) != secsize) {
    346 			if (secsize != dev_bsize && dev_bsize != 1)
    347 				printf(" %lld (%lld),",
    348 				    (long long)((blk*dev_bsize + i) / secsize),
    349 				    (long long)(blk + i / dev_bsize));
    350 			else
    351 				printf(" %lld,",
    352 				    (long long)(blk + i / dev_bsize));
    353 			errs++;
    354 		}
    355 	}
    356 	printf("\n");
    357 	return (errs);
    358 }
    359 
    360 void
    361 bwrite(int fd, char *buf, daddr_t blk, long size)
    362 {
    363 	int i;
    364 	char *cp;
    365 	off_t offset;
    366 
    367 	if (fd < 0)
    368 		return;
    369 	offset = blk;
    370 	offset *= dev_bsize;
    371 	if (pwrite(fd, buf, (int)size, offset) == size) {
    372 		fsmodified = 1;
    373 		return;
    374 	}
    375 	rwerror("WRITE", blk);
    376 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
    377 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
    378 		if (pwrite(fd, cp, (int)dev_bsize, offset + i) != dev_bsize)
    379 			printf(" %lld,", (long long)(blk + i / dev_bsize));
    380 	printf("\n");
    381 	return;
    382 }
    383 
    384 /*
    385  * allocate a data block with the specified number of fragments
    386  */
    387 daddr_t
    388 allocblk(long frags)
    389 {
    390 	int i, j, k, cg, baseblk;
    391 	struct cg *cgp = cgrp;
    392 
    393 	if (frags <= 0 || frags > sblock->fs_frag)
    394 		return (0);
    395 	for (i = 0; i < maxfsblock - sblock->fs_frag; i += sblock->fs_frag) {
    396 		for (j = 0; j <= sblock->fs_frag - frags; j++) {
    397 			if (testbmap(i + j))
    398 				continue;
    399 			for (k = 1; k < frags; k++)
    400 				if (testbmap(i + j + k))
    401 					break;
    402 			if (k < frags) {
    403 				j += k;
    404 				continue;
    405 			}
    406 			cg = dtog(sblock, i + j);
    407 			getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
    408 			memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
    409 			if ((doswap && !needswap) || (!doswap && needswap))
    410 				ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
    411 			if (!cg_chkmagic(cgp, 0))
    412 				pfatal("CG %d: ALLOCBLK: BAD MAGIC NUMBER\n",
    413 				    cg);
    414 			baseblk = dtogd(sblock, i + j);
    415 			for (k = 0; k < frags; k++) {
    416 				setbmap(i + j + k);
    417 				clrbit(cg_blksfree(cgp, 0), baseblk + k);
    418 			}
    419 			n_blks += frags;
    420 			if (frags == sblock->fs_frag)
    421 				cgp->cg_cs.cs_nbfree--;
    422 			else
    423 				cgp->cg_cs.cs_nffree -= frags;
    424 			cgdirty();
    425 			return (i + j);
    426 		}
    427 	}
    428 	return (0);
    429 }
    430 
    431 /*
    432  * Free a previously allocated block
    433  */
    434 void
    435 freeblk(daddr_t blkno, long frags)
    436 {
    437 	struct inodesc idesc;
    438 
    439 	idesc.id_blkno = blkno;
    440 	idesc.id_numfrags = frags;
    441 	(void)pass4check(&idesc);
    442 }
    443 
    444 /*
    445  * Find a pathname
    446  */
    447 void
    448 getpathname(char *namebuf, size_t namebuflen, ino_t curdir, ino_t ino)
    449 {
    450 	int len;
    451 	char *cp;
    452 	struct inodesc idesc;
    453 	static int busy = 0;
    454 	struct inostat *info;
    455 
    456 	if (curdir == ino && ino == ROOTINO) {
    457 		(void)strlcpy(namebuf, "/", namebuflen);
    458 		return;
    459 	}
    460 	info = inoinfo(curdir);
    461 	if (busy || (info->ino_state != DSTATE && info->ino_state != DFOUND)) {
    462 		(void)strlcpy(namebuf, "?", namebuflen);
    463 		return;
    464 	}
    465 	busy = 1;
    466 	memset(&idesc, 0, sizeof(struct inodesc));
    467 	idesc.id_type = DATA;
    468 	idesc.id_fix = IGNORE;
    469 	cp = &namebuf[MAXPATHLEN - 1];
    470 	*cp = '\0';
    471 	if (curdir != ino) {
    472 		idesc.id_parent = curdir;
    473 		goto namelookup;
    474 	}
    475 	while (ino != ROOTINO) {
    476 		idesc.id_number = ino;
    477 		idesc.id_func = findino;
    478 		idesc.id_name = "..";
    479 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
    480 			break;
    481 	namelookup:
    482 		idesc.id_number = idesc.id_parent;
    483 		idesc.id_parent = ino;
    484 		idesc.id_func = findname;
    485 		idesc.id_name = namebuf;
    486 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
    487 			break;
    488 		len = strlen(namebuf);
    489 		cp -= len;
    490 		memmove(cp, namebuf, (size_t)len);
    491 		*--cp = '/';
    492 		if (cp < &namebuf[FFS_MAXNAMLEN])
    493 			break;
    494 		ino = idesc.id_number;
    495 	}
    496 	busy = 0;
    497 	if (ino != ROOTINO)
    498 		*--cp = '?';
    499 	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
    500 }
    501 
    502 void
    503 catch(int sig)
    504 {
    505 	if (!doinglevel2) {
    506 		markclean = 0;
    507 		ckfini();
    508 	}
    509 	exit(FSCK_EXIT_SIGNALLED);
    510 }
    511 
    512 /*
    513  * When preening, allow a single quit to signal
    514  * a special exit after filesystem checks complete
    515  * so that reboot sequence may be interrupted.
    516  */
    517 void
    518 catchquit(int sig)
    519 {
    520 	int errsave = errno;
    521 
    522 	printf("returning to single-user after file system check\n");
    523 	returntosingle = 1;
    524 	(void)signal(SIGQUIT, SIG_DFL);
    525 	errno = errsave;
    526 }
    527 
    528 /*
    529  * Ignore a single quit signal; wait and flush just in case.
    530  * Used by child processes in preen.
    531  */
    532 void
    533 voidquit(int sig)
    534 {
    535 	int errsave = errno;
    536 
    537 	sleep(1);
    538 	(void)signal(SIGQUIT, SIG_IGN);
    539 	(void)signal(SIGQUIT, SIG_DFL);
    540 	errno = errsave;
    541 }
    542 
    543 /*
    544  * determine whether an inode should be fixed.
    545  */
    546 int
    547 dofix(struct inodesc *idesc, const char *msg)
    548 {
    549 
    550 	switch (idesc->id_fix) {
    551 
    552 	case DONTKNOW:
    553 		if (idesc->id_type == DATA)
    554 			direrror(idesc->id_number, msg);
    555 		else
    556 			pwarn("%s", msg);
    557 		if (preen) {
    558 			printf(" (SALVAGED)\n");
    559 			idesc->id_fix = FIX;
    560 			return (ALTERED);
    561 		}
    562 		if (reply("SALVAGE") == 0) {
    563 			idesc->id_fix = NOFIX;
    564 			return (0);
    565 		}
    566 		idesc->id_fix = FIX;
    567 		return (ALTERED);
    568 
    569 	case FIX:
    570 		return (ALTERED);
    571 
    572 	case NOFIX:
    573 	case IGNORE:
    574 		return (0);
    575 
    576 	default:
    577 		errexit("UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
    578 	}
    579 	/* NOTREACHED */
    580 	return (0);
    581 }
    582 
    583 void
    584 copyback_cg(struct bufarea *blk)
    585 {
    586 
    587 	memcpy(blk->b_un.b_cg, cgrp, sblock->fs_cgsize);
    588 	if (needswap)
    589 		ffs_cg_swap(cgrp, blk->b_un.b_cg, sblock);
    590 }
    591 
    592 void
    593 infohandler(int sig)
    594 {
    595 	got_siginfo = 1;
    596 }
    597 
    598 /*
    599  * Look up state information for an inode.
    600  */
    601 struct inostat *
    602 inoinfo(ino_t inum)
    603 {
    604 	static struct inostat unallocated = { USTATE, 0, 0 };
    605 	struct inostatlist *ilp;
    606 	int iloff;
    607 
    608 	if (inum > maxino)
    609 		errexit("inoinfo: inumber %llu out of range",
    610 		    (unsigned long long)inum);
    611 	ilp = &inostathead[inum / sblock->fs_ipg];
    612 	iloff = inum % sblock->fs_ipg;
    613 	if (iloff >= ilp->il_numalloced)
    614 		return (&unallocated);
    615 	return (&ilp->il_stat[iloff]);
    616 }
    617 
    618 void
    619 sb_oldfscompat_read(struct fs *fs, struct fs **fssave)
    620 {
    621 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
    622 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
    623 		return;
    624 
    625 	/* Save a copy of fields that may be modified for compatibility */
    626 	if (fssave) {
    627 		if (!*fssave)
    628 			*fssave = malloc(sizeof(struct fs));
    629 		if (!*fssave)
    630 			errexit("cannot allocate space for compat store");
    631 		memmove(*fssave, fs, sizeof(struct fs));
    632 
    633 		if (debug)
    634 			printf("detected ufs1 superblock not yet updated for ufs2 kernels\n");
    635 
    636 		if (doswap) {
    637 			uint16_t postbl[256];
    638 			int i, n;
    639 
    640 			if (fs->fs_old_postblformat == FS_42POSTBLFMT)
    641 				n = 256;
    642 			else
    643 				n = 128;
    644 
    645 			/* extract the postbl from the unswapped superblock */
    646 			if (!needswap)
    647 				ffs_sb_swap(*fssave, *fssave);
    648 			memmove(postbl, (&(*fssave)->fs_old_postbl_start),
    649 			    n * sizeof(postbl[0]));
    650 			if (!needswap)
    651 				ffs_sb_swap(*fssave, *fssave);
    652 
    653 			/* Now swap it */
    654 			for (i=0; i < n; i++)
    655 				postbl[i] = bswap16(postbl[i]);
    656 
    657 			/* And put it back such that it will get correctly
    658 			 * unscrambled if it is swapped again on the way out
    659 			 */
    660 			if (needswap)
    661 				ffs_sb_swap(*fssave, *fssave);
    662 			memmove((&(*fssave)->fs_old_postbl_start), postbl,
    663 			    n * sizeof(postbl[0]));
    664 			if (needswap)
    665 				ffs_sb_swap(*fssave, *fssave);
    666 		}
    667 
    668 	}
    669 
    670 	/* These fields will be overwritten by their
    671 	 * original values in fs_oldfscompat_write, so it is harmless
    672 	 * to modify them here.
    673 	 */
    674 	fs->fs_cstotal.cs_ndir =
    675 	    fs->fs_old_cstotal.cs_ndir;
    676 	fs->fs_cstotal.cs_nbfree =
    677 	    fs->fs_old_cstotal.cs_nbfree;
    678 	fs->fs_cstotal.cs_nifree =
    679 	    fs->fs_old_cstotal.cs_nifree;
    680 	fs->fs_cstotal.cs_nffree =
    681 	    fs->fs_old_cstotal.cs_nffree;
    682 
    683 	fs->fs_maxbsize = fs->fs_bsize;
    684 	fs->fs_time = fs->fs_old_time;
    685 	fs->fs_size = fs->fs_old_size;
    686 	fs->fs_dsize = fs->fs_old_dsize;
    687 	fs->fs_csaddr = fs->fs_old_csaddr;
    688 	fs->fs_sblockloc = SBLOCK_UFS1;
    689 
    690 	fs->fs_flags = fs->fs_old_flags;
    691 
    692 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
    693 		fs->fs_old_nrpos = 8;
    694 		fs->fs_old_npsect = fs->fs_old_nsect;
    695 		fs->fs_old_interleave = 1;
    696 		fs->fs_old_trackskew = 0;
    697 	}
    698 }
    699 
    700 void
    701 sb_oldfscompat_write(struct fs *fs, struct fs *fssave)
    702 {
    703 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
    704 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
    705 		return;
    706 
    707 	fs->fs_old_flags = fs->fs_flags;
    708 	fs->fs_old_time = fs->fs_time;
    709 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
    710 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
    711 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
    712 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
    713 
    714 	fs->fs_flags = fssave->fs_flags;
    715 
    716 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
    717 		fs->fs_old_nrpos = fssave->fs_old_nrpos;
    718 		fs->fs_old_npsect = fssave->fs_old_npsect;
    719 		fs->fs_old_interleave = fssave->fs_old_interleave;
    720 		fs->fs_old_trackskew = fssave->fs_old_trackskew;
    721 	}
    722 
    723 	memmove(&fs->fs_old_postbl_start, &fssave->fs_old_postbl_start,
    724 	    ((fs->fs_old_postblformat == FS_42POSTBLFMT) ?
    725 	    512 : 256));
    726 }
    727