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