2000-10-09 12:26:15 +02:00
|
|
|
/* $NetBSD: preen.c,v 1.18 1998/07/26 20:02:36 mycroft Exp $ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1990, 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.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)preen.c 8.5 (Berkeley) 4/28/95";
|
|
|
|
#else
|
|
|
|
__RCSID("$NetBSD: preen.c,v 1.18 1998/07/26 20:02:36 mycroft Exp $");
|
|
|
|
#endif
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fstab.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "fsutil.h"
|
|
|
|
|
|
|
|
struct partentry {
|
|
|
|
TAILQ_ENTRY(partentry) p_entries;
|
|
|
|
char *p_devname; /* device name */
|
|
|
|
char *p_mntpt; /* mount point */
|
2002-08-21 20:11:48 +02:00
|
|
|
char *p_type; /* file system type */
|
2000-10-09 12:26:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TAILQ_HEAD(part, partentry) badh;
|
|
|
|
|
|
|
|
struct diskentry {
|
|
|
|
TAILQ_ENTRY(diskentry) d_entries;
|
|
|
|
char *d_name; /* disk base name */
|
|
|
|
TAILQ_HEAD(prt, partentry) d_part; /* list of partitions on disk */
|
|
|
|
int d_pid; /* 0 or pid of fsck proc */
|
|
|
|
};
|
|
|
|
|
|
|
|
TAILQ_HEAD(disk, diskentry) diskh;
|
|
|
|
|
|
|
|
static int nrun = 0, ndisks = 0;
|
|
|
|
|
2002-03-20 23:57:10 +01:00
|
|
|
static struct diskentry *finddisk(const char *);
|
|
|
|
static void addpart(const char *, const char *, const char *);
|
|
|
|
static int startdisk(struct diskentry *,
|
|
|
|
int (*)(const char *, const char *, const char *, char *, pid_t *));
|
|
|
|
static void printpart(void);
|
2000-10-09 12:26:15 +02:00
|
|
|
|
|
|
|
int
|
2002-03-20 23:57:10 +01:00
|
|
|
checkfstab(int flags, int (*docheck)(struct fstab *),
|
|
|
|
int (*checkit)(const char *, const char *, const char *, char *, pid_t *))
|
2000-10-09 12:26:15 +02:00
|
|
|
{
|
|
|
|
struct fstab *fs;
|
|
|
|
struct diskentry *d, *nextdisk;
|
|
|
|
struct partentry *p;
|
2001-03-30 10:01:34 +02:00
|
|
|
int ret, pid, retcode, passno, sumstatus, status, nextpass;
|
2000-10-09 12:26:15 +02:00
|
|
|
const char *name;
|
|
|
|
|
|
|
|
TAILQ_INIT(&badh);
|
|
|
|
TAILQ_INIT(&diskh);
|
|
|
|
|
|
|
|
sumstatus = 0;
|
|
|
|
|
2001-03-30 10:01:34 +02:00
|
|
|
nextpass = 0;
|
|
|
|
for (passno = 1; nextpass != INT_MAX; passno = nextpass) {
|
|
|
|
if (flags & CHECK_DEBUG)
|
|
|
|
printf("pass %d\n", passno);
|
|
|
|
|
|
|
|
nextpass = INT_MAX;
|
2000-10-09 12:26:15 +02:00
|
|
|
if (setfsent() == 0) {
|
|
|
|
warnx("Can't open checklist file: %s\n", _PATH_FSTAB);
|
|
|
|
return (8);
|
|
|
|
}
|
|
|
|
while ((fs = getfsent()) != 0) {
|
|
|
|
name = fs->fs_spec;
|
2001-03-30 10:01:34 +02:00
|
|
|
if (fs->fs_passno > passno && fs->fs_passno < nextpass)
|
|
|
|
nextpass = fs->fs_passno;
|
|
|
|
|
2002-12-17 20:33:44 +01:00
|
|
|
if (passno != fs->fs_passno)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((*docheck)(fs) == 0)
|
2001-03-30 10:01:34 +02:00
|
|
|
continue;
|
|
|
|
|
2000-10-09 12:26:15 +02:00
|
|
|
if (flags & CHECK_DEBUG)
|
|
|
|
printf("pass %d, name %s\n", passno, name);
|
|
|
|
|
Add support for running foreground (-F) and background (-B) checks.
Traditionally, fsck is invoked before the filesystems are mounted
and all checks are done to completion at that time. If background
checking is available, fsck is invoked twice. It is first invoked
at the traditional time, before the filesystems are mounted, with
the -F flag to do checking on all the filesystems that cannot do
background checking. It is then invoked a second time, after the
system has completed going multiuser, with the -B flag to do checking
on all the filesystems that can do background checking. Unlike
the foreground checking, the background checking is started
asynchonously so that other system activity can proceed even on
the filesystems that are being checked.
At the moment, only the fast filesystem supports background checking.
To be able to do background checking, a filesystem must have been
running with soft updates, not have been marked as needing a
foreground check, and be mounted and writable when the background
check is to be done (i.e., not listed as `noauto' in /etc/fstab).
These changes are the final piece needed to support background
filesystem checking. They will not have any effect until you update
your /etc/rc to invoke fsck in its new mode of operation. I am
still playing around with exactly what those changes should be
and should be committing them later this week.
2001-04-25 09:18:22 +02:00
|
|
|
if ((flags & CHECK_PREEN) == 0 || passno == 1 ||
|
|
|
|
(flags & DO_BACKGRD) != 0) {
|
2000-10-09 12:26:15 +02:00
|
|
|
if (name == NULL) {
|
|
|
|
if (flags & CHECK_PREEN)
|
|
|
|
return 8;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sumstatus = (*checkit)(fs->fs_vfstype,
|
Add support for running foreground (-F) and background (-B) checks.
Traditionally, fsck is invoked before the filesystems are mounted
and all checks are done to completion at that time. If background
checking is available, fsck is invoked twice. It is first invoked
at the traditional time, before the filesystems are mounted, with
the -F flag to do checking on all the filesystems that cannot do
background checking. It is then invoked a second time, after the
system has completed going multiuser, with the -B flag to do checking
on all the filesystems that can do background checking. Unlike
the foreground checking, the background checking is started
asynchonously so that other system activity can proceed even on
the filesystems that are being checked.
At the moment, only the fast filesystem supports background checking.
To be able to do background checking, a filesystem must have been
running with soft updates, not have been marked as needing a
foreground check, and be mounted and writable when the background
check is to be done (i.e., not listed as `noauto' in /etc/fstab).
These changes are the final piece needed to support background
filesystem checking. They will not have any effect until you update
your /etc/rc to invoke fsck in its new mode of operation. I am
still playing around with exactly what those changes should be
and should be committing them later this week.
2001-04-25 09:18:22 +02:00
|
|
|
name, fs->fs_file, NULL, NULL);
|
2000-10-09 12:26:15 +02:00
|
|
|
|
|
|
|
if (sumstatus)
|
|
|
|
return (sumstatus);
|
2001-03-30 10:01:34 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (name == NULL) {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"BAD DISK NAME %s\n", fs->fs_spec);
|
|
|
|
sumstatus |= 8;
|
|
|
|
continue;
|
2000-10-09 12:26:15 +02:00
|
|
|
}
|
Add support for running foreground (-F) and background (-B) checks.
Traditionally, fsck is invoked before the filesystems are mounted
and all checks are done to completion at that time. If background
checking is available, fsck is invoked twice. It is first invoked
at the traditional time, before the filesystems are mounted, with
the -F flag to do checking on all the filesystems that cannot do
background checking. It is then invoked a second time, after the
system has completed going multiuser, with the -B flag to do checking
on all the filesystems that can do background checking. Unlike
the foreground checking, the background checking is started
asynchonously so that other system activity can proceed even on
the filesystems that are being checked.
At the moment, only the fast filesystem supports background checking.
To be able to do background checking, a filesystem must have been
running with soft updates, not have been marked as needing a
foreground check, and be mounted and writable when the background
check is to be done (i.e., not listed as `noauto' in /etc/fstab).
These changes are the final piece needed to support background
filesystem checking. They will not have any effect until you update
your /etc/rc to invoke fsck in its new mode of operation. I am
still playing around with exactly what those changes should be
and should be committing them later this week.
2001-04-25 09:18:22 +02:00
|
|
|
addpart(fs->fs_vfstype, name, fs->fs_file);
|
2000-10-09 12:26:15 +02:00
|
|
|
}
|
|
|
|
|
Add support for running foreground (-F) and background (-B) checks.
Traditionally, fsck is invoked before the filesystems are mounted
and all checks are done to completion at that time. If background
checking is available, fsck is invoked twice. It is first invoked
at the traditional time, before the filesystems are mounted, with
the -F flag to do checking on all the filesystems that cannot do
background checking. It is then invoked a second time, after the
system has completed going multiuser, with the -B flag to do checking
on all the filesystems that can do background checking. Unlike
the foreground checking, the background checking is started
asynchonously so that other system activity can proceed even on
the filesystems that are being checked.
At the moment, only the fast filesystem supports background checking.
To be able to do background checking, a filesystem must have been
running with soft updates, not have been marked as needing a
foreground check, and be mounted and writable when the background
check is to be done (i.e., not listed as `noauto' in /etc/fstab).
These changes are the final piece needed to support background
filesystem checking. They will not have any effect until you update
your /etc/rc to invoke fsck in its new mode of operation. I am
still playing around with exactly what those changes should be
and should be committing them later this week.
2001-04-25 09:18:22 +02:00
|
|
|
if ((flags & CHECK_PREEN) == 0 || passno == 1 ||
|
|
|
|
(flags & DO_BACKGRD) != 0)
|
2001-03-30 10:01:34 +02:00
|
|
|
continue;
|
2000-10-09 12:26:15 +02:00
|
|
|
|
2001-03-30 10:01:34 +02:00
|
|
|
if (flags & CHECK_DEBUG) {
|
|
|
|
printf("Parallel start\n");
|
|
|
|
printpart();
|
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_FOREACH(nextdisk, &diskh, d_entries) {
|
2000-10-09 12:26:15 +02:00
|
|
|
if ((ret = startdisk(nextdisk, checkit)) != 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-03-30 10:01:34 +02:00
|
|
|
if (flags & CHECK_DEBUG)
|
|
|
|
printf("Parallel wait\n");
|
2000-10-09 12:26:15 +02:00
|
|
|
while ((pid = wait(&status)) != -1) {
|
2000-12-27 22:38:06 +01:00
|
|
|
TAILQ_FOREACH(d, &diskh, d_entries)
|
2000-10-09 12:26:15 +02:00
|
|
|
if (d->d_pid == pid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (d == NULL) {
|
|
|
|
warnx("Unknown pid %d\n", pid);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
retcode = WEXITSTATUS(status);
|
|
|
|
else
|
|
|
|
retcode = 0;
|
|
|
|
|
2000-12-27 22:38:06 +01:00
|
|
|
p = TAILQ_FIRST(&d->d_part);
|
2000-10-09 12:26:15 +02:00
|
|
|
|
|
|
|
if (flags & (CHECK_DEBUG|CHECK_VERBOSE))
|
|
|
|
(void) printf("done %s: %s (%s) = 0x%x\n",
|
|
|
|
p->p_type, p->p_devname, p->p_mntpt,
|
|
|
|
status);
|
|
|
|
|
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"%s: %s (%s): EXITED WITH SIGNAL %d\n",
|
|
|
|
p->p_type, p->p_devname, p->p_mntpt,
|
|
|
|
WTERMSIG(status));
|
|
|
|
retcode = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_REMOVE(&d->d_part, p, p_entries);
|
|
|
|
|
|
|
|
if (retcode != 0) {
|
|
|
|
TAILQ_INSERT_TAIL(&badh, p, p_entries);
|
|
|
|
sumstatus |= retcode;
|
|
|
|
} else {
|
|
|
|
free(p->p_type);
|
|
|
|
free(p->p_devname);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
d->d_pid = 0;
|
|
|
|
nrun--;
|
|
|
|
|
2001-03-30 10:01:34 +02:00
|
|
|
if (TAILQ_EMPTY(&d->d_part)) {
|
|
|
|
TAILQ_REMOVE(&diskh, d, d_entries);
|
2000-10-09 12:26:15 +02:00
|
|
|
ndisks--;
|
2001-04-07 15:24:59 +02:00
|
|
|
} else {
|
|
|
|
if ((ret = startdisk(d, checkit)) != 0)
|
|
|
|
return ret;
|
2000-10-09 12:26:15 +02:00
|
|
|
}
|
|
|
|
}
|
2001-03-30 10:01:34 +02:00
|
|
|
if (flags & CHECK_DEBUG) {
|
|
|
|
printf("Parallel end\n");
|
|
|
|
printpart();
|
|
|
|
}
|
2000-10-09 12:26:15 +02:00
|
|
|
}
|
2001-03-30 10:01:34 +02:00
|
|
|
|
|
|
|
if (!(flags & CHECK_PREEN))
|
|
|
|
return 0;
|
|
|
|
|
2000-10-09 12:26:15 +02:00
|
|
|
if (sumstatus) {
|
2000-12-30 22:05:45 +01:00
|
|
|
p = TAILQ_FIRST(&badh);
|
2000-10-09 12:26:15 +02:00
|
|
|
if (p == NULL)
|
|
|
|
return (sumstatus);
|
|
|
|
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
|
2000-12-27 22:38:06 +01:00
|
|
|
TAILQ_NEXT(p, p_entries) ? "S" : "",
|
2000-10-09 12:26:15 +02:00
|
|
|
"UNEXPECTED INCONSISTENCY:");
|
|
|
|
|
2000-12-27 22:38:06 +01:00
|
|
|
for (; p; p = TAILQ_NEXT(p, p_entries))
|
2000-10-09 12:26:15 +02:00
|
|
|
(void) fprintf(stderr,
|
|
|
|
"%s: %s (%s)%s", p->p_type, p->p_devname,
|
2000-12-27 22:38:06 +01:00
|
|
|
p->p_mntpt, TAILQ_NEXT(p, p_entries) ? ", " : "\n");
|
2000-10-09 12:26:15 +02:00
|
|
|
|
|
|
|
return sumstatus;
|
|
|
|
}
|
|
|
|
(void) endfsent();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct diskentry *
|
2002-03-20 23:57:10 +01:00
|
|
|
finddisk(const char *name)
|
2000-10-09 12:26:15 +02:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
size_t len = 0;
|
|
|
|
struct diskentry *d;
|
|
|
|
|
2000-12-27 23:28:40 +01:00
|
|
|
p = strrchr(name, '/');
|
|
|
|
if (p == NULL)
|
|
|
|
p = name;
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
for (; *p && !isdigit(*p); p++)
|
|
|
|
continue;
|
|
|
|
for (; *p && isdigit(*p); p++)
|
|
|
|
continue;
|
|
|
|
len = p - name;
|
|
|
|
if (len == 0)
|
2000-10-09 12:26:15 +02:00
|
|
|
len = strlen(name);
|
|
|
|
|
2000-12-27 22:38:06 +01:00
|
|
|
TAILQ_FOREACH(d, &diskh, d_entries)
|
2000-10-09 12:26:15 +02:00
|
|
|
if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0)
|
|
|
|
return d;
|
|
|
|
|
|
|
|
d = emalloc(sizeof(*d));
|
|
|
|
d->d_name = estrdup(name);
|
|
|
|
d->d_name[len] = '\0';
|
|
|
|
TAILQ_INIT(&d->d_part);
|
|
|
|
d->d_pid = 0;
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&diskh, d, d_entries);
|
|
|
|
ndisks++;
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2002-03-20 23:57:10 +01:00
|
|
|
printpart(void)
|
2000-10-09 12:26:15 +02:00
|
|
|
{
|
|
|
|
struct diskentry *d;
|
|
|
|
struct partentry *p;
|
|
|
|
|
2000-12-27 22:38:06 +01:00
|
|
|
TAILQ_FOREACH(d, &diskh, d_entries) {
|
2000-10-09 12:26:15 +02:00
|
|
|
(void) printf("disk %s: ", d->d_name);
|
2000-12-27 22:38:06 +01:00
|
|
|
TAILQ_FOREACH(p, &d->d_part, p_entries)
|
2000-10-09 12:26:15 +02:00
|
|
|
(void) printf("%s ", p->p_devname);
|
|
|
|
(void) printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2002-03-20 23:57:10 +01:00
|
|
|
addpart(const char *type, const char *devname, const char *mntpt)
|
2000-10-09 12:26:15 +02:00
|
|
|
{
|
|
|
|
struct diskentry *d = finddisk(devname);
|
|
|
|
struct partentry *p;
|
|
|
|
|
2000-12-27 22:38:06 +01:00
|
|
|
TAILQ_FOREACH(p, &d->d_part, p_entries)
|
2000-10-09 12:26:15 +02:00
|
|
|
if (strcmp(p->p_devname, devname) == 0) {
|
|
|
|
warnx("%s in fstab more than once!\n", devname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = emalloc(sizeof(*p));
|
|
|
|
p->p_devname = estrdup(devname);
|
|
|
|
p->p_mntpt = estrdup(mntpt);
|
|
|
|
p->p_type = estrdup(type);
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&d->d_part, p, p_entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-03-20 23:57:10 +01:00
|
|
|
startdisk(struct diskentry *d, int (*checkit)(const char *, const char *,
|
|
|
|
const char *, char *, pid_t *))
|
2000-10-09 12:26:15 +02:00
|
|
|
{
|
2000-12-30 22:05:45 +01:00
|
|
|
struct partentry *p = TAILQ_FIRST(&d->d_part);
|
2000-10-09 12:26:15 +02:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
while ((rv = (*checkit)(p->p_type, p->p_devname, p->p_mntpt,
|
Add support for running foreground (-F) and background (-B) checks.
Traditionally, fsck is invoked before the filesystems are mounted
and all checks are done to completion at that time. If background
checking is available, fsck is invoked twice. It is first invoked
at the traditional time, before the filesystems are mounted, with
the -F flag to do checking on all the filesystems that cannot do
background checking. It is then invoked a second time, after the
system has completed going multiuser, with the -B flag to do checking
on all the filesystems that can do background checking. Unlike
the foreground checking, the background checking is started
asynchonously so that other system activity can proceed even on
the filesystems that are being checked.
At the moment, only the fast filesystem supports background checking.
To be able to do background checking, a filesystem must have been
running with soft updates, not have been marked as needing a
foreground check, and be mounted and writable when the background
check is to be done (i.e., not listed as `noauto' in /etc/fstab).
These changes are the final piece needed to support background
filesystem checking. They will not have any effect until you update
your /etc/rc to invoke fsck in its new mode of operation. I am
still playing around with exactly what those changes should be
and should be committing them later this week.
2001-04-25 09:18:22 +02:00
|
|
|
NULL, &d->d_pid)) != 0 && nrun > 0)
|
2000-10-09 12:26:15 +02:00
|
|
|
sleep(10);
|
|
|
|
|
|
|
|
if (rv == 0)
|
|
|
|
nrun++;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|