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