Home | History | Annotate | Line # | Download | only in fsck
preen.c revision 1.13
      1  1.13  christos /*	$NetBSD: preen.c,v 1.13 1996/09/23 16:11:35 christos Exp $	*/
      2  1.11       cgd 
      3   1.1       cgd /*
      4   1.6   mycroft  * Copyright (c) 1990, 1993
      5   1.6   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36   1.1       cgd #ifndef lint
     37  1.11       cgd #if 0
     38  1.11       cgd static char sccsid[] = "@(#)preen.c	8.3 (Berkeley) 12/6/94";
     39  1.11       cgd #else
     40  1.13  christos static char rcsid[] = "$NetBSD: preen.c,v 1.13 1996/09/23 16:11:35 christos Exp $";
     41  1.11       cgd #endif
     42   1.1       cgd #endif /* not lint */
     43   1.1       cgd 
     44   1.1       cgd #include <sys/param.h>
     45   1.1       cgd #include <sys/stat.h>
     46   1.1       cgd #include <sys/wait.h>
     47  1.13  christos #include <sys/queue.h>
     48  1.13  christos 
     49   1.1       cgd #include <fstab.h>
     50   1.1       cgd #include <string.h>
     51   1.1       cgd #include <stdio.h>
     52   1.1       cgd #include <stdlib.h>
     53   1.1       cgd #include <ctype.h>
     54   1.9       cgd #include <unistd.h>
     55  1.13  christos #include <err.h>
     56  1.13  christos 
     57  1.13  christos #include "util.h"
     58  1.13  christos #include "extern.h"
     59   1.1       cgd 
     60  1.13  christos struct partentry {
     61  1.13  christos 	TAILQ_ENTRY(partentry)	 p_entries;
     62  1.13  christos 	char		  	*p_name;	/* device name */
     63  1.13  christos 	char		  	*p_type;	/* filesystem type */
     64  1.13  christos };
     65  1.13  christos 
     66  1.13  christos TAILQ_HEAD(part, partentry) badh;
     67  1.13  christos 
     68  1.13  christos struct diskentry {
     69  1.13  christos 	TAILQ_ENTRY(diskentry) 	    d_entries;
     70  1.13  christos 	char		       	   *d_name;	/* disk base name */
     71  1.13  christos 	TAILQ_HEAD(prt, partentry)  d_part;	/* list of partitions on disk */
     72  1.13  christos 	int			    d_pid;	/* 0 or pid of fsck proc */
     73  1.13  christos };
     74  1.13  christos 
     75  1.13  christos TAILQ_HEAD(disk, diskentry) diskh;
     76  1.13  christos 
     77  1.13  christos static int nrun = 0, ndisks = 0;
     78  1.13  christos 
     79  1.13  christos static struct diskentry *finddisk __P((const char *));
     80  1.13  christos static void addpart __P((const char *, const char *));
     81  1.13  christos static int startdisk __P((struct diskentry *,
     82  1.13  christos     int (*)(const char *, const char *, pid_t *)));
     83  1.13  christos #ifdef DEBUG
     84  1.13  christos static void printpart __P((void));
     85  1.13  christos #endif
     86   1.9       cgd 
     87   1.9       cgd int
     88   1.1       cgd checkfstab(preen, maxrun, docheck, chkit)
     89   1.1       cgd 	int preen, maxrun;
     90  1.13  christos 	int (*docheck) __P((struct fstab *));
     91  1.13  christos 	int (*chkit) __P((const char *, const char *, pid_t *));
     92   1.1       cgd {
     93  1.13  christos 	struct fstab *fs;
     94  1.13  christos 	struct diskentry *d, *nextdisk;
     95  1.13  christos 	struct partentry *p;
     96   1.1       cgd 	int ret, pid, retcode, passno, sumstatus, status;
     97   1.1       cgd 	char *name;
     98   1.1       cgd 
     99  1.13  christos 	TAILQ_INIT(&badh);
    100  1.13  christos 	TAILQ_INIT(&diskh);
    101  1.13  christos 
    102   1.1       cgd 	sumstatus = 0;
    103  1.13  christos 
    104   1.1       cgd 	for (passno = 1; passno <= 2; passno++) {
    105   1.1       cgd 		if (setfsent() == 0) {
    106  1.13  christos 			warnx("Can't open checklist file: %s\n", _PATH_FSTAB);
    107   1.1       cgd 			return (8);
    108   1.1       cgd 		}
    109  1.13  christos 		while ((fs = getfsent()) != 0) {
    110  1.13  christos 			if ((*docheck)(fs) == 0)
    111   1.1       cgd 				continue;
    112  1.13  christos 
    113  1.13  christos 			name = blockcheck(fs->fs_spec);
    114  1.13  christos #ifdef DEBUG
    115  1.13  christos 			if (debug > 1)
    116  1.13  christos 				printf("pass %d, name %s\n", passno, name);
    117  1.13  christos #endif
    118  1.13  christos 
    119  1.13  christos 			if (preen == 0 || (passno == 1 && fs->fs_passno == 1)) {
    120  1.13  christos 				if (name == NULL) {
    121  1.13  christos 					if (preen)
    122  1.13  christos 						return 8;
    123  1.13  christos 					else
    124  1.13  christos 						continue;
    125  1.13  christos 				}
    126  1.13  christos 				sumstatus = (*chkit)(fs->fs_vfstype,
    127  1.13  christos 				    name, NULL);
    128  1.13  christos 
    129  1.13  christos 				if (sumstatus)
    130  1.13  christos 					return (sumstatus);
    131  1.13  christos 			} else if (passno == 2 && fs->fs_passno > 1) {
    132  1.13  christos 				if (name == NULL) {
    133  1.13  christos 					(void) fprintf(stderr,
    134  1.13  christos 					    "BAD DISK NAME %s\n", fs->fs_spec);
    135   1.1       cgd 					sumstatus |= 8;
    136   1.1       cgd 					continue;
    137   1.1       cgd 				}
    138  1.13  christos 				addpart(fs->fs_vfstype, name);
    139   1.1       cgd 			}
    140   1.1       cgd 		}
    141   1.1       cgd 		if (preen == 0)
    142   1.1       cgd 			return (0);
    143   1.1       cgd 	}
    144  1.13  christos 
    145  1.13  christos #ifdef DEBUG
    146  1.13  christos 	if (debug > 1)
    147  1.13  christos 		printpart();
    148  1.13  christos #endif
    149  1.13  christos 
    150   1.1       cgd 	if (preen) {
    151   1.1       cgd 		if (maxrun == 0)
    152   1.1       cgd 			maxrun = ndisks;
    153   1.1       cgd 		if (maxrun > ndisks)
    154   1.1       cgd 			maxrun = ndisks;
    155  1.13  christos 		nextdisk = diskh.tqh_first;
    156   1.1       cgd 		for (passno = 0; passno < maxrun; ++passno) {
    157  1.13  christos 			if ((ret = startdisk(nextdisk, chkit)) != 0)
    158  1.13  christos 				return ret;
    159  1.13  christos 			nextdisk = nextdisk->d_entries.tqe_next;
    160   1.1       cgd 		}
    161  1.13  christos 
    162   1.1       cgd 		while ((pid = wait(&status)) != -1) {
    163  1.13  christos 			for (d = diskh.tqh_first; d; d = d->d_entries.tqe_next)
    164  1.13  christos 				if (d->d_pid == pid)
    165   1.1       cgd 					break;
    166  1.13  christos 
    167  1.13  christos 			if (d == NULL) {
    168  1.13  christos 				warnx("Unknown pid %d\n", pid);
    169   1.1       cgd 				continue;
    170   1.1       cgd 			}
    171  1.13  christos 
    172  1.13  christos 
    173   1.1       cgd 			if (WIFEXITED(status))
    174   1.1       cgd 				retcode = WEXITSTATUS(status);
    175   1.1       cgd 			else
    176   1.1       cgd 				retcode = 0;
    177  1.13  christos 
    178  1.13  christos 			p = d->d_part.tqh_first;
    179  1.13  christos 
    180  1.13  christos 			if (debug || verbose)
    181  1.13  christos 				(void) printf("done %s(%s) = %x\n", p->p_name,
    182  1.13  christos 				    p->p_type, status);
    183  1.13  christos 
    184   1.1       cgd 			if (WIFSIGNALED(status)) {
    185  1.13  christos 				(void) fprintf(stderr,
    186  1.13  christos 				    "%s (%s): EXITED WITH SIGNAL %d\n",
    187  1.13  christos 				    p->p_name, p->p_type, WTERMSIG(status));
    188   1.1       cgd 				retcode = 8;
    189   1.1       cgd 			}
    190  1.13  christos 
    191  1.13  christos 			TAILQ_REMOVE(&d->d_part, p, p_entries);
    192  1.13  christos 
    193   1.1       cgd 			if (retcode != 0) {
    194  1.13  christos 				TAILQ_INSERT_TAIL(&badh, p, p_entries);
    195   1.1       cgd 				sumstatus |= retcode;
    196  1.13  christos 			} else {
    197  1.13  christos 				free(p->p_type);
    198  1.13  christos 				free(p->p_name);
    199  1.13  christos 				free(p);
    200  1.13  christos 			}
    201  1.13  christos 			d->d_pid = 0;
    202   1.1       cgd 			nrun--;
    203  1.13  christos 
    204  1.13  christos 			if (d->d_part.tqh_first == NULL)
    205   1.1       cgd 				ndisks--;
    206   1.1       cgd 
    207   1.1       cgd 			if (nextdisk == NULL) {
    208  1.13  christos 				if (d->d_part.tqh_first) {
    209  1.13  christos 					if ((ret = startdisk(d, chkit)) != 0)
    210  1.13  christos 						return ret;
    211   1.1       cgd 				}
    212   1.1       cgd 			} else if (nrun < maxrun && nrun < ndisks) {
    213   1.1       cgd 				for ( ;; ) {
    214  1.13  christos 					nextdisk = nextdisk->d_entries.tqe_next;
    215  1.13  christos 					if (nextdisk == NULL)
    216  1.13  christos 						nextdisk = diskh.tqh_first;
    217  1.13  christos 					if (nextdisk->d_part.tqh_first != NULL
    218  1.13  christos 					    && nextdisk->d_pid == 0)
    219   1.1       cgd 						break;
    220   1.1       cgd 				}
    221  1.13  christos 				if ((ret = startdisk(nextdisk, chkit)) != 0)
    222  1.13  christos 					return ret;
    223   1.1       cgd 			}
    224   1.1       cgd 		}
    225   1.1       cgd 	}
    226   1.1       cgd 	if (sumstatus) {
    227  1.13  christos 		p = badh.tqh_first;
    228  1.13  christos 		if (p == NULL)
    229   1.1       cgd 			return (sumstatus);
    230  1.13  christos 
    231  1.13  christos 		(void) fprintf(stderr,
    232  1.13  christos 			"THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
    233  1.13  christos 			p->p_entries.tqe_next ? "S" : "",
    234  1.13  christos 			"UNEXPECTED INCONSISTENCY:");
    235  1.13  christos 
    236  1.13  christos 		for (; p; p = p->p_entries.tqe_next)
    237  1.13  christos 			(void) fprintf(stderr,
    238  1.13  christos 			    "%s (%s)%s", p->p_name, p->p_type,
    239  1.13  christos 			    p->p_entries.tqe_next ? ", " : "\n");
    240  1.13  christos 
    241  1.13  christos 		return sumstatus;
    242   1.1       cgd 	}
    243  1.13  christos 	(void) endfsent();
    244   1.1       cgd 	return (0);
    245   1.1       cgd }
    246   1.1       cgd 
    247  1.13  christos 
    248  1.13  christos static struct diskentry *
    249   1.1       cgd finddisk(name)
    250  1.13  christos 	const char *name;
    251   1.1       cgd {
    252  1.13  christos 	const char *p;
    253   1.1       cgd 	size_t len;
    254  1.13  christos 	struct diskentry *d;
    255   1.1       cgd 
    256   1.1       cgd 	for (p = name + strlen(name) - 1; p >= name; --p)
    257   1.1       cgd 		if (isdigit(*p)) {
    258   1.1       cgd 			len = p - name + 1;
    259   1.1       cgd 			break;
    260   1.1       cgd 		}
    261  1.13  christos 
    262   1.1       cgd 	if (p < name)
    263   1.1       cgd 		len = strlen(name);
    264   1.1       cgd 
    265  1.13  christos 	for (d = diskh.tqh_first; d != NULL; d = d->d_entries.tqe_next)
    266  1.13  christos 		if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0)
    267  1.13  christos 			return d;
    268  1.13  christos 
    269  1.13  christos 	d = emalloc(sizeof(*d));
    270  1.13  christos 	d->d_name = estrdup(name);
    271  1.13  christos 	d->d_name[len] = '\0';
    272  1.13  christos 	TAILQ_INIT(&d->d_part);
    273  1.13  christos 	d->d_pid = 0;
    274  1.13  christos 
    275  1.13  christos 	TAILQ_INSERT_TAIL(&diskh, d, d_entries);
    276   1.1       cgd 	ndisks++;
    277  1.13  christos 
    278  1.13  christos 	return d;
    279   1.1       cgd }
    280   1.1       cgd 
    281  1.13  christos 
    282  1.13  christos #ifdef DEBUG
    283  1.13  christos static void
    284  1.13  christos printpart()
    285   1.1       cgd {
    286  1.13  christos 	struct diskentry *d;
    287  1.13  christos 	struct partentry *p;
    288   1.1       cgd 
    289  1.13  christos 	for (d = diskh.tqh_first; d != NULL; d = d->d_entries.tqe_next) {
    290  1.13  christos 		(void) printf("disk %s: ", d->d_name);
    291  1.13  christos 		for (p = d->d_part.tqh_first; p != NULL;
    292  1.13  christos 		    p = p->p_entries.tqe_next)
    293  1.13  christos 			(void) printf("%s ", p->p_name);
    294  1.13  christos 		(void) printf("\n");
    295   1.1       cgd 	}
    296   1.1       cgd }
    297  1.13  christos #endif
    298   1.1       cgd 
    299  1.13  christos 
    300  1.13  christos static void
    301  1.13  christos addpart(type, name)
    302  1.13  christos 	const char *type, *name;
    303   1.1       cgd {
    304  1.13  christos 	struct diskentry *d = finddisk(name);
    305  1.13  christos 	struct partentry *p;
    306  1.13  christos 
    307  1.13  christos 	for (p = d->d_part.tqh_first; p != NULL; p = p->p_entries.tqe_next)
    308  1.13  christos 		if (strcmp(p->p_name, name) == 0) {
    309  1.13  christos 			warnx("%s in fstab more than once!\n", name);
    310  1.13  christos 			return;
    311  1.13  christos 		}
    312   1.1       cgd 
    313  1.13  christos 	p = emalloc(sizeof(*p));
    314  1.13  christos 	p->p_name = estrdup(name);
    315  1.13  christos 	p->p_type = estrdup(type);
    316  1.13  christos 
    317  1.13  christos 	TAILQ_INSERT_TAIL(&d->d_part, p, p_entries);
    318   1.1       cgd }
    319   1.1       cgd 
    320   1.1       cgd 
    321  1.13  christos static int
    322  1.13  christos startdisk(d, checkit)
    323  1.13  christos 	register struct diskentry *d;
    324  1.13  christos 	int (*checkit) __P((const char *, const char *, pid_t *));
    325   1.1       cgd {
    326  1.13  christos 	register struct partentry *p = d->d_part.tqh_first;
    327  1.13  christos 	int rv;
    328   1.1       cgd 
    329  1.13  christos 	while ((rv = (*checkit)(p->p_type, p->p_name, &d->d_pid)) != 0 &&
    330  1.13  christos 	    nrun > 0)
    331  1.13  christos 		sleep(10);
    332   1.1       cgd 
    333  1.13  christos 	if (rv == 0)
    334  1.13  christos 		nrun++;
    335   1.1       cgd 
    336  1.13  christos 	return rv;
    337   1.1       cgd }
    338