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