Home | History | Annotate | Line # | Download | only in fsck
preen.c revision 1.19
      1  1.19     lukem /*	$NetBSD: preen.c,v 1.19 2001/06/18 02:22:33 lukem 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.16     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.11       cgd #if 0
     39  1.17     lukem static char sccsid[] = "@(#)preen.c	8.5 (Berkeley) 4/28/95";
     40  1.11       cgd #else
     41  1.19     lukem __RCSID("$NetBSD: preen.c,v 1.19 2001/06/18 02:22:33 lukem Exp $");
     42  1.11       cgd #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/param.h>
     46   1.1       cgd #include <sys/stat.h>
     47   1.1       cgd #include <sys/wait.h>
     48  1.13  christos #include <sys/queue.h>
     49  1.13  christos 
     50  1.17     lukem #include <err.h>
     51  1.17     lukem #include <ctype.h>
     52   1.1       cgd #include <fstab.h>
     53   1.1       cgd #include <string.h>
     54   1.1       cgd #include <stdio.h>
     55   1.1       cgd #include <stdlib.h>
     56   1.9       cgd #include <unistd.h>
     57  1.13  christos 
     58  1.14  christos #include "fsutil.h"
     59   1.1       cgd 
     60  1.13  christos struct partentry {
     61  1.13  christos 	TAILQ_ENTRY(partentry)	 p_entries;
     62  1.14  christos 	char		  	*p_devname;	/* device name */
     63  1.14  christos 	char			*p_mntpt;	/* mount point */
     64  1.13  christos 	char		  	*p_type;	/* filesystem type */
     65  1.15  christos 	void			*p_auxarg;	/* auxiliary argument */
     66  1.13  christos };
     67  1.13  christos 
     68  1.13  christos TAILQ_HEAD(part, partentry) badh;
     69  1.13  christos 
     70  1.13  christos struct diskentry {
     71  1.13  christos 	TAILQ_ENTRY(diskentry) 	    d_entries;
     72  1.13  christos 	char		       	   *d_name;	/* disk base name */
     73  1.13  christos 	TAILQ_HEAD(prt, partentry)  d_part;	/* list of partitions on disk */
     74  1.13  christos 	int			    d_pid;	/* 0 or pid of fsck proc */
     75  1.13  christos };
     76  1.13  christos 
     77  1.13  christos TAILQ_HEAD(disk, diskentry) diskh;
     78  1.13  christos 
     79  1.13  christos static int nrun = 0, ndisks = 0;
     80  1.13  christos 
     81  1.13  christos static struct diskentry *finddisk __P((const char *));
     82  1.15  christos static void addpart __P((const char *, const char *, const char *, void *));
     83  1.13  christos static int startdisk __P((struct diskentry *,
     84  1.15  christos     int (*)(const char *, const char *, const char *, void *, pid_t *)));
     85  1.13  christos static void printpart __P((void));
     86   1.9       cgd 
     87   1.9       cgd int
     88  1.15  christos checkfstab(flags, maxrun, docheck, checkit)
     89  1.14  christos 	int flags, maxrun;
     90  1.14  christos 	void *(*docheck) __P((struct fstab *));
     91  1.15  christos 	int (*checkit) __P((const char *, const char *, const char *, void *,
     92  1.15  christos 	    pid_t *));
     93   1.1       cgd {
     94  1.13  christos 	struct fstab *fs;
     95  1.13  christos 	struct diskentry *d, *nextdisk;
     96  1.13  christos 	struct partentry *p;
     97   1.1       cgd 	int ret, pid, retcode, passno, sumstatus, status;
     98  1.15  christos 	void *auxarg;
     99  1.18   mycroft 	const char *name;
    100   1.1       cgd 
    101  1.13  christos 	TAILQ_INIT(&badh);
    102  1.13  christos 	TAILQ_INIT(&diskh);
    103  1.13  christos 
    104   1.1       cgd 	sumstatus = 0;
    105  1.13  christos 
    106   1.1       cgd 	for (passno = 1; passno <= 2; passno++) {
    107   1.1       cgd 		if (setfsent() == 0) {
    108  1.13  christos 			warnx("Can't open checklist file: %s\n", _PATH_FSTAB);
    109   1.1       cgd 			return (8);
    110   1.1       cgd 		}
    111  1.13  christos 		while ((fs = getfsent()) != 0) {
    112  1.15  christos 			if ((auxarg = (*docheck)(fs)) == NULL)
    113   1.1       cgd 				continue;
    114  1.13  christos 
    115  1.13  christos 			name = blockcheck(fs->fs_spec);
    116  1.14  christos 			if (flags & CHECK_DEBUG)
    117  1.13  christos 				printf("pass %d, name %s\n", passno, name);
    118  1.13  christos 
    119  1.14  christos 			if ((flags & CHECK_PREEN) == 0 ||
    120  1.14  christos 			    (passno == 1 && fs->fs_passno == 1)) {
    121  1.13  christos 				if (name == NULL) {
    122  1.14  christos 					if (flags & CHECK_PREEN)
    123  1.13  christos 						return 8;
    124  1.13  christos 					else
    125  1.13  christos 						continue;
    126  1.13  christos 				}
    127  1.15  christos 				sumstatus = (*checkit)(fs->fs_vfstype,
    128  1.15  christos 				    name, fs->fs_file, auxarg, NULL);
    129  1.13  christos 
    130  1.13  christos 				if (sumstatus)
    131  1.13  christos 					return (sumstatus);
    132  1.13  christos 			} else if (passno == 2 && fs->fs_passno > 1) {
    133  1.13  christos 				if (name == NULL) {
    134  1.13  christos 					(void) fprintf(stderr,
    135  1.13  christos 					    "BAD DISK NAME %s\n", fs->fs_spec);
    136   1.1       cgd 					sumstatus |= 8;
    137   1.1       cgd 					continue;
    138   1.1       cgd 				}
    139  1.15  christos 				addpart(fs->fs_vfstype, name, fs->fs_file,
    140  1.15  christos 				    auxarg);
    141   1.1       cgd 			}
    142   1.1       cgd 		}
    143  1.14  christos 		if ((flags & CHECK_PREEN) == 0)
    144  1.14  christos 			return 0;
    145   1.1       cgd 	}
    146  1.13  christos 
    147  1.14  christos 	if (flags & CHECK_DEBUG)
    148  1.13  christos 		printpart();
    149  1.13  christos 
    150  1.14  christos 	if (flags & CHECK_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.19     lukem 		nextdisk = TAILQ_FIRST(&diskh);
    156   1.1       cgd 		for (passno = 0; passno < maxrun; ++passno) {
    157  1.15  christos 			if ((ret = startdisk(nextdisk, checkit)) != 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.19     lukem 			TAILQ_FOREACH(d, &diskh, d_entries)
    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.19     lukem 			p = TAILQ_FIRST(&d->d_part);
    179  1.13  christos 
    180  1.14  christos 			if (flags & (CHECK_DEBUG|CHECK_VERBOSE))
    181  1.16     lukem 				(void) printf("done %s: %s (%s) = 0x%x\n",
    182  1.14  christos 				    p->p_type, p->p_devname, p->p_mntpt,
    183  1.14  christos 				    status);
    184  1.13  christos 
    185   1.1       cgd 			if (WIFSIGNALED(status)) {
    186  1.13  christos 				(void) fprintf(stderr,
    187  1.14  christos 				    "%s: %s (%s): EXITED WITH SIGNAL %d\n",
    188  1.14  christos 				    p->p_type, p->p_devname, p->p_mntpt,
    189  1.14  christos 				    WTERMSIG(status));
    190   1.1       cgd 				retcode = 8;
    191   1.1       cgd 			}
    192  1.13  christos 
    193  1.13  christos 			TAILQ_REMOVE(&d->d_part, p, p_entries);
    194  1.13  christos 
    195   1.1       cgd 			if (retcode != 0) {
    196  1.13  christos 				TAILQ_INSERT_TAIL(&badh, p, p_entries);
    197   1.1       cgd 				sumstatus |= retcode;
    198  1.13  christos 			} else {
    199  1.13  christos 				free(p->p_type);
    200  1.14  christos 				free(p->p_devname);
    201  1.13  christos 				free(p);
    202  1.13  christos 			}
    203  1.13  christos 			d->d_pid = 0;
    204   1.1       cgd 			nrun--;
    205  1.13  christos 
    206  1.19     lukem 			if (TAILQ_FIRST(&d->d_part) == NULL)
    207   1.1       cgd 				ndisks--;
    208   1.1       cgd 
    209   1.1       cgd 			if (nextdisk == NULL) {
    210  1.19     lukem 				if (TAILQ_FIRST(&d->d_part) != NULL) {
    211  1.15  christos 					if ((ret = startdisk(d, checkit)) != 0)
    212  1.13  christos 						return ret;
    213   1.1       cgd 				}
    214   1.1       cgd 			} else if (nrun < maxrun && nrun < ndisks) {
    215   1.1       cgd 				for ( ;; ) {
    216  1.19     lukem 					nextdisk = TAILQ_NEXT(nextdisk,
    217  1.19     lukem 					    d_entries);
    218  1.13  christos 					if (nextdisk == NULL)
    219  1.19     lukem 						nextdisk = TAILQ_FIRST(&diskh);
    220  1.19     lukem 					if (TAILQ_FIRST(&nextdisk->d_part)
    221  1.19     lukem 					    != NULL && nextdisk->d_pid == 0)
    222   1.1       cgd 						break;
    223   1.1       cgd 				}
    224  1.15  christos 				if ((ret = startdisk(nextdisk, checkit)) != 0)
    225  1.13  christos 					return ret;
    226   1.1       cgd 			}
    227   1.1       cgd 		}
    228   1.1       cgd 	}
    229   1.1       cgd 	if (sumstatus) {
    230  1.19     lukem 		p = TAILQ_FIRST(&badh);
    231  1.13  christos 		if (p == NULL)
    232   1.1       cgd 			return (sumstatus);
    233  1.13  christos 
    234  1.13  christos 		(void) fprintf(stderr,
    235  1.13  christos 			"THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
    236  1.13  christos 			p->p_entries.tqe_next ? "S" : "",
    237  1.13  christos 			"UNEXPECTED INCONSISTENCY:");
    238  1.13  christos 
    239  1.13  christos 		for (; p; p = p->p_entries.tqe_next)
    240  1.13  christos 			(void) fprintf(stderr,
    241  1.14  christos 			    "%s: %s (%s)%s", p->p_type, p->p_devname,
    242  1.14  christos 			    p->p_mntpt, p->p_entries.tqe_next ? ", " : "\n");
    243  1.13  christos 
    244  1.13  christos 		return sumstatus;
    245   1.1       cgd 	}
    246  1.13  christos 	(void) endfsent();
    247   1.1       cgd 	return (0);
    248   1.1       cgd }
    249   1.1       cgd 
    250  1.13  christos 
    251  1.13  christos static struct diskentry *
    252   1.1       cgd finddisk(name)
    253  1.13  christos 	const char *name;
    254   1.1       cgd {
    255  1.13  christos 	const char *p;
    256  1.16     lukem 	size_t len = 0;
    257  1.13  christos 	struct diskentry *d;
    258   1.1       cgd 
    259  1.17     lukem 	for (len = strlen(name), p = name + len - 1; p >= name; --p)
    260   1.1       cgd 		if (isdigit(*p)) {
    261   1.1       cgd 			len = p - name + 1;
    262   1.1       cgd 			break;
    263   1.1       cgd 		}
    264   1.1       cgd 	if (p < name)
    265   1.1       cgd 		len = strlen(name);
    266   1.1       cgd 
    267  1.19     lukem 	TAILQ_FOREACH(d, &diskh, d_entries)
    268  1.13  christos 		if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0)
    269  1.13  christos 			return d;
    270  1.13  christos 
    271  1.13  christos 	d = emalloc(sizeof(*d));
    272  1.13  christos 	d->d_name = estrdup(name);
    273  1.13  christos 	d->d_name[len] = '\0';
    274  1.13  christos 	TAILQ_INIT(&d->d_part);
    275  1.13  christos 	d->d_pid = 0;
    276  1.13  christos 
    277  1.13  christos 	TAILQ_INSERT_TAIL(&diskh, d, d_entries);
    278   1.1       cgd 	ndisks++;
    279  1.13  christos 
    280  1.13  christos 	return d;
    281   1.1       cgd }
    282   1.1       cgd 
    283  1.13  christos 
    284  1.13  christos static void
    285  1.13  christos printpart()
    286   1.1       cgd {
    287  1.13  christos 	struct diskentry *d;
    288  1.13  christos 	struct partentry *p;
    289   1.1       cgd 
    290  1.19     lukem 	TAILQ_FOREACH(d, &diskh, d_entries) {
    291  1.13  christos 		(void) printf("disk %s: ", d->d_name);
    292  1.19     lukem 		TAILQ_FOREACH(p, &d->d_part, p_entries)
    293  1.14  christos 			(void) printf("%s ", p->p_devname);
    294  1.13  christos 		(void) printf("\n");
    295   1.1       cgd 	}
    296   1.1       cgd }
    297   1.1       cgd 
    298  1.13  christos 
    299  1.13  christos static void
    300  1.15  christos addpart(type, devname, mntpt, auxarg)
    301  1.14  christos 	const char *type, *devname, *mntpt;
    302  1.15  christos 	void *auxarg;
    303   1.1       cgd {
    304  1.14  christos 	struct diskentry *d = finddisk(devname);
    305  1.13  christos 	struct partentry *p;
    306  1.13  christos 
    307  1.19     lukem 	TAILQ_FOREACH(p, &d->d_part, p_entries)
    308  1.14  christos 		if (strcmp(p->p_devname, devname) == 0) {
    309  1.14  christos 			warnx("%s in fstab more than once!\n", devname);
    310  1.13  christos 			return;
    311  1.13  christos 		}
    312   1.1       cgd 
    313  1.13  christos 	p = emalloc(sizeof(*p));
    314  1.14  christos 	p->p_devname = estrdup(devname);
    315  1.14  christos 	p->p_mntpt = estrdup(mntpt);
    316  1.13  christos 	p->p_type = estrdup(type);
    317  1.15  christos 	p->p_auxarg = auxarg;
    318  1.13  christos 
    319  1.13  christos 	TAILQ_INSERT_TAIL(&d->d_part, p, p_entries);
    320   1.1       cgd }
    321   1.1       cgd 
    322   1.1       cgd 
    323  1.13  christos static int
    324  1.13  christos startdisk(d, checkit)
    325  1.16     lukem 	struct diskentry *d;
    326  1.15  christos 	int (*checkit) __P((const char *, const char *, const char *, void *,
    327  1.15  christos 	    pid_t *));
    328   1.1       cgd {
    329  1.19     lukem 	struct partentry *p = TAILQ_FIRST(&d->d_part);
    330  1.13  christos 	int rv;
    331   1.1       cgd 
    332  1.14  christos 	while ((rv = (*checkit)(p->p_type, p->p_devname, p->p_mntpt,
    333  1.15  christos 	    p->p_auxarg, &d->d_pid)) != 0 && nrun > 0)
    334  1.13  christos 		sleep(10);
    335   1.1       cgd 
    336  1.13  christos 	if (rv == 0)
    337  1.13  christos 		nrun++;
    338   1.1       cgd 
    339  1.13  christos 	return rv;
    340   1.1       cgd }
    341