preen.c revision 75015
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 75015 2001-03-30 08:01:34Z phk $
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, docheck, checkit)
91	int flags;
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, nextpass;
100	void *auxarg;
101	const char *name;
102
103	TAILQ_INIT(&badh);
104	TAILQ_INIT(&diskh);
105
106	sumstatus = 0;
107
108	nextpass = 0;
109	for (passno = 1; nextpass != INT_MAX; passno = nextpass) {
110		if (flags & CHECK_DEBUG)
111			printf("pass %d\n", passno);
112
113		nextpass = INT_MAX;
114		if (setfsent() == 0) {
115			warnx("Can't open checklist file: %s\n", _PATH_FSTAB);
116			return (8);
117		}
118		while ((fs = getfsent()) != 0) {
119			if ((auxarg = (*docheck)(fs)) == NULL)
120				continue;
121
122			name = fs->fs_spec;
123			if (fs->fs_passno > passno && fs->fs_passno < nextpass)
124				nextpass = fs->fs_passno;
125
126			if (passno != fs->fs_passno)
127				continue;
128
129			if (flags & CHECK_DEBUG)
130				printf("pass %d, name %s\n", passno, name);
131
132			if ((flags & CHECK_PREEN) == 0 || passno == 1) {
133				if (name == NULL) {
134					if (flags & CHECK_PREEN)
135						return 8;
136					else
137						continue;
138				}
139				sumstatus = (*checkit)(fs->fs_vfstype,
140				    name, fs->fs_file, auxarg, NULL);
141
142				if (sumstatus)
143					return (sumstatus);
144				continue;
145			}
146			if (name == NULL) {
147				(void) fprintf(stderr,
148				    "BAD DISK NAME %s\n", fs->fs_spec);
149				sumstatus |= 8;
150				continue;
151			}
152			addpart(fs->fs_vfstype, name, fs->fs_file,
153			    auxarg);
154		}
155
156		if ((flags & CHECK_PREEN) == 0 || passno == 1)
157			continue;
158
159		if (flags & CHECK_DEBUG) {
160			printf("Parallel start\n");
161			printpart();
162		}
163
164		TAILQ_FOREACH(nextdisk, &diskh, d_entries) {
165			if ((ret = startdisk(nextdisk, checkit)) != 0)
166				return ret;
167		}
168
169		if (flags & CHECK_DEBUG)
170			printf("Parallel wait\n");
171		while ((pid = wait(&status)) != -1) {
172			TAILQ_FOREACH(d, &diskh, d_entries)
173				if (d->d_pid == pid)
174					break;
175
176			if (d == NULL) {
177				warnx("Unknown pid %d\n", pid);
178				continue;
179			}
180
181
182			if (WIFEXITED(status))
183				retcode = WEXITSTATUS(status);
184			else
185				retcode = 0;
186
187			p = TAILQ_FIRST(&d->d_part);
188
189			if (flags & (CHECK_DEBUG|CHECK_VERBOSE))
190				(void) printf("done %s: %s (%s) = 0x%x\n",
191				    p->p_type, p->p_devname, p->p_mntpt,
192				    status);
193
194			if (WIFSIGNALED(status)) {
195				(void) fprintf(stderr,
196				    "%s: %s (%s): EXITED WITH SIGNAL %d\n",
197				    p->p_type, p->p_devname, p->p_mntpt,
198				    WTERMSIG(status));
199				retcode = 8;
200			}
201
202			TAILQ_REMOVE(&d->d_part, p, p_entries);
203
204			if (retcode != 0) {
205				TAILQ_INSERT_TAIL(&badh, p, p_entries);
206				sumstatus |= retcode;
207			} else {
208				free(p->p_type);
209				free(p->p_devname);
210				free(p);
211			}
212			d->d_pid = 0;
213			nrun--;
214
215			if (TAILQ_EMPTY(&d->d_part)) {
216				TAILQ_REMOVE(&diskh, d, d_entries);
217				ndisks--;
218			}
219		}
220		if (flags & CHECK_DEBUG) {
221			printf("Parallel end\n");
222			printpart();
223		}
224	}
225
226	if (!(flags & CHECK_PREEN))
227			return 0;
228
229	if (sumstatus) {
230		p = TAILQ_FIRST(&badh);
231		if (p == NULL)
232			return (sumstatus);
233
234		(void) fprintf(stderr,
235			"THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
236			TAILQ_NEXT(p, p_entries) ? "S" : "",
237			"UNEXPECTED INCONSISTENCY:");
238
239		for (; p; p = TAILQ_NEXT(p, p_entries))
240			(void) fprintf(stderr,
241			    "%s: %s (%s)%s", p->p_type, p->p_devname,
242			    p->p_mntpt, TAILQ_NEXT(p, p_entries) ? ", " : "\n");
243
244		return sumstatus;
245	}
246	(void) endfsent();
247	return (0);
248}
249
250
251static struct diskentry *
252finddisk(name)
253	const char *name;
254{
255	const char *p;
256	size_t len = 0;
257	struct diskentry *d;
258
259	p = strrchr(name, '/');
260	if (p == NULL)
261		p = name;
262	else
263		p++;
264	for (; *p && !isdigit(*p); p++)
265		continue;
266	for (; *p && isdigit(*p); p++)
267		continue;
268	len = p - name;
269	if (len == 0)
270		len = strlen(name);
271
272	TAILQ_FOREACH(d, &diskh, d_entries)
273		if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0)
274			return d;
275
276	d = emalloc(sizeof(*d));
277	d->d_name = estrdup(name);
278	d->d_name[len] = '\0';
279	TAILQ_INIT(&d->d_part);
280	d->d_pid = 0;
281
282	TAILQ_INSERT_TAIL(&diskh, d, d_entries);
283	ndisks++;
284
285	return d;
286}
287
288
289static void
290printpart()
291{
292	struct diskentry *d;
293	struct partentry *p;
294
295	TAILQ_FOREACH(d, &diskh, d_entries) {
296		(void) printf("disk %s: ", d->d_name);
297		TAILQ_FOREACH(p, &d->d_part, p_entries)
298			(void) printf("%s ", p->p_devname);
299		(void) printf("\n");
300	}
301}
302
303
304static void
305addpart(type, devname, mntpt, auxarg)
306	const char *type, *devname, *mntpt;
307	void *auxarg;
308{
309	struct diskentry *d = finddisk(devname);
310	struct partentry *p;
311
312	TAILQ_FOREACH(p, &d->d_part, p_entries)
313		if (strcmp(p->p_devname, devname) == 0) {
314			warnx("%s in fstab more than once!\n", devname);
315			return;
316		}
317
318	p = emalloc(sizeof(*p));
319	p->p_devname = estrdup(devname);
320	p->p_mntpt = estrdup(mntpt);
321	p->p_type = estrdup(type);
322	p->p_auxarg = auxarg;
323
324	TAILQ_INSERT_TAIL(&d->d_part, p, p_entries);
325}
326
327
328static int
329startdisk(d, checkit)
330	struct diskentry *d;
331	int (*checkit) __P((const char *, const char *, const char *, void *,
332	    pid_t *));
333{
334	struct partentry *p = TAILQ_FIRST(&d->d_part);
335	int rv;
336
337	while ((rv = (*checkit)(p->p_type, p->p_devname, p->p_mntpt,
338	    p->p_auxarg, &d->d_pid)) != 0 && nrun > 0)
339		sleep(10);
340
341	if (rv == 0)
342		nrun++;
343
344	return rv;
345}
346