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