2001-08-15 07:54:53 +04:00
|
|
|
/* $NetBSD: utilities.c,v 1.31 2001/08/15 03:54:53 lukem Exp $ */
|
1995-04-23 14:33:03 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-06-08 22:57:30 +04:00
|
|
|
* Copyright (c) 1980, 1986, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-09-14 18:36:29 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-18 17:54:19 +03:00
|
|
|
#if 0
|
1997-09-16 20:44:43 +04:00
|
|
|
static char sccsid[] = "@(#)utilities.c 8.6 (Berkeley) 5/19/95";
|
1995-03-18 17:54:19 +03:00
|
|
|
#else
|
2001-08-15 07:54:53 +04:00
|
|
|
__RCSID("$NetBSD: utilities.c,v 1.31 2001/08/15 03:54:53 lukem Exp $");
|
1995-03-18 17:54:19 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1994-04-25 22:28:42 +04:00
|
|
|
#include <sys/time.h>
|
1997-09-16 20:44:43 +04:00
|
|
|
|
1994-06-08 22:57:30 +04:00
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/dir.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
1998-03-18 20:01:23 +03:00
|
|
|
#include <ufs/ffs/ffs_extern.h>
|
1999-11-15 22:18:24 +03:00
|
|
|
#include <ufs/ufs/ufs_bswap.h>
|
1997-09-16 20:44:43 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <ctype.h>
|
1997-09-16 20:44:43 +04:00
|
|
|
#include <err.h>
|
1997-09-20 10:16:23 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1997-09-16 20:44:43 +04:00
|
|
|
#include <string.h>
|
1997-09-20 10:16:23 +04:00
|
|
|
#include <unistd.h>
|
1994-12-05 23:15:31 +03:00
|
|
|
|
1996-09-28 02:45:10 +04:00
|
|
|
#include "fsutil.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "fsck.h"
|
1994-12-05 23:15:31 +03:00
|
|
|
#include "extern.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
long diskreads, totalreads; /* Disk cache statistics */
|
|
|
|
|
1997-09-16 20:44:43 +04:00
|
|
|
static void rwerror __P((char *, ufs_daddr_t));
|
1996-09-23 20:18:31 +04:00
|
|
|
|
2001-02-05 00:25:54 +03:00
|
|
|
extern int returntosingle;
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
ftypeok(dp)
|
|
|
|
struct dinode *dp;
|
|
|
|
{
|
1998-03-18 20:01:23 +03:00
|
|
|
switch (iswap16(dp->di_mode) & IFMT) {
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
case IFDIR:
|
|
|
|
case IFREG:
|
|
|
|
case IFBLK:
|
|
|
|
case IFCHR:
|
|
|
|
case IFLNK:
|
|
|
|
case IFSOCK:
|
|
|
|
case IFIFO:
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (debug)
|
1998-03-18 20:01:23 +03:00
|
|
|
printf("bad file type 0%o\n", iswap16(dp->di_mode));
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
reply(question)
|
|
|
|
char *question;
|
|
|
|
{
|
|
|
|
int persevere;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (preen)
|
|
|
|
pfatal("INTERNAL ERROR: GOT TO reply()");
|
|
|
|
persevere = !strcmp(question, "CONTINUE");
|
|
|
|
printf("\n");
|
|
|
|
if (!persevere && (nflag || fswritefd < 0)) {
|
|
|
|
printf("%s? no\n\n", question);
|
1999-11-15 22:18:24 +03:00
|
|
|
resolved = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (yflag || (persevere && nflag)) {
|
|
|
|
printf("%s? yes\n\n", question);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
printf("%s? [yn] ", question);
|
|
|
|
(void) fflush(stdout);
|
|
|
|
c = getc(stdin);
|
1999-11-15 22:18:24 +03:00
|
|
|
while (c != '\n' && getc(stdin) != '\n') {
|
|
|
|
if (feof(stdin)) {
|
|
|
|
resolved = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
1999-11-15 22:18:24 +03:00
|
|
|
}
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
|
|
|
|
printf("\n");
|
|
|
|
if (c == 'y' || c == 'Y')
|
|
|
|
return (1);
|
1999-11-15 22:18:24 +03:00
|
|
|
resolved = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Malloc buffers and set up cache.
|
|
|
|
*/
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
bufinit()
|
|
|
|
{
|
1997-09-14 18:36:29 +04:00
|
|
|
struct bufarea *bp;
|
1993-03-21 12:45:37 +03:00
|
|
|
long bufcnt, i;
|
|
|
|
char *bufp;
|
|
|
|
|
|
|
|
pbp = pdirbp = (struct bufarea *)0;
|
1998-03-18 20:01:23 +03:00
|
|
|
bufp = malloc((unsigned int)sblock->fs_bsize);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (bufp == 0)
|
1997-09-16 20:44:43 +04:00
|
|
|
errx(EEXIT, "cannot allocate buffer pool");
|
1993-03-21 12:45:37 +03:00
|
|
|
cgblk.b_un.b_buf = bufp;
|
|
|
|
initbarea(&cgblk);
|
|
|
|
bufhead.b_next = bufhead.b_prev = &bufhead;
|
1998-03-18 20:01:23 +03:00
|
|
|
bufcnt = MAXBUFSPACE / sblock->fs_bsize;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (bufcnt < MINBUFS)
|
|
|
|
bufcnt = MINBUFS;
|
|
|
|
for (i = 0; i < bufcnt; i++) {
|
|
|
|
bp = (struct bufarea *)malloc(sizeof(struct bufarea));
|
1998-03-18 20:01:23 +03:00
|
|
|
bufp = malloc((unsigned int)sblock->fs_bsize);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (bp == NULL || bufp == NULL) {
|
|
|
|
if (i >= MINBUFS)
|
|
|
|
break;
|
1997-09-16 20:44:43 +04:00
|
|
|
errx(EEXIT, "cannot allocate buffer pool");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
bp->b_un.b_buf = bufp;
|
|
|
|
bp->b_prev = &bufhead;
|
|
|
|
bp->b_next = bufhead.b_next;
|
|
|
|
bufhead.b_next->b_prev = bp;
|
|
|
|
bufhead.b_next = bp;
|
|
|
|
initbarea(bp);
|
|
|
|
}
|
|
|
|
bufhead.b_size = i; /* save number of buffers */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Manage a cache of directory blocks.
|
|
|
|
*/
|
|
|
|
struct bufarea *
|
|
|
|
getdatablk(blkno, size)
|
1997-09-16 20:44:43 +04:00
|
|
|
ufs_daddr_t blkno;
|
1993-03-21 12:45:37 +03:00
|
|
|
long size;
|
|
|
|
{
|
1997-09-14 18:36:29 +04:00
|
|
|
struct bufarea *bp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
|
1998-03-18 20:01:23 +03:00
|
|
|
if (bp->b_bno == fsbtodb(sblock, blkno))
|
1993-03-21 12:45:37 +03:00
|
|
|
goto foundit;
|
|
|
|
for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
|
|
|
|
if ((bp->b_flags & B_INUSE) == 0)
|
|
|
|
break;
|
|
|
|
if (bp == &bufhead)
|
1997-09-16 20:44:43 +04:00
|
|
|
errx(EEXIT, "deadlocked buffer pool");
|
1993-03-21 12:45:37 +03:00
|
|
|
getblk(bp, blkno, size);
|
|
|
|
/* fall through */
|
|
|
|
foundit:
|
|
|
|
totalreads++;
|
|
|
|
bp->b_prev->b_next = bp->b_next;
|
|
|
|
bp->b_next->b_prev = bp->b_prev;
|
|
|
|
bp->b_prev = &bufhead;
|
|
|
|
bp->b_next = bufhead.b_next;
|
|
|
|
bufhead.b_next->b_prev = bp;
|
|
|
|
bufhead.b_next = bp;
|
|
|
|
bp->b_flags |= B_INUSE;
|
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
getblk(bp, blk, size)
|
1997-09-14 18:36:29 +04:00
|
|
|
struct bufarea *bp;
|
1997-09-16 20:44:43 +04:00
|
|
|
ufs_daddr_t blk;
|
1993-03-21 12:45:37 +03:00
|
|
|
long size;
|
|
|
|
{
|
1997-09-16 20:44:43 +04:00
|
|
|
ufs_daddr_t dblk;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-03-18 20:01:23 +03:00
|
|
|
dblk = fsbtodb(sblock, blk);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (bp->b_bno != dblk) {
|
|
|
|
flush(fswritefd, bp);
|
|
|
|
diskreads++;
|
|
|
|
bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
|
|
|
|
bp->b_bno = dblk;
|
|
|
|
bp->b_size = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
flush(fd, bp)
|
|
|
|
int fd;
|
1997-09-14 18:36:29 +04:00
|
|
|
struct bufarea *bp;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-14 18:36:29 +04:00
|
|
|
int i, j;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (!bp->b_dirty)
|
|
|
|
return;
|
|
|
|
if (bp->b_errs != 0)
|
|
|
|
pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
|
|
|
|
(bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
|
|
|
|
bp->b_bno);
|
|
|
|
bp->b_dirty = 0;
|
|
|
|
bp->b_errs = 0;
|
|
|
|
bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
|
|
|
|
if (bp != &sblk)
|
|
|
|
return;
|
1998-03-18 20:01:23 +03:00
|
|
|
for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
|
|
|
|
int size = sblock->fs_cssize - i < sblock->fs_bsize ?
|
|
|
|
sblock->fs_cssize - i : sblock->fs_bsize;
|
|
|
|
/*
|
|
|
|
* The following routines assumes that struct csum is made of
|
|
|
|
* u_int32_t's
|
|
|
|
*/
|
|
|
|
if (needswap) {
|
|
|
|
int k;
|
2001-01-09 12:25:32 +03:00
|
|
|
u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
|
|
|
|
|
1998-03-18 20:01:23 +03:00
|
|
|
for (k = 0; k < size / sizeof(u_int32_t); k++)
|
|
|
|
cd[k] = bswap32(cd[k]);
|
|
|
|
}
|
|
|
|
bwrite(fswritefd, (char *)sblock->fs_csp[j],
|
|
|
|
fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
|
2001-01-09 12:25:32 +03:00
|
|
|
size);
|
1998-03-18 20:01:23 +03:00
|
|
|
if (needswap) {
|
|
|
|
int k;
|
2001-01-09 12:25:32 +03:00
|
|
|
u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
|
|
|
|
|
1998-03-18 20:01:23 +03:00
|
|
|
for (k = 0; k < size / sizeof(u_int32_t); k++)
|
|
|
|
cd[k] = bswap32(cd[k]);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-09-23 20:18:31 +04:00
|
|
|
static void
|
1993-03-21 12:45:37 +03:00
|
|
|
rwerror(mesg, blk)
|
|
|
|
char *mesg;
|
1997-09-16 20:44:43 +04:00
|
|
|
ufs_daddr_t blk;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
if (preen == 0)
|
|
|
|
printf("\n");
|
1996-09-23 20:18:31 +04:00
|
|
|
pfatal("CANNOT %s: BLK %d", mesg, blk);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (reply("CONTINUE") == 0)
|
1997-09-16 20:44:43 +04:00
|
|
|
exit(EEXIT);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
1998-03-18 20:01:23 +03:00
|
|
|
ckfini()
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-14 18:36:29 +04:00
|
|
|
struct bufarea *bp, *nbp;
|
1997-09-16 20:44:43 +04:00
|
|
|
int ofsmodified, cnt = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-08 22:57:30 +04:00
|
|
|
if (fswritefd < 0) {
|
|
|
|
(void)close(fsreadfd);
|
|
|
|
return;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
flush(fswritefd, &sblk);
|
|
|
|
if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
|
|
|
|
!preen && reply("UPDATE STANDARD SUPERBLOCK")) {
|
|
|
|
sblk.b_bno = SBOFF / dev_bsize;
|
|
|
|
sbdirty();
|
|
|
|
flush(fswritefd, &sblk);
|
|
|
|
}
|
|
|
|
flush(fswritefd, &cgblk);
|
|
|
|
free(cgblk.b_un.b_buf);
|
1994-04-09 12:53:37 +04:00
|
|
|
for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cnt++;
|
|
|
|
flush(fswritefd, bp);
|
|
|
|
nbp = bp->b_prev;
|
|
|
|
free(bp->b_un.b_buf);
|
|
|
|
free((char *)bp);
|
|
|
|
}
|
|
|
|
if (bufhead.b_size != cnt)
|
1997-09-16 20:44:43 +04:00
|
|
|
errx(EEXIT, "Panic: lost %d buffers", bufhead.b_size - cnt);
|
1993-03-21 12:45:37 +03:00
|
|
|
pbp = pdirbp = (struct bufarea *)0;
|
1998-03-18 20:01:23 +03:00
|
|
|
if (markclean && (sblock->fs_clean & FS_ISCLEAN) == 0) {
|
1995-04-13 01:24:07 +04:00
|
|
|
/*
|
|
|
|
* Mark the file system as clean, and sync the superblock.
|
|
|
|
*/
|
|
|
|
if (preen)
|
|
|
|
pwarn("MARKING FILE SYSTEM CLEAN\n");
|
|
|
|
else if (!reply("MARK FILE SYSTEM CLEAN"))
|
|
|
|
markclean = 0;
|
|
|
|
if (markclean) {
|
1998-03-18 20:01:23 +03:00
|
|
|
sblock->fs_clean = FS_ISCLEAN;
|
1995-04-13 01:24:07 +04:00
|
|
|
sbdirty();
|
1997-09-16 20:44:43 +04:00
|
|
|
ofsmodified = fsmodified;
|
1995-04-13 01:24:07 +04:00
|
|
|
flush(fswritefd, &sblk);
|
1997-09-24 13:24:21 +04:00
|
|
|
#if LITE2BORKEN
|
1997-09-16 20:44:43 +04:00
|
|
|
fsmodified = ofsmodified;
|
1997-09-24 13:24:21 +04:00
|
|
|
#endif
|
1997-09-16 20:44:43 +04:00
|
|
|
if (!preen)
|
|
|
|
printf(
|
|
|
|
"\n***** FILE SYSTEM MARKED CLEAN *****\n");
|
1995-04-13 01:24:07 +04:00
|
|
|
}
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("cache missed %ld of %ld (%d%%)\n", diskreads,
|
|
|
|
totalreads, (int)(diskreads * 100 / totalreads));
|
|
|
|
(void)close(fsreadfd);
|
|
|
|
(void)close(fswritefd);
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
bread(fd, buf, blk, size)
|
|
|
|
int fd;
|
|
|
|
char *buf;
|
1997-09-16 20:44:43 +04:00
|
|
|
ufs_daddr_t blk;
|
1993-03-21 12:45:37 +03:00
|
|
|
long size;
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
int i, errs;
|
1994-06-08 22:57:30 +04:00
|
|
|
off_t offset;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-08 22:57:30 +04:00
|
|
|
offset = blk;
|
|
|
|
offset *= dev_bsize;
|
|
|
|
if (lseek(fd, offset, 0) < 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
rwerror("SEEK", blk);
|
|
|
|
else if (read(fd, buf, (int)size) == size)
|
|
|
|
return (0);
|
|
|
|
rwerror("READ", blk);
|
1994-06-08 22:57:30 +04:00
|
|
|
if (lseek(fd, offset, 0) < 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
rwerror("SEEK", blk);
|
|
|
|
errs = 0;
|
1994-09-23 18:26:58 +04:00
|
|
|
memset(buf, 0, (size_t)size);
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
|
|
|
|
for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
|
|
|
|
if (read(fd, cp, (int)secsize) != secsize) {
|
1994-06-08 22:57:30 +04:00
|
|
|
(void)lseek(fd, offset + i + secsize, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (secsize != dev_bsize && dev_bsize != 1)
|
|
|
|
printf(" %ld (%ld),",
|
|
|
|
(blk * dev_bsize + i) / secsize,
|
|
|
|
blk + i / dev_bsize);
|
|
|
|
else
|
|
|
|
printf(" %ld,", blk + i / dev_bsize);
|
|
|
|
errs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return (errs);
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
bwrite(fd, buf, blk, size)
|
|
|
|
int fd;
|
|
|
|
char *buf;
|
1997-09-16 20:44:43 +04:00
|
|
|
ufs_daddr_t blk;
|
1993-03-21 12:45:37 +03:00
|
|
|
long size;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *cp;
|
1994-06-08 22:57:30 +04:00
|
|
|
off_t offset;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
1994-06-08 22:57:30 +04:00
|
|
|
offset = blk;
|
|
|
|
offset *= dev_bsize;
|
|
|
|
if (lseek(fd, offset, 0) < 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
rwerror("SEEK", blk);
|
|
|
|
else if (write(fd, buf, (int)size) == size) {
|
|
|
|
fsmodified = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rwerror("WRITE", blk);
|
1994-06-08 22:57:30 +04:00
|
|
|
if (lseek(fd, offset, 0) < 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
rwerror("SEEK", blk);
|
|
|
|
printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
|
|
|
|
for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
|
|
|
|
if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
|
1994-06-08 22:57:30 +04:00
|
|
|
(void)lseek(fd, offset + i + dev_bsize, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
printf(" %ld,", blk + i / dev_bsize);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate a data block with the specified number of fragments
|
|
|
|
*/
|
1997-09-16 20:44:43 +04:00
|
|
|
ufs_daddr_t
|
1993-03-21 12:45:37 +03:00
|
|
|
allocblk(frags)
|
|
|
|
long frags;
|
|
|
|
{
|
1999-11-15 22:18:24 +03:00
|
|
|
int i, j, k, cg, baseblk;
|
|
|
|
struct cg *cgp = cgrp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-03-18 20:01:23 +03:00
|
|
|
if (frags <= 0 || frags > sblock->fs_frag)
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
1998-03-18 20:01:23 +03:00
|
|
|
for (i = 0; i < maxfsblock - sblock->fs_frag; i += sblock->fs_frag) {
|
|
|
|
for (j = 0; j <= sblock->fs_frag - frags; j++) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (testbmap(i + j))
|
|
|
|
continue;
|
|
|
|
for (k = 1; k < frags; k++)
|
|
|
|
if (testbmap(i + j + k))
|
|
|
|
break;
|
|
|
|
if (k < frags) {
|
|
|
|
j += k;
|
|
|
|
continue;
|
|
|
|
}
|
1999-11-15 22:18:24 +03:00
|
|
|
cg = dtog(sblock, i + j);
|
|
|
|
getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
|
|
|
|
memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
|
|
|
|
if ((doswap && !needswap) || (!doswap && needswap))
|
|
|
|
swap_cg(cgblk.b_un.b_cg, cgp);
|
|
|
|
if (!cg_chkmagic(cgp, 0))
|
|
|
|
pfatal("CG %d: ALLOCBLK: BAD MAGIC NUMBER\n",
|
|
|
|
cg);
|
|
|
|
baseblk = dtogd(sblock, i + j);
|
|
|
|
for (k = 0; k < frags; k++) {
|
1993-03-21 12:45:37 +03:00
|
|
|
setbmap(i + j + k);
|
1999-11-15 22:18:24 +03:00
|
|
|
clrbit(cg_blksfree(cgp, 0), baseblk + k);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
n_blks += frags;
|
1999-11-15 22:18:24 +03:00
|
|
|
if (frags == sblock->fs_frag)
|
|
|
|
cgp->cg_cs.cs_nbfree--;
|
|
|
|
else
|
|
|
|
cgp->cg_cs.cs_nffree -= frags;
|
|
|
|
cgdirty();
|
1993-03-21 12:45:37 +03:00
|
|
|
return (i + j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a previously allocated block
|
|
|
|
*/
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
freeblk(blkno, frags)
|
1997-09-16 20:44:43 +04:00
|
|
|
ufs_daddr_t blkno;
|
1993-03-21 12:45:37 +03:00
|
|
|
long frags;
|
|
|
|
{
|
|
|
|
struct inodesc idesc;
|
|
|
|
|
|
|
|
idesc.id_blkno = blkno;
|
|
|
|
idesc.id_numfrags = frags;
|
|
|
|
(void)pass4check(&idesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a pathname
|
|
|
|
*/
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
getpathname(namebuf, curdir, ino)
|
|
|
|
char *namebuf;
|
|
|
|
ino_t curdir, ino;
|
|
|
|
{
|
|
|
|
int len;
|
1997-09-14 18:36:29 +04:00
|
|
|
char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct inodesc idesc;
|
|
|
|
static int busy = 0;
|
|
|
|
|
1994-06-08 22:57:30 +04:00
|
|
|
if (curdir == ino && ino == ROOTINO) {
|
|
|
|
(void)strcpy(namebuf, "/");
|
|
|
|
return;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (busy ||
|
|
|
|
(statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
|
|
|
|
(void)strcpy(namebuf, "?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
busy = 1;
|
1994-09-23 18:26:58 +04:00
|
|
|
memset(&idesc, 0, sizeof(struct inodesc));
|
1993-03-21 12:45:37 +03:00
|
|
|
idesc.id_type = DATA;
|
|
|
|
idesc.id_fix = IGNORE;
|
|
|
|
cp = &namebuf[MAXPATHLEN - 1];
|
|
|
|
*cp = '\0';
|
|
|
|
if (curdir != ino) {
|
|
|
|
idesc.id_parent = curdir;
|
|
|
|
goto namelookup;
|
|
|
|
}
|
|
|
|
while (ino != ROOTINO) {
|
|
|
|
idesc.id_number = ino;
|
|
|
|
idesc.id_func = findino;
|
|
|
|
idesc.id_name = "..";
|
|
|
|
if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
|
|
|
|
break;
|
|
|
|
namelookup:
|
|
|
|
idesc.id_number = idesc.id_parent;
|
|
|
|
idesc.id_parent = ino;
|
|
|
|
idesc.id_func = findname;
|
|
|
|
idesc.id_name = namebuf;
|
|
|
|
if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
|
|
|
|
break;
|
|
|
|
len = strlen(namebuf);
|
|
|
|
cp -= len;
|
1997-09-16 20:44:43 +04:00
|
|
|
memmove(cp, namebuf, (size_t)len);
|
1993-03-21 12:45:37 +03:00
|
|
|
*--cp = '/';
|
|
|
|
if (cp < &namebuf[MAXNAMLEN])
|
|
|
|
break;
|
|
|
|
ino = idesc.id_number;
|
|
|
|
}
|
|
|
|
busy = 0;
|
|
|
|
if (ino != ROOTINO)
|
|
|
|
*--cp = '?';
|
1997-09-16 20:44:43 +04:00
|
|
|
memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-09-16 20:44:43 +04:00
|
|
|
catch(sig)
|
|
|
|
int sig;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-03-18 20:01:23 +03:00
|
|
|
if (!doinglevel2) {
|
|
|
|
markclean = 0;
|
|
|
|
ckfini();
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(12);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When preening, allow a single quit to signal
|
|
|
|
* a special exit after filesystem checks complete
|
|
|
|
* so that reboot sequence may be interrupted.
|
|
|
|
*/
|
|
|
|
void
|
1997-09-16 20:44:43 +04:00
|
|
|
catchquit(sig)
|
|
|
|
int sig;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-08-15 07:54:53 +04:00
|
|
|
printf("returning to single-user after file system check\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
returntosingle = 1;
|
|
|
|
(void)signal(SIGQUIT, SIG_DFL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ignore a single quit signal; wait and flush just in case.
|
|
|
|
* Used by child processes in preen.
|
|
|
|
*/
|
|
|
|
void
|
1997-09-16 20:44:43 +04:00
|
|
|
voidquit(sig)
|
|
|
|
int sig;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
(void)signal(SIGQUIT, SIG_IGN);
|
|
|
|
(void)signal(SIGQUIT, SIG_DFL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* determine whether an inode should be fixed.
|
|
|
|
*/
|
1994-12-05 23:15:31 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
dofix(idesc, msg)
|
1997-09-14 18:36:29 +04:00
|
|
|
struct inodesc *idesc;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *msg;
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (idesc->id_fix) {
|
|
|
|
|
|
|
|
case DONTKNOW:
|
|
|
|
if (idesc->id_type == DATA)
|
|
|
|
direrror(idesc->id_number, msg);
|
|
|
|
else
|
2000-10-11 00:24:49 +04:00
|
|
|
pwarn("%s", msg);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (preen) {
|
|
|
|
printf(" (SALVAGED)\n");
|
|
|
|
idesc->id_fix = FIX;
|
|
|
|
return (ALTERED);
|
|
|
|
}
|
|
|
|
if (reply("SALVAGE") == 0) {
|
|
|
|
idesc->id_fix = NOFIX;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
idesc->id_fix = FIX;
|
|
|
|
return (ALTERED);
|
|
|
|
|
|
|
|
case FIX:
|
|
|
|
return (ALTERED);
|
|
|
|
|
|
|
|
case NOFIX:
|
|
|
|
case IGNORE:
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
default:
|
1997-09-16 20:44:43 +04:00
|
|
|
errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
1997-09-16 20:44:43 +04:00
|
|
|
return (0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1998-03-18 20:01:23 +03:00
|
|
|
|
1998-05-06 06:45:09 +04:00
|
|
|
void
|
|
|
|
copyback_cg(blk)
|
1998-03-18 20:01:23 +03:00
|
|
|
struct bufarea *blk;
|
|
|
|
{
|
1998-05-06 06:45:09 +04:00
|
|
|
|
|
|
|
memcpy(blk->b_un.b_cg, cgrp, sblock->fs_cgsize);
|
1998-03-18 20:01:23 +03:00
|
|
|
if (needswap)
|
|
|
|
swap_cg(cgrp, blk->b_un.b_cg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
swap_cg(o, n)
|
|
|
|
struct cg *o, *n;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u_int32_t *n32, *o32;
|
|
|
|
u_int16_t *n16, *o16;
|
|
|
|
|
|
|
|
n->cg_firstfield = bswap32(o->cg_firstfield);
|
|
|
|
n->cg_magic = bswap32(o->cg_magic);
|
|
|
|
n->cg_time = bswap32(o->cg_time);
|
|
|
|
n->cg_cgx = bswap32(o->cg_cgx);
|
|
|
|
n->cg_ncyl = bswap16(o->cg_ncyl);
|
|
|
|
n->cg_niblk = bswap16(o->cg_niblk);
|
|
|
|
n->cg_ndblk = bswap32(o->cg_ndblk);
|
|
|
|
n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
|
|
|
|
n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
|
|
|
|
n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
|
|
|
|
n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
|
|
|
|
n->cg_rotor = bswap32(o->cg_rotor);
|
|
|
|
n->cg_frotor = bswap32(o->cg_frotor);
|
|
|
|
n->cg_irotor = bswap32(o->cg_irotor);
|
|
|
|
n->cg_btotoff = bswap32(o->cg_btotoff);
|
|
|
|
n->cg_boff = bswap32(o->cg_boff);
|
|
|
|
n->cg_iusedoff = bswap32(o->cg_iusedoff);
|
|
|
|
n->cg_freeoff = bswap32(o->cg_freeoff);
|
|
|
|
n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
|
|
|
|
n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
|
|
|
|
n->cg_clusteroff = bswap32(o->cg_clusteroff);
|
|
|
|
n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
|
|
|
|
for (i=0; i < MAXFRAG; i++)
|
|
|
|
n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
|
|
|
|
|
|
|
|
if (sblock->fs_postblformat == FS_42POSTBLFMT) { /* old format */
|
|
|
|
struct ocg *on, *oo;
|
|
|
|
int j;
|
|
|
|
on = (struct ocg *)n;
|
|
|
|
oo = (struct ocg *)o;
|
|
|
|
for(i = 0; i < 8; i++) {
|
|
|
|
on->cg_frsum[i] = bswap32(oo->cg_frsum[i]);
|
|
|
|
}
|
|
|
|
for(i = 0; i < 32; i++) {
|
|
|
|
on->cg_btot[i] = bswap32(oo->cg_btot[i]);
|
|
|
|
for (j = 0; j < 8; j++)
|
|
|
|
on->cg_b[i][j] = bswap16(oo->cg_b[i][j]);
|
|
|
|
}
|
|
|
|
memmove(on->cg_iused, oo->cg_iused, 256);
|
|
|
|
on->cg_magic = bswap32(oo->cg_magic);
|
|
|
|
} else { /* new format */
|
|
|
|
if (n->cg_magic == CG_MAGIC) {
|
|
|
|
n32 = (u_int32_t*)((u_int8_t*)n + n->cg_btotoff);
|
|
|
|
o32 = (u_int32_t*)((u_int8_t*)o + n->cg_btotoff);
|
|
|
|
n16 = (u_int16_t*)((u_int8_t*)n + n->cg_boff);
|
|
|
|
o16 = (u_int16_t*)((u_int8_t*)o + n->cg_boff);
|
|
|
|
} else {
|
|
|
|
n32 = (u_int32_t*)((u_int8_t*)n + o->cg_btotoff);
|
|
|
|
o32 = (u_int32_t*)((u_int8_t*)o + o->cg_btotoff);
|
|
|
|
n16 = (u_int16_t*)((u_int8_t*)n + o->cg_boff);
|
|
|
|
o16 = (u_int16_t*)((u_int8_t*)o + o->cg_boff);
|
|
|
|
}
|
|
|
|
for (i=0; i < sblock->fs_cpg; i++)
|
|
|
|
n32[i] = bswap32(o32[i]);
|
|
|
|
|
|
|
|
for (i=0; i < sblock->fs_cpg * sblock->fs_nrpos; i++)
|
|
|
|
n16[i] = bswap16(o16[i]);
|
|
|
|
|
|
|
|
if (n->cg_magic == CG_MAGIC) {
|
|
|
|
n32 = (u_int32_t*)((u_int8_t*)n + n->cg_clustersumoff);
|
|
|
|
o32 = (u_int32_t*)((u_int8_t*)o + n->cg_clustersumoff);
|
|
|
|
} else {
|
|
|
|
n32 = (u_int32_t*)((u_int8_t*)n + o->cg_clustersumoff);
|
|
|
|
o32 = (u_int32_t*)((u_int8_t*)o + o->cg_clustersumoff);
|
|
|
|
}
|
2001-01-09 12:08:35 +03:00
|
|
|
for (i = 1; i < sblock->fs_contigsumsize + 1; i++)
|
1998-03-18 20:01:23 +03:00
|
|
|
n32[i] = bswap32(o32[i]);
|
|
|
|
}
|
|
|
|
}
|