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