1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1980, 1986, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-12-03 02:41:11 +00:00
|
|
|
#if 0
|
1997-03-11 12:20:21 +00:00
|
|
|
static const char sccsid[] = "@(#)utilities.c 8.6 (Berkeley) 5/19/95";
|
1998-12-03 02:41:11 +00:00
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1997-03-11 12:20:21 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/dir.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
1997-03-12 17:01:11 +00:00
|
|
|
|
1997-03-11 12:20:21 +00:00
|
|
|
#include <err.h>
|
1997-03-12 17:01:11 +00:00
|
|
|
#include <string.h>
|
1997-03-11 12:20:21 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#include "fsck.h"
|
|
|
|
|
|
|
|
long diskreads, totalreads; /* Disk cache statistics */
|
|
|
|
|
1997-03-11 12:20:21 +00:00
|
|
|
static void rwerror __P((char *mesg, ufs_daddr_t blk));
|
1995-04-02 14:52:29 +00:00
|
|
|
|
|
|
|
int
|
1994-05-26 06:35:07 +00:00
|
|
|
ftypeok(dp)
|
|
|
|
struct dinode *dp;
|
|
|
|
{
|
|
|
|
switch (dp->di_mode & IFMT) {
|
|
|
|
|
|
|
|
case IFDIR:
|
|
|
|
case IFREG:
|
|
|
|
case IFBLK:
|
|
|
|
case IFCHR:
|
|
|
|
case IFLNK:
|
|
|
|
case IFSOCK:
|
|
|
|
case IFIFO:
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (debug)
|
|
|
|
printf("bad file type 0%o\n", dp->di_mode);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-04-02 14:52:29 +00:00
|
|
|
int
|
1994-05-26 06:35:07 +00: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);
|
1998-03-08 09:59:44 +00:00
|
|
|
resolved = 0;
|
1994-05-26 06:35:07 +00: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);
|
1998-03-08 09:59:44 +00:00
|
|
|
while (c != '\n' && getc(stdin) != '\n') {
|
|
|
|
if (feof(stdin)) {
|
|
|
|
resolved = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
return (0);
|
1998-03-08 09:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
|
|
|
|
printf("\n");
|
|
|
|
if (c == 'y' || c == 'Y')
|
|
|
|
return (1);
|
1998-03-08 09:59:44 +00:00
|
|
|
resolved = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1998-12-03 02:27:35 +00:00
|
|
|
/*
|
|
|
|
* Look up state information for an inode.
|
|
|
|
*/
|
|
|
|
struct inostat *
|
|
|
|
inoinfo(inum)
|
|
|
|
ino_t inum;
|
|
|
|
{
|
|
|
|
static struct inostat unallocated = { USTATE, 0, 0 };
|
|
|
|
struct inostatlist *ilp;
|
|
|
|
int iloff;
|
|
|
|
|
|
|
|
if (inum > maxino)
|
|
|
|
errx(EEXIT, "inoinfo: inumber %d out of range", inum);
|
|
|
|
ilp = &inostathead[inum / sblock.fs_ipg];
|
|
|
|
iloff = inum % sblock.fs_ipg;
|
|
|
|
if (iloff >= ilp->il_numalloced)
|
|
|
|
return (&unallocated);
|
|
|
|
return (&ilp->il_stat[iloff]);
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Malloc buffers and set up cache.
|
|
|
|
*/
|
1995-04-02 14:52:29 +00:00
|
|
|
void
|
1994-05-26 06:35:07 +00:00
|
|
|
bufinit()
|
|
|
|
{
|
|
|
|
register struct bufarea *bp;
|
|
|
|
long bufcnt, i;
|
|
|
|
char *bufp;
|
|
|
|
|
|
|
|
pbp = pdirbp = (struct bufarea *)0;
|
|
|
|
bufp = malloc((unsigned int)sblock.fs_bsize);
|
|
|
|
if (bufp == 0)
|
1997-03-11 12:20:21 +00:00
|
|
|
errx(EEXIT, "cannot allocate buffer pool");
|
1994-05-26 06:35:07 +00:00
|
|
|
cgblk.b_un.b_buf = bufp;
|
|
|
|
initbarea(&cgblk);
|
|
|
|
bufhead.b_next = bufhead.b_prev = &bufhead;
|
|
|
|
bufcnt = MAXBUFSPACE / sblock.fs_bsize;
|
|
|
|
if (bufcnt < MINBUFS)
|
|
|
|
bufcnt = MINBUFS;
|
|
|
|
for (i = 0; i < bufcnt; i++) {
|
|
|
|
bp = (struct bufarea *)malloc(sizeof(struct bufarea));
|
|
|
|
bufp = malloc((unsigned int)sblock.fs_bsize);
|
|
|
|
if (bp == NULL || bufp == NULL) {
|
|
|
|
if (i >= MINBUFS)
|
|
|
|
break;
|
1997-03-11 12:20:21 +00:00
|
|
|
errx(EEXIT, "cannot allocate buffer pool");
|
1994-05-26 06:35:07 +00: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-03-11 12:20:21 +00:00
|
|
|
ufs_daddr_t blkno;
|
1994-05-26 06:35:07 +00:00
|
|
|
long size;
|
|
|
|
{
|
|
|
|
register struct bufarea *bp;
|
|
|
|
|
|
|
|
for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
|
|
|
|
if (bp->b_bno == fsbtodb(&sblock, blkno))
|
|
|
|
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-03-11 12:20:21 +00:00
|
|
|
errx(EEXIT, "deadlocked buffer pool");
|
1994-05-26 06:35:07 +00: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)
|
|
|
|
register struct bufarea *bp;
|
1997-03-11 12:20:21 +00:00
|
|
|
ufs_daddr_t blk;
|
1994-05-26 06:35:07 +00:00
|
|
|
long size;
|
|
|
|
{
|
1997-03-11 12:20:21 +00:00
|
|
|
ufs_daddr_t dblk;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
dblk = fsbtodb(&sblock, blk);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-04-02 14:52:29 +00:00
|
|
|
void
|
1994-05-26 06:35:07 +00:00
|
|
|
flush(fd, bp)
|
|
|
|
int fd;
|
|
|
|
register struct bufarea *bp;
|
|
|
|
{
|
|
|
|
register int i, j;
|
|
|
|
|
|
|
|
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;
|
|
|
|
for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
|
|
|
|
bwrite(fswritefd, (char *)sblock.fs_csp[j],
|
|
|
|
fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
|
|
|
|
sblock.fs_cssize - i < sblock.fs_bsize ?
|
|
|
|
sblock.fs_cssize - i : sblock.fs_bsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-03-11 12:20:21 +00:00
|
|
|
static void
|
1994-05-26 06:35:07 +00:00
|
|
|
rwerror(mesg, blk)
|
|
|
|
char *mesg;
|
1997-03-11 12:20:21 +00:00
|
|
|
ufs_daddr_t blk;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (preen == 0)
|
|
|
|
printf("\n");
|
|
|
|
pfatal("CANNOT %s: BLK %ld", mesg, blk);
|
|
|
|
if (reply("CONTINUE") == 0)
|
1997-03-11 12:20:21 +00:00
|
|
|
exit(EEXIT);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
1995-04-02 14:52:29 +00:00
|
|
|
void
|
1997-03-11 12:20:21 +00:00
|
|
|
ckfini(markclean)
|
|
|
|
int markclean;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
register struct bufarea *bp, *nbp;
|
1997-03-11 12:20:21 +00:00
|
|
|
int ofsmodified, cnt = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
if (fswritefd < 0) {
|
|
|
|
(void)close(fsreadfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
|
|
|
|
cnt++;
|
|
|
|
flush(fswritefd, bp);
|
|
|
|
nbp = bp->b_prev;
|
|
|
|
free(bp->b_un.b_buf);
|
|
|
|
free((char *)bp);
|
|
|
|
}
|
|
|
|
if (bufhead.b_size != cnt)
|
1998-12-03 02:41:11 +00:00
|
|
|
errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
|
1994-05-26 06:35:07 +00:00
|
|
|
pbp = pdirbp = (struct bufarea *)0;
|
1998-12-03 02:27:35 +00:00
|
|
|
if (sblock.fs_clean != markclean) {
|
|
|
|
sblock.fs_clean = markclean;
|
1997-03-11 12:20:21 +00:00
|
|
|
sbdirty();
|
|
|
|
ofsmodified = fsmodified;
|
|
|
|
flush(fswritefd, &sblk);
|
|
|
|
fsmodified = ofsmodified;
|
1998-12-03 02:27:35 +00:00
|
|
|
if (!preen) {
|
|
|
|
printf("\n***** FILE SYSTEM MARKED %s *****\n",
|
|
|
|
markclean ? "CLEAN" : "DIRTY");
|
|
|
|
if (!markclean)
|
|
|
|
rerun = 1;
|
|
|
|
}
|
|
|
|
} else if (!preen && !markclean) {
|
|
|
|
printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
|
|
|
|
rerun = 1;
|
1997-03-11 12:20:21 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
if (debug)
|
|
|
|
printf("cache missed %ld of %ld (%d%%)\n", diskreads,
|
|
|
|
totalreads, (int)(diskreads * 100 / totalreads));
|
|
|
|
(void)close(fsreadfd);
|
|
|
|
(void)close(fswritefd);
|
|
|
|
}
|
|
|
|
|
1995-04-02 14:52:29 +00:00
|
|
|
int
|
1994-05-26 06:35:07 +00:00
|
|
|
bread(fd, buf, blk, size)
|
|
|
|
int fd;
|
|
|
|
char *buf;
|
1997-03-11 12:20:21 +00:00
|
|
|
ufs_daddr_t blk;
|
1994-05-26 06:35:07 +00:00
|
|
|
long size;
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
int i, errs;
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
offset = blk;
|
|
|
|
offset *= dev_bsize;
|
|
|
|
if (lseek(fd, offset, 0) < 0)
|
|
|
|
rwerror("SEEK", blk);
|
|
|
|
else if (read(fd, buf, (int)size) == size)
|
|
|
|
return (0);
|
|
|
|
rwerror("READ", blk);
|
|
|
|
if (lseek(fd, offset, 0) < 0)
|
|
|
|
rwerror("SEEK", blk);
|
|
|
|
errs = 0;
|
1997-03-11 12:20:21 +00:00
|
|
|
memset(buf, 0, (size_t)size);
|
1994-05-26 06:35:07 +00: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) {
|
|
|
|
(void)lseek(fd, offset + i + secsize, 0);
|
|
|
|
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");
|
1998-12-03 02:27:35 +00:00
|
|
|
if (errs)
|
|
|
|
resolved = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
return (errs);
|
|
|
|
}
|
|
|
|
|
1995-04-02 14:52:29 +00:00
|
|
|
void
|
1994-05-26 06:35:07 +00:00
|
|
|
bwrite(fd, buf, blk, size)
|
|
|
|
int fd;
|
|
|
|
char *buf;
|
1997-03-11 12:20:21 +00:00
|
|
|
ufs_daddr_t blk;
|
1994-05-26 06:35:07 +00:00
|
|
|
long size;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *cp;
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
offset = blk;
|
|
|
|
offset *= dev_bsize;
|
|
|
|
if (lseek(fd, offset, 0) < 0)
|
|
|
|
rwerror("SEEK", blk);
|
|
|
|
else if (write(fd, buf, (int)size) == size) {
|
|
|
|
fsmodified = 1;
|
|
|
|
return;
|
|
|
|
}
|
1998-12-03 02:27:35 +00:00
|
|
|
resolved = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
rwerror("WRITE", blk);
|
|
|
|
if (lseek(fd, offset, 0) < 0)
|
|
|
|
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) {
|
|
|
|
(void)lseek(fd, offset + i + dev_bsize, 0);
|
|
|
|
printf(" %ld,", blk + i / dev_bsize);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate a data block with the specified number of fragments
|
|
|
|
*/
|
1997-03-11 12:20:21 +00:00
|
|
|
ufs_daddr_t
|
1994-05-26 06:35:07 +00:00
|
|
|
allocblk(frags)
|
|
|
|
long frags;
|
|
|
|
{
|
1998-03-08 09:59:44 +00:00
|
|
|
int i, j, k, cg, baseblk;
|
|
|
|
struct cg *cgp = &cgrp;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
if (frags <= 0 || frags > sblock.fs_frag)
|
|
|
|
return (0);
|
|
|
|
for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
|
|
|
|
for (j = 0; j <= sblock.fs_frag - frags; j++) {
|
|
|
|
if (testbmap(i + j))
|
|
|
|
continue;
|
|
|
|
for (k = 1; k < frags; k++)
|
|
|
|
if (testbmap(i + j + k))
|
|
|
|
break;
|
|
|
|
if (k < frags) {
|
|
|
|
j += k;
|
|
|
|
continue;
|
|
|
|
}
|
1998-03-08 09:59:44 +00:00
|
|
|
cg = dtog(&sblock, i + j);
|
|
|
|
getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
|
|
|
|
if (!cg_chkmagic(cgp))
|
|
|
|
pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
|
|
|
|
baseblk = dtogd(&sblock, i + j);
|
|
|
|
for (k = 0; k < frags; k++) {
|
1994-05-26 06:35:07 +00:00
|
|
|
setbmap(i + j + k);
|
1998-03-08 09:59:44 +00:00
|
|
|
clrbit(cg_blksfree(cgp), baseblk + k);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
n_blks += frags;
|
1998-03-08 09:59:44 +00:00
|
|
|
if (frags == sblock.fs_frag)
|
|
|
|
cgp->cg_cs.cs_nbfree--;
|
|
|
|
else
|
|
|
|
cgp->cg_cs.cs_nffree -= frags;
|
|
|
|
cgdirty();
|
1994-05-26 06:35:07 +00:00
|
|
|
return (i + j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a previously allocated block
|
|
|
|
*/
|
1995-04-02 14:52:29 +00:00
|
|
|
void
|
1994-05-26 06:35:07 +00:00
|
|
|
freeblk(blkno, frags)
|
1997-03-11 12:20:21 +00:00
|
|
|
ufs_daddr_t blkno;
|
1994-05-26 06:35:07 +00:00
|
|
|
long frags;
|
|
|
|
{
|
|
|
|
struct inodesc idesc;
|
|
|
|
|
|
|
|
idesc.id_blkno = blkno;
|
|
|
|
idesc.id_numfrags = frags;
|
|
|
|
(void)pass4check(&idesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a pathname
|
|
|
|
*/
|
1995-04-02 14:52:29 +00:00
|
|
|
void
|
1994-05-26 06:35:07 +00:00
|
|
|
getpathname(namebuf, curdir, ino)
|
|
|
|
char *namebuf;
|
|
|
|
ino_t curdir, ino;
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
register char *cp;
|
|
|
|
struct inodesc idesc;
|
|
|
|
static int busy = 0;
|
|
|
|
|
|
|
|
if (curdir == ino && ino == ROOTINO) {
|
|
|
|
(void)strcpy(namebuf, "/");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (busy ||
|
1998-12-03 02:27:35 +00:00
|
|
|
(inoinfo(curdir)->ino_state != DSTATE &&
|
|
|
|
inoinfo(curdir)->ino_state != DFOUND)) {
|
1994-05-26 06:35:07 +00:00
|
|
|
(void)strcpy(namebuf, "?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
busy = 1;
|
1997-03-11 12:20:21 +00:00
|
|
|
memset(&idesc, 0, sizeof(struct inodesc));
|
1994-05-26 06:35:07 +00: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-03-11 12:20:21 +00:00
|
|
|
memmove(cp, namebuf, (size_t)len);
|
1994-05-26 06:35:07 +00:00
|
|
|
*--cp = '/';
|
|
|
|
if (cp < &namebuf[MAXNAMLEN])
|
|
|
|
break;
|
|
|
|
ino = idesc.id_number;
|
|
|
|
}
|
|
|
|
busy = 0;
|
|
|
|
if (ino != ROOTINO)
|
|
|
|
*--cp = '?';
|
1997-03-11 12:20:21 +00:00
|
|
|
memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-03-11 12:20:21 +00:00
|
|
|
catch(sig)
|
|
|
|
int sig;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
if (!doinglevel2)
|
1997-03-11 12:20:21 +00:00
|
|
|
ckfini(0);
|
1994-05-26 06:35:07 +00: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-03-11 12:20:21 +00:00
|
|
|
catchquit(sig)
|
|
|
|
int sig;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
printf("returning to single-user after filesystem check\n");
|
|
|
|
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-03-11 12:20:21 +00:00
|
|
|
voidquit(sig)
|
|
|
|
int sig;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
(void)signal(SIGQUIT, SIG_IGN);
|
|
|
|
(void)signal(SIGQUIT, SIG_DFL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* determine whether an inode should be fixed.
|
|
|
|
*/
|
1995-04-02 14:52:29 +00:00
|
|
|
int
|
1994-05-26 06:35:07 +00:00
|
|
|
dofix(idesc, msg)
|
|
|
|
register struct inodesc *idesc;
|
|
|
|
char *msg;
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (idesc->id_fix) {
|
|
|
|
|
|
|
|
case DONTKNOW:
|
|
|
|
if (idesc->id_type == DATA)
|
|
|
|
direrror(idesc->id_number, msg);
|
|
|
|
else
|
2000-10-06 23:24:45 +00:00
|
|
|
pwarn("%s", msg);
|
1994-05-26 06:35:07 +00: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-03-11 12:20:21 +00:00
|
|
|
errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
1997-03-11 12:20:21 +00:00
|
|
|
return (0);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
1997-03-11 12:20:21 +00:00
|
|
|
#if __STDC__
|
|
|
|
#include <stdarg.h>
|
|
|
|
#else
|
|
|
|
#include <varargs.h>
|
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* An unexpected inconsistency occured.
|
1998-03-08 09:59:44 +00:00
|
|
|
* Die if preening or filesystem is running with soft dependency protocol,
|
|
|
|
* otherwise just print message and continue.
|
1994-05-26 06:35:07 +00:00
|
|
|
*/
|
1995-04-02 14:52:29 +00:00
|
|
|
void
|
1997-03-11 12:20:21 +00:00
|
|
|
#if __STDC__
|
|
|
|
pfatal(const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
pfatal(fmt, va_alist)
|
|
|
|
char *fmt;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1995-04-02 14:52:29 +00:00
|
|
|
va_list ap;
|
1997-03-11 12:20:21 +00:00
|
|
|
#if __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
va_start(ap);
|
|
|
|
#endif
|
|
|
|
if (!preen) {
|
|
|
|
(void)vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
1998-03-08 09:59:44 +00:00
|
|
|
if (usedsoftdep)
|
|
|
|
(void)fprintf(stderr,
|
1998-12-03 02:27:35 +00:00
|
|
|
"\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
|
1997-03-11 12:20:21 +00:00
|
|
|
return;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1998-12-03 02:27:35 +00:00
|
|
|
if (cdevname == NULL)
|
|
|
|
cdevname = "fsck";
|
1997-03-11 12:20:21 +00:00
|
|
|
(void)fprintf(stderr, "%s: ", cdevname);
|
|
|
|
(void)vfprintf(stderr, fmt, ap);
|
|
|
|
(void)fprintf(stderr,
|
1998-03-08 09:59:44 +00:00
|
|
|
"\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
|
1998-12-03 02:27:35 +00:00
|
|
|
cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
|
1998-03-08 09:59:44 +00:00
|
|
|
ckfini(0);
|
1997-03-11 12:20:21 +00:00
|
|
|
exit(EEXIT);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-03-08 09:59:44 +00:00
|
|
|
* Pwarn just prints a message when not preening or running soft dependency
|
|
|
|
* protocol, or a warning (preceded by filename) when preening.
|
1994-05-26 06:35:07 +00:00
|
|
|
*/
|
1995-04-02 14:52:29 +00:00
|
|
|
void
|
1997-03-11 12:20:21 +00:00
|
|
|
#if __STDC__
|
|
|
|
pwarn(const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
pwarn(fmt, va_alist)
|
|
|
|
char *fmt;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1995-04-02 14:52:29 +00:00
|
|
|
va_list ap;
|
1997-03-11 12:20:21 +00:00
|
|
|
#if __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
va_start(ap);
|
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
if (preen)
|
1997-03-11 12:20:21 +00:00
|
|
|
(void)fprintf(stderr, "%s: ", cdevname);
|
|
|
|
(void)vfprintf(stderr, fmt, ap);
|
1995-04-02 14:52:29 +00:00
|
|
|
va_end(ap);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stub for routines from kernel.
|
|
|
|
*/
|
1996-09-14 03:00:32 +00:00
|
|
|
void
|
1997-03-11 12:20:21 +00:00
|
|
|
#if __STDC__
|
1995-04-02 14:52:29 +00:00
|
|
|
panic(const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
panic(fmt, va_alist)
|
|
|
|
char *fmt;
|
1997-03-11 12:20:21 +00:00
|
|
|
va_dcl
|
1995-04-02 14:52:29 +00:00
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1997-03-11 12:20:21 +00:00
|
|
|
va_list ap;
|
|
|
|
#if __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
va_start(ap);
|
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
pfatal("INTERNAL INCONSISTENCY:");
|
1997-03-11 12:20:21 +00:00
|
|
|
(void)vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
exit(EEXIT);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|