2000-10-09 09:21:04 +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.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
2003-05-03 18:41:59 +00:00
|
|
|
#ifndef lint
|
2000-10-09 09:21:04 +00:00
|
|
|
static const char sccsid[] = "@(#)utilities.c 8.6 (Berkeley) 5/19/95";
|
|
|
|
#endif /* not lint */
|
2003-05-03 18:41:59 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
2000-10-09 09:21:04 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2002-12-07 21:40:31 +00:00
|
|
|
#include <sys/time.h>
|
2000-10-09 09:21:04 +00:00
|
|
|
#include <sys/types.h>
|
2001-11-17 23:48:21 +00:00
|
|
|
#include <sys/sysctl.h>
|
2002-06-21 06:18:05 +00:00
|
|
|
#include <sys/disklabel.h>
|
2000-10-09 09:21:04 +00:00
|
|
|
#include <sys/stat.h>
|
2002-05-12 20:49:41 +00:00
|
|
|
#include <sys/disklabel.h>
|
2000-10-09 09:21:04 +00:00
|
|
|
|
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/dir.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fstab.h>
|
2002-07-31 12:01:14 +00:00
|
|
|
#include <stdint.h>
|
2000-10-09 09:21:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "fsck.h"
|
|
|
|
|
2002-12-07 21:40:31 +00:00
|
|
|
static void slowio_start(void);
|
|
|
|
static void slowio_end(void);
|
|
|
|
|
2000-10-09 09:21:04 +00:00
|
|
|
long diskreads, totalreads; /* Disk cache statistics */
|
2002-12-07 21:40:31 +00:00
|
|
|
struct timeval slowio_starttime;
|
|
|
|
int slowio_delay_usec = 10000; /* Initial IO delay for background fsck */
|
|
|
|
int slowio_pollcnt;
|
2000-10-09 09:21:04 +00:00
|
|
|
|
|
|
|
int
|
2002-06-21 06:18:05 +00:00
|
|
|
ftypeok(union dinode *dp)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
2002-06-21 06:18:05 +00:00
|
|
|
switch (DIP(dp, di_mode) & IFMT) {
|
2000-10-09 09:21:04 +00:00
|
|
|
|
|
|
|
case IFDIR:
|
|
|
|
case IFREG:
|
|
|
|
case IFBLK:
|
|
|
|
case IFCHR:
|
|
|
|
case IFLNK:
|
|
|
|
case IFSOCK:
|
|
|
|
case IFIFO:
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (debug)
|
2002-06-21 06:18:05 +00:00
|
|
|
printf("bad file type 0%o\n", DIP(dp, di_mode));
|
2000-10-09 09:21:04 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-07-30 13:01:25 +00:00
|
|
|
reply(const char *question)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
|
|
|
int persevere;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (preen)
|
|
|
|
pfatal("INTERNAL ERROR: GOT TO reply()");
|
|
|
|
persevere = !strcmp(question, "CONTINUE");
|
|
|
|
printf("\n");
|
2001-03-21 09:48:03 +00:00
|
|
|
if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
|
2000-10-09 09:21:04 +00:00
|
|
|
printf("%s? no\n\n", question);
|
|
|
|
resolved = 0;
|
|
|
|
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);
|
|
|
|
while (c != '\n' && getc(stdin) != '\n') {
|
|
|
|
if (feof(stdin)) {
|
|
|
|
resolved = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
|
|
|
|
printf("\n");
|
|
|
|
if (c == 'y' || c == 'Y')
|
|
|
|
return (1);
|
|
|
|
resolved = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look up state information for an inode.
|
|
|
|
*/
|
|
|
|
struct inostat *
|
2002-03-20 22:57:10 +00:00
|
|
|
inoinfo(ino_t inum)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Malloc buffers and set up cache.
|
|
|
|
*/
|
|
|
|
void
|
2002-03-20 22:57:10 +00:00
|
|
|
bufinit(void)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
struct bufarea *bp;
|
2000-10-09 09:21:04 +00:00
|
|
|
long bufcnt, i;
|
|
|
|
char *bufp;
|
|
|
|
|
|
|
|
pbp = pdirbp = (struct bufarea *)0;
|
|
|
|
bufp = malloc((unsigned int)sblock.fs_bsize);
|
|
|
|
if (bufp == 0)
|
|
|
|
errx(EEXIT, "cannot allocate buffer pool");
|
|
|
|
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;
|
|
|
|
errx(EEXIT, "cannot allocate buffer pool");
|
|
|
|
}
|
|
|
|
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 *
|
2002-06-21 06:18:05 +00:00
|
|
|
getdatablk(ufs2_daddr_t blkno, long size)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
struct bufarea *bp;
|
2000-10-09 09:21:04 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
errx(EEXIT, "deadlocked buffer pool");
|
|
|
|
getblk(bp, blkno, size);
|
|
|
|
/* fall through */
|
|
|
|
foundit:
|
|
|
|
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
|
2002-06-21 06:18:05 +00:00
|
|
|
getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t dblk;
|
2000-10-09 09:21:04 +00:00
|
|
|
|
2002-05-02 01:03:06 +00:00
|
|
|
totalreads++;
|
2000-10-09 09:21:04 +00:00
|
|
|
dblk = fsbtodb(&sblock, blk);
|
|
|
|
if (bp->b_bno != dblk) {
|
|
|
|
flush(fswritefd, bp);
|
|
|
|
diskreads++;
|
2006-10-31 22:06:56 +00:00
|
|
|
bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size);
|
2000-10-09 09:21:04 +00:00
|
|
|
bp->b_bno = dblk;
|
|
|
|
bp->b_size = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-20 22:57:10 +00:00
|
|
|
flush(int fd, struct bufarea *bp)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
int i, j;
|
2000-10-09 09:21:04 +00:00
|
|
|
|
|
|
|
if (!bp->b_dirty)
|
|
|
|
return;
|
2001-03-21 09:48:03 +00:00
|
|
|
bp->b_dirty = 0;
|
|
|
|
if (fswritefd < 0) {
|
|
|
|
pfatal("WRITING IN READ_ONLY MODE.\n");
|
|
|
|
return;
|
|
|
|
}
|
2000-10-09 09:21:04 +00:00
|
|
|
if (bp->b_errs != 0)
|
2002-07-30 13:01:25 +00:00
|
|
|
pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
|
2000-10-09 09:21:04 +00:00
|
|
|
(bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
|
2002-07-30 13:01:25 +00:00
|
|
|
(long long)bp->b_bno);
|
2000-10-09 09:21:04 +00:00
|
|
|
bp->b_errs = 0;
|
2006-10-31 22:06:56 +00:00
|
|
|
blwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
|
2000-10-09 09:21:04 +00:00
|
|
|
if (bp != &sblk)
|
|
|
|
return;
|
|
|
|
for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
|
2006-10-31 22:06:56 +00:00
|
|
|
blwrite(fswritefd, (char *)sblock.fs_csp + i,
|
2000-10-09 09:21:04 +00:00
|
|
|
fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
|
|
|
|
sblock.fs_cssize - i < sblock.fs_bsize ?
|
|
|
|
sblock.fs_cssize - i : sblock.fs_bsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-21 09:48:03 +00:00
|
|
|
void
|
2002-07-30 13:01:25 +00:00
|
|
|
rwerror(const char *mesg, ufs2_daddr_t blk)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
|
|
|
|
2001-04-24 22:38:08 +00:00
|
|
|
if (bkgrdcheck)
|
|
|
|
exit(EEXIT);
|
2000-10-09 09:21:04 +00:00
|
|
|
if (preen == 0)
|
|
|
|
printf("\n");
|
2001-11-17 23:48:21 +00:00
|
|
|
pfatal("CANNOT %s: %ld", mesg, (long)blk);
|
2000-10-09 09:21:04 +00:00
|
|
|
if (reply("CONTINUE") == 0)
|
|
|
|
exit(EEXIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-20 22:57:10 +00:00
|
|
|
ckfini(int markclean)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
struct bufarea *bp, *nbp;
|
2000-10-09 09:21:04 +00:00
|
|
|
int ofsmodified, cnt = 0;
|
|
|
|
|
2001-03-21 09:48:03 +00:00
|
|
|
if (bkgrdflag) {
|
|
|
|
unlink(snapname);
|
|
|
|
if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
|
|
|
|
cmd.value = FS_UNCLEAN;
|
|
|
|
cmd.size = markclean ? -1 : 1;
|
|
|
|
if (sysctlbyname("vfs.ffs.setflags", 0, 0,
|
|
|
|
&cmd, sizeof cmd) == -1)
|
2003-08-01 11:31:19 +00:00
|
|
|
rwerror("SET FILE SYSTEM FLAGS", FS_UNCLEAN);
|
2001-03-21 09:48:03 +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;
|
|
|
|
}
|
|
|
|
}
|
2000-10-09 09:21:04 +00:00
|
|
|
if (fswritefd < 0) {
|
|
|
|
(void)close(fsreadfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
flush(fswritefd, &sblk);
|
2002-06-21 06:18:05 +00:00
|
|
|
if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
|
2002-11-27 02:18:58 +00:00
|
|
|
sblk.b_bno != sblock.fs_sblockloc / dev_bsize &&
|
2000-10-09 09:21:04 +00:00
|
|
|
!preen && reply("UPDATE STANDARD SUPERBLOCK")) {
|
2002-11-27 02:18:58 +00:00
|
|
|
sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
|
2000-10-09 09:21:04 +00:00
|
|
|
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)
|
|
|
|
errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
|
|
|
|
pbp = pdirbp = (struct bufarea *)0;
|
2001-03-21 09:48:03 +00:00
|
|
|
if (cursnapshot == 0 && sblock.fs_clean != markclean) {
|
2002-10-19 05:36:48 +00:00
|
|
|
if ((sblock.fs_clean = markclean) != 0) {
|
2001-04-16 22:22:21 +00:00
|
|
|
sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
|
2002-10-19 05:36:48 +00:00
|
|
|
sblock.fs_pendingblocks = 0;
|
|
|
|
sblock.fs_pendinginodes = 0;
|
|
|
|
}
|
2000-10-09 09:21:04 +00:00
|
|
|
sbdirty();
|
|
|
|
ofsmodified = fsmodified;
|
|
|
|
flush(fswritefd, &sblk);
|
|
|
|
fsmodified = ofsmodified;
|
|
|
|
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;
|
|
|
|
}
|
2001-04-16 22:22:21 +00:00
|
|
|
if (debug && totalreads > 0)
|
2000-10-09 09:21:04 +00:00
|
|
|
printf("cache missed %ld of %ld (%d%%)\n", diskreads,
|
|
|
|
totalreads, (int)(diskreads * 100 / totalreads));
|
|
|
|
(void)close(fsreadfd);
|
|
|
|
(void)close(fswritefd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-10-31 22:06:56 +00:00
|
|
|
blread(int fd, char *buf, ufs2_daddr_t blk, long size)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
int i, errs;
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
offset = blk;
|
|
|
|
offset *= dev_bsize;
|
2002-12-07 21:40:31 +00:00
|
|
|
if (bkgrdflag)
|
|
|
|
slowio_start();
|
2000-10-09 09:21:04 +00:00
|
|
|
if (lseek(fd, offset, 0) < 0)
|
2001-03-21 09:48:03 +00:00
|
|
|
rwerror("SEEK BLK", blk);
|
2002-12-07 21:40:31 +00:00
|
|
|
else if (read(fd, buf, (int)size) == size) {
|
|
|
|
if (bkgrdflag)
|
|
|
|
slowio_end();
|
2000-10-09 09:21:04 +00:00
|
|
|
return (0);
|
2002-12-07 21:40:31 +00:00
|
|
|
}
|
2001-03-21 09:48:03 +00:00
|
|
|
rwerror("READ BLK", blk);
|
2000-10-09 09:21:04 +00:00
|
|
|
if (lseek(fd, offset, 0) < 0)
|
2001-03-21 09:48:03 +00:00
|
|
|
rwerror("SEEK BLK", blk);
|
2000-10-09 09:21:04 +00:00
|
|
|
errs = 0;
|
|
|
|
memset(buf, 0, (size_t)size);
|
|
|
|
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)
|
2002-07-31 12:01:14 +00:00
|
|
|
printf(" %jd (%jd),",
|
|
|
|
(intmax_t)(blk * dev_bsize + i) / secsize,
|
|
|
|
(intmax_t)blk + i / dev_bsize);
|
2000-10-09 09:21:04 +00:00
|
|
|
else
|
2002-07-31 12:01:14 +00:00
|
|
|
printf(" %jd,", (intmax_t)blk + i / dev_bsize);
|
2000-10-09 09:21:04 +00:00
|
|
|
errs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
if (errs)
|
|
|
|
resolved = 0;
|
|
|
|
return (errs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-10-31 22:06:56 +00:00
|
|
|
blwrite(int fd, char *buf, ufs2_daddr_t blk, long size)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *cp;
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
offset = blk;
|
|
|
|
offset *= dev_bsize;
|
|
|
|
if (lseek(fd, offset, 0) < 0)
|
2001-03-21 09:48:03 +00:00
|
|
|
rwerror("SEEK BLK", blk);
|
2000-10-09 09:21:04 +00:00
|
|
|
else if (write(fd, buf, (int)size) == size) {
|
|
|
|
fsmodified = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolved = 0;
|
2001-03-21 09:48:03 +00:00
|
|
|
rwerror("WRITE BLK", blk);
|
2000-10-09 09:21:04 +00:00
|
|
|
if (lseek(fd, offset, 0) < 0)
|
2001-03-21 09:48:03 +00:00
|
|
|
rwerror("SEEK BLK", blk);
|
2000-10-09 09:21:04 +00:00
|
|
|
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);
|
2002-07-31 12:01:14 +00:00
|
|
|
printf(" %jd,", (intmax_t)blk + i / dev_bsize);
|
2000-10-09 09:21:04 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-10 23:49:23 +00:00
|
|
|
/*
|
|
|
|
* Check cg's magic number. If catastrophic mode is enabled and the cg's
|
|
|
|
* magic number is bad, offer an option to clear the whole cg.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
check_cgmagic(int cg, struct cg *cgp)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!cg_chkmagic(cgp)) {
|
|
|
|
pwarn("CG %d: BAD MAGIC NUMBER\n", cg);
|
|
|
|
if (catastrophicflag) {
|
|
|
|
if (reply("CLEAR CG")) {
|
|
|
|
memset(cgp, 0, (size_t)sblock.fs_cgsize);
|
|
|
|
cgp->cg_initediblk = sblock.fs_ipg;
|
|
|
|
cgp->cg_old_niblk = sblock.fs_ipg;
|
|
|
|
cgp->cg_old_ncyl = sblock.fs_old_cpg;
|
|
|
|
cgp->cg_cgx = cg;
|
|
|
|
cgp->cg_niblk = sblock.fs_ipg;
|
|
|
|
cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg);
|
|
|
|
cgp->cg_magic = CG_MAGIC;
|
|
|
|
cgdirty();
|
|
|
|
printf("PLEASE RERUN FSCK.\n");
|
|
|
|
rerun = 1;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
printf("YOU MAY NEED TO RERUN FSCK WITH -C IF IT CRASHED.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-09 09:21:04 +00:00
|
|
|
/*
|
|
|
|
* allocate a data block with the specified number of fragments
|
|
|
|
*/
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t
|
2002-03-20 22:57:10 +00:00
|
|
|
allocblk(long frags)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
|
|
|
int i, j, k, cg, baseblk;
|
|
|
|
struct cg *cgp = &cgrp;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
cg = dtog(&sblock, i + j);
|
|
|
|
getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
|
2008-04-10 23:49:23 +00:00
|
|
|
check_cgmagic(cg, cgp);
|
2000-10-09 09:21:04 +00:00
|
|
|
baseblk = dtogd(&sblock, i + j);
|
|
|
|
for (k = 0; k < frags; k++) {
|
|
|
|
setbmap(i + j + k);
|
|
|
|
clrbit(cg_blksfree(cgp), baseblk + k);
|
|
|
|
}
|
|
|
|
n_blks += frags;
|
|
|
|
if (frags == sblock.fs_frag)
|
|
|
|
cgp->cg_cs.cs_nbfree--;
|
|
|
|
else
|
|
|
|
cgp->cg_cs.cs_nffree -= frags;
|
|
|
|
cgdirty();
|
|
|
|
return (i + j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a previously allocated block
|
|
|
|
*/
|
|
|
|
void
|
2002-06-21 06:18:05 +00:00
|
|
|
freeblk(ufs2_daddr_t blkno, long frags)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
|
|
|
struct inodesc idesc;
|
|
|
|
|
|
|
|
idesc.id_blkno = blkno;
|
|
|
|
idesc.id_numfrags = frags;
|
|
|
|
(void)pass4check(&idesc);
|
|
|
|
}
|
|
|
|
|
2002-12-07 21:40:31 +00:00
|
|
|
/* Slow down IO so as to leave some disk bandwidth for other processes */
|
|
|
|
void
|
|
|
|
slowio_start()
|
|
|
|
{
|
|
|
|
|
2004-05-18 19:51:41 +00:00
|
|
|
/* Delay one in every 8 operations */
|
2002-12-07 21:40:31 +00:00
|
|
|
slowio_pollcnt = (slowio_pollcnt + 1) & 7;
|
|
|
|
if (slowio_pollcnt == 0) {
|
|
|
|
gettimeofday(&slowio_starttime, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
slowio_end()
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int delay_usec;
|
|
|
|
|
|
|
|
if (slowio_pollcnt != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Update the slowdown interval. */
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 +
|
|
|
|
(tv.tv_usec - slowio_starttime.tv_usec);
|
|
|
|
if (delay_usec < 64)
|
|
|
|
delay_usec = 64;
|
2004-05-18 19:51:41 +00:00
|
|
|
if (delay_usec > 2500000)
|
|
|
|
delay_usec = 2500000;
|
2002-12-07 21:40:31 +00:00
|
|
|
slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6;
|
2004-05-18 19:51:41 +00:00
|
|
|
/* delay by 8 times the average IO delay */
|
|
|
|
if (slowio_delay_usec > 64)
|
|
|
|
usleep(slowio_delay_usec * 8);
|
2002-12-07 21:40:31 +00:00
|
|
|
}
|
|
|
|
|
2000-10-09 09:21:04 +00:00
|
|
|
/*
|
|
|
|
* Find a pathname
|
|
|
|
*/
|
|
|
|
void
|
2002-03-20 22:57:10 +00:00
|
|
|
getpathname(char *namebuf, ino_t curdir, ino_t ino)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
|
|
|
int len;
|
2002-03-20 17:55:10 +00:00
|
|
|
char *cp;
|
2000-10-09 09:21:04 +00:00
|
|
|
struct inodesc idesc;
|
|
|
|
static int busy = 0;
|
|
|
|
|
|
|
|
if (curdir == ino && ino == ROOTINO) {
|
|
|
|
(void)strcpy(namebuf, "/");
|
|
|
|
return;
|
|
|
|
}
|
2004-10-08 20:44:47 +00:00
|
|
|
if (busy || !INO_IS_DVALID(curdir)) {
|
2000-10-09 09:21:04 +00:00
|
|
|
(void)strcpy(namebuf, "?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
busy = 1;
|
|
|
|
memset(&idesc, 0, sizeof(struct inodesc));
|
|
|
|
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;
|
2002-07-30 13:01:25 +00:00
|
|
|
idesc.id_name = strdup("..");
|
2000-10-09 09:21:04 +00:00
|
|
|
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;
|
|
|
|
memmove(cp, namebuf, (size_t)len);
|
|
|
|
*--cp = '/';
|
|
|
|
if (cp < &namebuf[MAXNAMLEN])
|
|
|
|
break;
|
|
|
|
ino = idesc.id_number;
|
|
|
|
}
|
|
|
|
busy = 0;
|
|
|
|
if (ino != ROOTINO)
|
|
|
|
*--cp = '?';
|
|
|
|
memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-30 13:01:25 +00:00
|
|
|
catch(int sig __unused)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
2002-05-12 23:44:15 +00:00
|
|
|
|
|
|
|
ckfini(0);
|
2000-10-09 09:21:04 +00:00
|
|
|
exit(12);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When preening, allow a single quit to signal
|
2002-08-21 18:11:48 +00:00
|
|
|
* a special exit after file system checks complete
|
2000-10-09 09:21:04 +00:00
|
|
|
* so that reboot sequence may be interrupted.
|
|
|
|
*/
|
|
|
|
void
|
2002-07-30 13:01:25 +00:00
|
|
|
catchquit(int sig __unused)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
2002-08-21 18:11:48 +00:00
|
|
|
printf("returning to single-user after file system check\n");
|
2000-10-09 09:21:04 +00:00
|
|
|
returntosingle = 1;
|
|
|
|
(void)signal(SIGQUIT, SIG_DFL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* determine whether an inode should be fixed.
|
|
|
|
*/
|
|
|
|
int
|
2002-07-30 13:01:25 +00:00
|
|
|
dofix(struct inodesc *idesc, const char *msg)
|
2000-10-09 09:21:04 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
switch (idesc->id_fix) {
|
|
|
|
|
|
|
|
case DONTKNOW:
|
|
|
|
if (idesc->id_type == DATA)
|
|
|
|
direrror(idesc->id_number, msg);
|
|
|
|
else
|
2001-08-19 08:19:37 +00:00
|
|
|
pwarn("%s", msg);
|
2000-10-09 09:21:04 +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:
|
|
|
|
errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An unexpected inconsistency occured.
|
2002-08-21 18:11:48 +00:00
|
|
|
* Die if preening or file system is running with soft dependency protocol,
|
2000-10-09 09:21:04 +00:00
|
|
|
* otherwise just print message and continue.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pfatal(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (!preen) {
|
2001-04-24 22:38:08 +00:00
|
|
|
(void)vfprintf(stdout, fmt, ap);
|
2000-10-09 09:21:04 +00:00
|
|
|
va_end(ap);
|
|
|
|
if (usedsoftdep)
|
2001-04-24 22:38:08 +00:00
|
|
|
(void)fprintf(stdout,
|
2000-10-09 09:21:04 +00:00
|
|
|
"\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
|
2001-04-16 22:22:21 +00:00
|
|
|
/*
|
|
|
|
* Force foreground fsck to clean up inconsistency.
|
|
|
|
*/
|
|
|
|
if (bkgrdflag) {
|
|
|
|
cmd.value = FS_NEEDSFSCK;
|
|
|
|
cmd.size = 1;
|
|
|
|
if (sysctlbyname("vfs.ffs.setflags", 0, 0,
|
|
|
|
&cmd, sizeof cmd) == -1)
|
|
|
|
pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
|
2001-04-24 22:38:08 +00:00
|
|
|
fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
|
2001-04-16 22:22:21 +00:00
|
|
|
ckfini(0);
|
|
|
|
exit(EEXIT);
|
|
|
|
}
|
2000-10-09 09:21:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (cdevname == NULL)
|
2002-07-30 13:01:25 +00:00
|
|
|
cdevname = strdup("fsck");
|
2001-04-24 22:38:08 +00:00
|
|
|
(void)fprintf(stdout, "%s: ", cdevname);
|
|
|
|
(void)vfprintf(stdout, fmt, ap);
|
|
|
|
(void)fprintf(stdout,
|
2000-10-09 09:21:04 +00:00
|
|
|
"\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
|
|
|
|
cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
|
2001-04-16 22:22:21 +00:00
|
|
|
/*
|
|
|
|
* Force foreground fsck to clean up inconsistency.
|
|
|
|
*/
|
|
|
|
if (bkgrdflag) {
|
|
|
|
cmd.value = FS_NEEDSFSCK;
|
|
|
|
cmd.size = 1;
|
|
|
|
if (sysctlbyname("vfs.ffs.setflags", 0, 0,
|
|
|
|
&cmd, sizeof cmd) == -1)
|
|
|
|
pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
|
|
|
|
}
|
2000-10-09 09:21:04 +00:00
|
|
|
ckfini(0);
|
|
|
|
exit(EEXIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pwarn just prints a message when not preening or running soft dependency
|
|
|
|
* protocol, or a warning (preceded by filename) when preening.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pwarn(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (preen)
|
2001-04-24 22:38:08 +00:00
|
|
|
(void)fprintf(stdout, "%s: ", cdevname);
|
|
|
|
(void)vfprintf(stdout, fmt, ap);
|
2000-10-09 09:21:04 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stub for routines from kernel.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
panic(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
pfatal("INTERNAL INCONSISTENCY:");
|
2001-04-24 22:38:08 +00:00
|
|
|
(void)vfprintf(stdout, fmt, ap);
|
2000-10-09 09:21:04 +00:00
|
|
|
va_end(ap);
|
|
|
|
exit(EEXIT);
|
|
|
|
}
|