Home | History | Annotate | Line # | Download | only in fsck_ffs
utilities.c revision 1.44
      1 /*	$NetBSD: utilities.c,v 1.44 2004/01/15 14:49:28 dbj 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.44 2004/01/15 14:49:28 dbj 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");
    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 (lseek(fd, offset, 0) < 0)
    347 		rwerror("SEEK", blk);
    348 	else if (read(fd, buf, (int)size) == size)
    349 		return (0);
    350 	rwerror("READ", blk);
    351 	if (lseek(fd, offset, 0) < 0)
    352 		rwerror("SEEK", blk);
    353 	errs = 0;
    354 	memset(buf, 0, (size_t)size);
    355 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
    356 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
    357 		if (read(fd, cp, (int)secsize) != secsize) {
    358 			(void)lseek(fd, offset + i + secsize, 0);
    359 			if (secsize != dev_bsize && dev_bsize != 1)
    360 				printf(" %lld (%lld),",
    361 				    (long long)((blk*dev_bsize + i) / secsize),
    362 				    (long long)(blk + i / dev_bsize));
    363 			else
    364 				printf(" %lld,",
    365 				    (long long)(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 	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(" %lld,", (long long)(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 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 				ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
    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 	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, namebuflen, curdir, ino)
    475 	char *namebuf;
    476 	size_t namebuflen;
    477 	ino_t curdir, ino;
    478 {
    479 	int len;
    480 	char *cp;
    481 	struct inodesc idesc;
    482 	static int busy = 0;
    483 	struct inostat *info;
    484 
    485 	if (curdir == ino && ino == ROOTINO) {
    486 		(void)strlcpy(namebuf, "/", namebuflen);
    487 		return;
    488 	}
    489 	info = inoinfo(curdir);
    490 	if (busy || (info->ino_state != DSTATE && info->ino_state != DFOUND)) {
    491 		(void)strlcpy(namebuf, "?", namebuflen);
    492 		return;
    493 	}
    494 	busy = 1;
    495 	memset(&idesc, 0, sizeof(struct inodesc));
    496 	idesc.id_type = DATA;
    497 	idesc.id_fix = IGNORE;
    498 	cp = &namebuf[MAXPATHLEN - 1];
    499 	*cp = '\0';
    500 	if (curdir != ino) {
    501 		idesc.id_parent = curdir;
    502 		goto namelookup;
    503 	}
    504 	while (ino != ROOTINO) {
    505 		idesc.id_number = ino;
    506 		idesc.id_func = findino;
    507 		idesc.id_name = "..";
    508 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
    509 			break;
    510 	namelookup:
    511 		idesc.id_number = idesc.id_parent;
    512 		idesc.id_parent = ino;
    513 		idesc.id_func = findname;
    514 		idesc.id_name = namebuf;
    515 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
    516 			break;
    517 		len = strlen(namebuf);
    518 		cp -= len;
    519 		memmove(cp, namebuf, (size_t)len);
    520 		*--cp = '/';
    521 		if (cp < &namebuf[MAXNAMLEN])
    522 			break;
    523 		ino = idesc.id_number;
    524 	}
    525 	busy = 0;
    526 	if (ino != ROOTINO)
    527 		*--cp = '?';
    528 	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
    529 }
    530 
    531 void
    532 catch(sig)
    533 	int sig;
    534 {
    535 	if (!doinglevel2) {
    536 		markclean = 0;
    537 		ckfini();
    538 	}
    539 	exit(12);
    540 }
    541 
    542 /*
    543  * When preening, allow a single quit to signal
    544  * a special exit after filesystem checks complete
    545  * so that reboot sequence may be interrupted.
    546  */
    547 void
    548 catchquit(sig)
    549 	int sig;
    550 {
    551 	int errsave = errno;
    552 
    553 	printf("returning to single-user after file system check\n");
    554 	returntosingle = 1;
    555 	(void)signal(SIGQUIT, SIG_DFL);
    556 	errno = errsave;
    557 }
    558 
    559 /*
    560  * Ignore a single quit signal; wait and flush just in case.
    561  * Used by child processes in preen.
    562  */
    563 void
    564 voidquit(sig)
    565 	int sig;
    566 {
    567 	int errsave = errno;
    568 
    569 	sleep(1);
    570 	(void)signal(SIGQUIT, SIG_IGN);
    571 	(void)signal(SIGQUIT, SIG_DFL);
    572 	errno = errsave;
    573 }
    574 
    575 /*
    576  * determine whether an inode should be fixed.
    577  */
    578 int
    579 dofix(idesc, msg)
    580 	struct inodesc *idesc;
    581 	char *msg;
    582 {
    583 
    584 	switch (idesc->id_fix) {
    585 
    586 	case DONTKNOW:
    587 		if (idesc->id_type == DATA)
    588 			direrror(idesc->id_number, msg);
    589 		else
    590 			pwarn("%s", msg);
    591 		if (preen) {
    592 			printf(" (SALVAGED)\n");
    593 			idesc->id_fix = FIX;
    594 			return (ALTERED);
    595 		}
    596 		if (reply("SALVAGE") == 0) {
    597 			idesc->id_fix = NOFIX;
    598 			return (0);
    599 		}
    600 		idesc->id_fix = FIX;
    601 		return (ALTERED);
    602 
    603 	case FIX:
    604 		return (ALTERED);
    605 
    606 	case NOFIX:
    607 	case IGNORE:
    608 		return (0);
    609 
    610 	default:
    611 		errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
    612 	}
    613 	/* NOTREACHED */
    614 	return (0);
    615 }
    616 
    617 void
    618 copyback_cg(blk)
    619 	struct bufarea *blk;
    620 {
    621 
    622 	memcpy(blk->b_un.b_cg, cgrp, sblock->fs_cgsize);
    623 	if (needswap)
    624 		ffs_cg_swap(cgrp, blk->b_un.b_cg, sblock);
    625 }
    626 
    627 void
    628 infohandler(int sig)
    629 {
    630 	got_siginfo = 1;
    631 }
    632 
    633 /*
    634  * Look up state information for an inode.
    635  */
    636 struct inostat *
    637 inoinfo(ino_t inum)
    638 {
    639 	static struct inostat unallocated = { USTATE, 0, 0 };
    640 	struct inostatlist *ilp;
    641 	int iloff;
    642 
    643 	if (inum > maxino)
    644 		errx(EEXIT, "inoinfo: inumber %d out of range", inum);
    645 	ilp = &inostathead[inum / sblock->fs_ipg];
    646 	iloff = inum % sblock->fs_ipg;
    647 	if (iloff >= ilp->il_numalloced)
    648 		return (&unallocated);
    649 	return (&ilp->il_stat[iloff]);
    650 }
    651 
    652 void
    653 sb_oldfscompat_read(struct fs *fs, struct fs **fssave)
    654 {
    655 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
    656 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
    657 		return;
    658 
    659 	/* Save a copy of fields that may be modified for compatibility */
    660 	if (fssave) {
    661 		if (!*fssave)
    662 			*fssave = malloc(sizeof(struct fs));
    663 		if (!*fssave)
    664 			errx(EEXIT, "cannot allocate space for compat store");
    665 		memmove(*fssave, fs, sizeof(struct fs));
    666 
    667 		if (debug)
    668 			printf("detected ufs1 superblock not yet updated for ufs2 kernels\n");
    669 
    670 		if (doswap) {
    671 			uint16_t postbl[256];
    672 			int i, n;
    673 
    674 			if (fs->fs_old_postblformat == FS_42POSTBLFMT)
    675 				n = 256;
    676 			else
    677 				n = 128;
    678 
    679 			/* extract the postbl from the unswapped superblock */
    680 			if (!needswap)
    681 				ffs_sb_swap(*fssave, *fssave);
    682 			memmove(postbl, (&(*fssave)->fs_old_postbl_start), n);
    683 			if (!needswap)
    684 				ffs_sb_swap(*fssave, *fssave);
    685 
    686 			/* Now swap it */
    687 			for (i=0; i < n; i++)
    688 				postbl[i] = bswap16(postbl[i]);
    689 
    690 			/* And put it back such that it will get correctly
    691 			 * unscrambled if it is swapped again on the way out
    692 			 */
    693 			if (needswap)
    694 				ffs_sb_swap(*fssave, *fssave);
    695 			memmove((&(*fssave)->fs_old_postbl_start), postbl, n);
    696 			if (needswap)
    697 				ffs_sb_swap(*fssave, *fssave);
    698 		}
    699 
    700 	}
    701 
    702 	/* These fields will be overwritten by their
    703 	 * original values in fs_oldfscompat_write, so it is harmless
    704 	 * to modify them here.
    705 	 */
    706 	fs->fs_cstotal.cs_ndir =
    707 	    fs->fs_old_cstotal.cs_ndir;
    708 	fs->fs_cstotal.cs_nbfree =
    709 	    fs->fs_old_cstotal.cs_nbfree;
    710 	fs->fs_cstotal.cs_nifree =
    711 	    fs->fs_old_cstotal.cs_nifree;
    712 	fs->fs_cstotal.cs_nffree =
    713 	    fs->fs_old_cstotal.cs_nffree;
    714 
    715 	fs->fs_maxbsize = fs->fs_bsize;
    716 	fs->fs_time = fs->fs_old_time;
    717 	fs->fs_size = fs->fs_old_size;
    718 	fs->fs_dsize = fs->fs_old_dsize;
    719 	fs->fs_csaddr = fs->fs_old_csaddr;
    720 	fs->fs_sblockloc = SBLOCK_UFS1;
    721 
    722 	fs->fs_flags = fs->fs_old_flags;
    723 
    724 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
    725 		fs->fs_old_nrpos = 8;
    726 		fs->fs_old_npsect = fs->fs_old_nsect;
    727 		fs->fs_old_interleave = 1;
    728 		fs->fs_old_trackskew = 0;
    729 	}
    730 }
    731 
    732 void
    733 sb_oldfscompat_write(struct fs *fs, struct fs *fssave)
    734 {
    735 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
    736 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
    737 		return;
    738 
    739 	fs->fs_old_flags = fs->fs_flags;
    740 	fs->fs_old_time = fs->fs_time;
    741 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
    742 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
    743 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
    744 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
    745 
    746 	fs->fs_flags = fssave->fs_flags;
    747 
    748 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
    749 		fs->fs_old_nrpos = fssave->fs_old_nrpos;
    750 		fs->fs_old_npsect = fssave->fs_old_npsect;
    751 		fs->fs_old_interleave = fssave->fs_old_interleave;
    752 		fs->fs_old_trackskew = fssave->fs_old_trackskew;
    753 	}
    754 
    755 	memmove(&fs->fs_old_postbl_start, &fssave->fs_old_postbl_start,
    756 	    ((fs->fs_old_postblformat == FS_42POSTBLFMT) ?
    757 	    512 : 256));
    758 }
    759