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