summaryrefslogtreecommitdiff
path: root/sbin/fsck/preen.c
blob: cc051798d2a45a5519d71a707223f62556fd4337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*	$OpenBSD: preen.c,v 1.13 2003/06/02 20:06:15 millert Exp $	*/
/*	$NetBSD: preen.c,v 1.15 1996/09/28 19:21:42 christos Exp $	*/

/*
 * Copyright (c) 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
#if 0
static char sccsid[] = "@(#)preen.c	8.3 (Berkeley) 12/6/94";
#else
static char rcsid[] = "$NetBSD: preen.c,v 1.15 1996/09/28 19:21:42 christos Exp $";
#endif
#endif /* not lint */

#include <sys/param.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/queue.h>

#include <fstab.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <err.h>

#include "fsutil.h"

struct partentry {
	TAILQ_ENTRY(partentry)	 p_entries;
	char		  	*p_devname;	/* device name */
	char			*p_mntpt;	/* mount point */
	char		  	*p_type;	/* filesystem type */
	void			*p_auxarg;	/* auxiliary argument */
};

TAILQ_HEAD(part, partentry) badh;

struct diskentry {
	TAILQ_ENTRY(diskentry) 	    d_entries;
	char		       	   *d_name;	/* disk base name */
	TAILQ_HEAD(prt, partentry)  d_part;	/* list of partitions on disk */
	pid_t			    d_pid;	/* 0 or pid of fsck proc */
};

TAILQ_HEAD(disk, diskentry) diskh;

static int nrun = 0, ndisks = 0;

static struct diskentry *finddisk(const char *);
static void addpart(const char *, const char *, const char *, void *);
static int startdisk(struct diskentry *,
    int (*)(const char *, const char *, const char *, void *, pid_t *));
static void printpart(void);

int
checkfstab(int flags, int maxrun, void *(*docheck)(struct fstab *),
    int (*checkit)(const char *, const char *, const char *, void *, pid_t *))
{
	struct fstab *fs;
	struct diskentry *d, *nextdisk;
	struct partentry *p;
	int ret, retcode, passno, sumstatus, status;
	void *auxarg;
	char *name;
	pid_t pid;

	TAILQ_INIT(&badh);
	TAILQ_INIT(&diskh);

	sumstatus = 0;

	for (passno = 1; passno <= 2; passno++) {
		if (setfsent() == 0) {
			warnx("Can't open checklist file: %s", _PATH_FSTAB);
			return (8);
		}
		while ((fs = getfsent()) != 0) {
			if ((auxarg = (*docheck)(fs)) == NULL)
				continue;

			name = blockcheck(fs->fs_spec);
			if (flags & CHECK_DEBUG)
				printf("pass %d, name %s\n", passno, name);

			if ((flags & CHECK_PREEN) == 0 ||
			    (passno == 1 && fs->fs_passno == 1)) {
				if (name == NULL) {
					if (flags & CHECK_PREEN)
						return 8;
					else
						continue;
				}
				sumstatus = (*checkit)(fs->fs_vfstype,
				    name, fs->fs_file, auxarg, NULL);

				if (sumstatus)
					return (sumstatus);
			} else if (passno == 2 && fs->fs_passno > 1) {
				if (name == NULL) {
					(void) fprintf(stderr,
					    "BAD DISK NAME %s\n", fs->fs_spec);
					sumstatus |= 8;
					continue;
				}
				addpart(fs->fs_vfstype, name, fs->fs_file,
				    auxarg);
			}
		}
		if ((flags & CHECK_PREEN) == 0)
			return 0;
	}

	if (flags & CHECK_DEBUG)
		printpart();

	if (flags & CHECK_PREEN) {
		if (maxrun == 0)
			maxrun = ndisks;
		if (maxrun > ndisks)
			maxrun = ndisks;
		nextdisk = diskh.tqh_first;
		for (passno = 0; passno < maxrun; ++passno) {
			if ((ret = startdisk(nextdisk, checkit)) != 0)
				return ret;
			nextdisk = nextdisk->d_entries.tqe_next;
		}

		while ((pid = wait(&status)) != -1) {
			for (d = diskh.tqh_first; d; d = d->d_entries.tqe_next)
				if (d->d_pid == pid)
					break;

			if (d == NULL) {
				warnx("Unknown pid %ld", (long)pid);
				continue;
			}


			if (WIFEXITED(status))
				retcode = WEXITSTATUS(status);
			else
				retcode = 0;

			p = d->d_part.tqh_first;

			if (flags & (CHECK_DEBUG|CHECK_VERBOSE))
				(void) printf("done %s: %s (%s) = %x\n",
				    p->p_type, p->p_devname, p->p_mntpt,
				    status);

			if (WIFSIGNALED(status)) {
				(void) fprintf(stderr,
				    "%s: %s (%s): EXITED WITH SIGNAL %d\n",
				    p->p_type, p->p_devname, p->p_mntpt,
				    WTERMSIG(status));
				retcode = 8;
			}

			TAILQ_REMOVE(&d->d_part, p, p_entries);

			if (retcode != 0) {
				TAILQ_INSERT_TAIL(&badh, p, p_entries);
				sumstatus |= retcode;
			} else {
				free(p->p_type);
				free(p->p_devname);
				free(p);
			}
			d->d_pid = 0;
			nrun--;

			if (d->d_part.tqh_first == NULL)
				ndisks--;

			if (nextdisk == NULL) {
				if (d->d_part.tqh_first) {
					if ((ret = startdisk(d, checkit)) != 0)
						return ret;
				}
			} else if (nrun < maxrun && nrun < ndisks) {
				for ( ;; ) {
					nextdisk = nextdisk->d_entries.tqe_next;
					if (nextdisk == NULL)
						nextdisk = diskh.tqh_first;
					if (nextdisk->d_part.tqh_first != NULL
					    && nextdisk->d_pid == 0)
						break;
				}
				if ((ret = startdisk(nextdisk, checkit)) != 0)
					return ret;
			}
		}
	}
	if (sumstatus) {
		p = badh.tqh_first;
		if (p == NULL)
			return (sumstatus);

		(void) fprintf(stderr,
			"THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
			p->p_entries.tqe_next ? "S" : "",
			"UNEXPECTED INCONSISTENCY:");

		for (; p; p = p->p_entries.tqe_next)
			(void) fprintf(stderr,
			    "%s: %s (%s)%s", p->p_type, p->p_devname,
			    p->p_mntpt, p->p_entries.tqe_next ? ", " : "\n");

		return sumstatus;
	}
	(void) endfsent();
	return (0);
}


static struct diskentry *
finddisk(const char *name)
{
	const char *p;
	size_t len = 0;
	struct diskentry *d;

	for (p = name + strlen(name) - 1; p >= name; --p)
		if (isdigit(*p)) {
			len = p - name + 1;
			break;
		}

	if (p < name)
		len = strlen(name);

	for (d = diskh.tqh_first; d != NULL; d = d->d_entries.tqe_next)
		if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0)
			return d;

	d = emalloc(sizeof(*d));
	d->d_name = estrdup(name);
	d->d_name[len] = '\0';
	TAILQ_INIT(&d->d_part);
	d->d_pid = 0;

	TAILQ_INSERT_TAIL(&diskh, d, d_entries);
	ndisks++;

	return d;
}


static void
printpart(void)
{
	struct diskentry *d;
	struct partentry *p;

	for (d = diskh.tqh_first; d != NULL; d = d->d_entries.tqe_next) {
		(void) printf("disk %s: ", d->d_name);
		for (p = d->d_part.tqh_first; p != NULL;
		    p = p->p_entries.tqe_next)
			(void) printf("%s ", p->p_devname);
		(void) printf("\n");
	}
}


static void
addpart(const char *type, const char *devname, const char *mntpt, void *auxarg)
{
	struct diskentry *d = finddisk(devname);
	struct partentry *p;

	for (p = d->d_part.tqh_first; p != NULL; p = p->p_entries.tqe_next)
		if (strcmp(p->p_devname, devname) == 0) {
			warnx("%s in fstab more than once!", devname);
			return;
		}

	p = emalloc(sizeof(*p));
	p->p_devname = estrdup(devname);
	p->p_mntpt = estrdup(mntpt);
	p->p_type = estrdup(type);
	p->p_auxarg = auxarg;

	TAILQ_INSERT_TAIL(&d->d_part, p, p_entries);
}


static int
startdisk(struct diskentry *d,
    int (*checkit)(const char *, const char *, const char *, void *, pid_t *))
{
	struct partentry *p = d->d_part.tqh_first;
	int rv;

	while ((rv = (*checkit)(p->p_type, p->p_devname, p->p_mntpt,
	    p->p_auxarg, &d->d_pid)) != 0 && nrun > 0)
		sleep(10);

	if (rv == 0)
		nrun++;

	return rv;
}