xref: /openbsd-src/sbin/fsck_ext2fs/utilities.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: utilities.c,v 1.23 2014/07/13 16:08:53 pelikan Exp $	*/
2 /*	$NetBSD: utilities.c,v 1.6 2001/02/04 21:19:34 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1980, 1986, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <ufs/ext2fs/ext2fs_dinode.h>
37 #include <ufs/ext2fs/ext2fs_dir.h>
38 #include <ufs/ext2fs/ext2fs.h>
39 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <unistd.h>
45 #include <errno.h>
46 
47 #include "fsutil.h"
48 #include "fsck.h"
49 #include "extern.h"
50 
51 long	diskreads, totalreads;	/* Disk cache statistics */
52 
53 static void rwerror(char *, daddr32_t);
54 
55 int
56 ftypeok(struct ext2fs_dinode *dp)
57 {
58 	switch (letoh16(dp->e2di_mode) & IFMT) {
59 
60 	case IFDIR:
61 	case IFREG:
62 	case IFBLK:
63 	case IFCHR:
64 	case IFLNK:
65 	case IFSOCK:
66 	case IFIFO:
67 		return (1);
68 
69 	default:
70 		if (debug)
71 			printf("bad file type 0%o\n", letoh16(dp->e2di_mode));
72 		return (0);
73 	}
74 }
75 
76 int
77 reply(char *question)
78 {
79 	int persevere;
80 	int c;
81 
82 	if (preen)
83 		pfatal("INTERNAL ERROR: GOT TO reply()");
84 	persevere = !strcmp(question, "CONTINUE");
85 	printf("\n");
86 	if (!persevere && (nflag || fswritefd < 0)) {
87 		printf("%s? no\n\n", question);
88 		return (0);
89 	}
90 	if (yflag || (persevere && nflag)) {
91 		printf("%s? yes\n\n", question);
92 		return (1);
93 	}
94 	do {
95 		printf("%s? [Fyn?] ", question);
96 		(void) fflush(stdout);
97 		c = getc(stdin);
98 		if (c == 'F') {
99 			yflag = 1;
100 			return (1);
101 		}
102 		while (c != '\n' && getc(stdin) != '\n')
103 			if (feof(stdin))
104 				return (0);
105 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
106 	printf("\n");
107 	if (c == 'y' || c == 'Y')
108 		return (1);
109 	return (0);
110 }
111 
112 /*
113  * Malloc buffers and set up cache.
114  */
115 void
116 bufinit(void)
117 {
118 	struct bufarea *bp;
119 	long bufcnt, i;
120 	char *bufp;
121 
122 	diskreads = totalreads = 0;
123 	pbp = pdirbp = (struct bufarea *)0;
124 	bufhead.b_next = bufhead.b_prev = &bufhead;
125 	bufcnt = MAXBUFSPACE / sblock.e2fs_bsize;
126 	if (bufcnt < MINBUFS)
127 		bufcnt = MINBUFS;
128 	for (i = 0; i < bufcnt; i++) {
129 		bp = (struct bufarea *)malloc(sizeof(struct bufarea));
130 		bufp = malloc((unsigned int)sblock.e2fs_bsize);
131 		if (bp == NULL || bufp == NULL) {
132 			free(bp);
133 			free(bufp);
134 			if (i >= MINBUFS)
135 				break;
136 			errexit("cannot allocate buffer pool\n");
137 		}
138 		bp->b_un.b_buf = bufp;
139 		bp->b_prev = &bufhead;
140 		bp->b_next = bufhead.b_next;
141 		bufhead.b_next->b_prev = bp;
142 		bufhead.b_next = bp;
143 		initbarea(bp);
144 	}
145 	bufhead.b_size = i;	/* save number of buffers */
146 }
147 
148 /*
149  * Manage a cache of directory blocks.
150  */
151 struct bufarea *
152 getdatablk(daddr32_t blkno, long size)
153 {
154 	struct bufarea *bp;
155 
156 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
157 		if (bp->b_bno == fsbtodb(&sblock, blkno))
158 			goto foundit;
159 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
160 		if ((bp->b_flags & B_INUSE) == 0)
161 			break;
162 	if (bp == &bufhead)
163 		errexit("deadlocked buffer pool\n");
164 	getblk(bp, blkno, size);
165 	diskreads++;
166 	/* fall through */
167 foundit:
168 	totalreads++;
169 	bp->b_prev->b_next = bp->b_next;
170 	bp->b_next->b_prev = bp->b_prev;
171 	bp->b_prev = &bufhead;
172 	bp->b_next = bufhead.b_next;
173 	bufhead.b_next->b_prev = bp;
174 	bufhead.b_next = bp;
175 	bp->b_flags |= B_INUSE;
176 	return (bp);
177 }
178 
179 void
180 getblk(struct bufarea *bp, daddr32_t blk, long size)
181 {
182 	daddr32_t dblk;
183 
184 	dblk = fsbtodb(&sblock, blk);
185 	if (bp->b_bno != dblk) {
186 		flush(fswritefd, bp);
187 		diskreads++;
188 		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
189 		bp->b_bno = dblk;
190 		bp->b_size = size;
191 	}
192 }
193 
194 void
195 flush(int fd, struct bufarea *bp)
196 {
197 	int i;
198 
199 	if (!bp->b_dirty)
200 		return;
201 	if (bp->b_errs != 0)
202 		pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
203 		    (bp->b_errs == bp->b_size / DEV_BSIZE) ? "" : "PARTIALLY ",
204 		    bp->b_bno);
205 	bp->b_dirty = 0;
206 	bp->b_errs = 0;
207 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
208 	if (bp != &sblk)
209 		return;
210 	for (i = 0; i < sblock.e2fs_ngdb; i++) {
211 		bwrite(fswritefd, (char *)
212 			&sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)],
213 		    fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1),
214 		    sblock.e2fs_bsize);
215 	}
216 }
217 
218 static void
219 rwerror(char *mesg, daddr32_t blk)
220 {
221 
222 	if (preen == 0)
223 		printf("\n");
224 	pfatal("CANNOT %s: BLK %d", mesg, blk);
225 	if (reply("CONTINUE") == 0)
226 		errexit("Program terminated\n");
227 }
228 
229 void
230 ckfini(int markclean)
231 {
232 	struct bufarea *bp, *nbp;
233 	int cnt = 0;
234 
235 	if (fswritefd < 0) {
236 		(void)close(fsreadfd);
237 		return;
238 	}
239 	flush(fswritefd, &sblk);
240 	if (havesb && sblk.b_bno != SBOFF / DEV_BSIZE &&
241 	    !preen && reply("UPDATE STANDARD SUPERBLOCKS")) {
242 		sblk.b_bno = SBOFF / DEV_BSIZE;
243 		sbdirty();
244 		flush(fswritefd, &sblk);
245 		copyback_sb(&asblk);
246 		asblk.b_dirty = 1;
247 		flush(fswritefd, &asblk);
248 	}
249 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
250 		cnt++;
251 		flush(fswritefd, bp);
252 		nbp = bp->b_prev;
253 		free(bp->b_un.b_buf);
254 		free((char *)bp);
255 	}
256 	if (bufhead.b_size != cnt)
257 		errexit("Panic: lost %d buffers\n", bufhead.b_size - cnt);
258 	pbp = pdirbp = (struct bufarea *)0;
259 	if (markclean && (sblock.e2fs.e2fs_state & E2FS_ISCLEAN) == 0) {
260 		/*
261 		 * Mark the file system as clean, and sync the superblock.
262 		 */
263 		if (preen)
264 			pwarn("MARKING FILE SYSTEM CLEAN\n");
265 		else if (!reply("MARK FILE SYSTEM CLEAN"))
266 			markclean = 0;
267 		if (markclean) {
268 			sblock.e2fs.e2fs_state = E2FS_ISCLEAN;
269 			sbdirty();
270 			flush(fswritefd, &sblk);
271 		}
272 	}
273 	if (debug)
274 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
275 		    totalreads, (int)(diskreads * 100 / totalreads));
276 	(void)close(fsreadfd);
277 	(void)close(fswritefd);
278 }
279 
280 int
281 bread(int fd, char *buf, daddr32_t blk, long size)
282 {
283 	char *cp;
284 	int i, errs;
285 	off_t offset;
286 
287 	offset = blk;
288 	offset *= DEV_BSIZE;
289 	if (pread(fd, buf, size, offset) == size)
290 		return (0);
291 	rwerror("READ", blk);
292 	errs = 0;
293 	memset(buf, 0, (size_t)size);
294 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
295 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
296 		if (pread(fd, cp, secsize, offset + i) != secsize) {
297 			if (secsize != DEV_BSIZE)
298 				printf(" %lld (%lld),",
299 				    (long long)(offset + i) / secsize,
300 				    (long long)blk + i / DEV_BSIZE);
301 			else
302 				printf(" %lld,", (long long)blk +
303 				    i / DEV_BSIZE);
304 			errs++;
305 		}
306 	}
307 	printf("\n");
308 	return (errs);
309 }
310 
311 void
312 bwrite(int fd, char *buf, daddr32_t blk, long size)
313 {
314 	int i;
315 	char *cp;
316 	off_t offset;
317 
318 	if (fd < 0)
319 		return;
320 	offset = blk;
321 	offset *= DEV_BSIZE;
322 	if (pwrite(fd, buf, size, offset) == size) {
323 		fsmodified = 1;
324 		return;
325 	}
326 	rwerror("WRITE", blk);
327 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
328 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize)
329 		if (pwrite(fd, cp, secsize, offset + i) != secsize) {
330 			if (secsize != DEV_BSIZE)
331 				printf(" %lld (%lld),",
332 				    (long long)(offset + i) / secsize,
333 				    (long long)blk + i / DEV_BSIZE);
334 			else
335 				printf(" %lld,", (long long)blk +
336 				    i / DEV_BSIZE);
337 		}
338 	printf("\n");
339 	return;
340 }
341 
342 /*
343  * allocate a data block
344  */
345 int
346 allocblk(void)
347 {
348 	int i;
349 
350 	for (i = 0; i < maxfsblock - 1; i++) {
351 		if (testbmap(i))
352 				continue;
353 		setbmap(i);
354 		n_blks ++;
355 		return (i);
356 	}
357 	return (0);
358 }
359 
360 /*
361  * Free a previously allocated block
362  */
363 void
364 freeblk(daddr32_t blkno)
365 {
366 	struct inodesc idesc;
367 
368 	idesc.id_blkno = blkno;
369 	idesc.id_numfrags = 1;
370 	(void)pass4check(&idesc);
371 }
372 
373 /*
374  * Find a pathname
375  */
376 void
377 getpathname(char *namebuf, size_t buflen, ino_t curdir, ino_t ino)
378 {
379 	size_t len;
380 	char *cp;
381 	struct inodesc idesc;
382 	static int busy = 0;
383 
384 	if (curdir == ino && ino == EXT2_ROOTINO) {
385 		(void)strlcpy(namebuf, "/", buflen);
386 		return;
387 	}
388 	if (busy ||
389 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
390 
391 		(void)strlcpy(namebuf, "?", buflen);
392 		return;
393 	}
394 	busy = 1;
395 	memset(&idesc, 0, sizeof(struct inodesc));
396 	idesc.id_type = DATA;
397 	idesc.id_fix = IGNORE;
398 	cp = &namebuf[buflen - 1];
399 	*cp = '\0';
400 	if (curdir != ino) {
401 		idesc.id_parent = curdir;
402 		goto namelookup;
403 	}
404 	while (ino != EXT2_ROOTINO) {
405 		idesc.id_number = ino;
406 		idesc.id_func = findino;
407 		idesc.id_name = "..";
408 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
409 			break;
410 	namelookup:
411 		idesc.id_number = idesc.id_parent;
412 		idesc.id_parent = ino;
413 		idesc.id_func = findname;
414 		idesc.id_name = namebuf;
415 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
416 			break;
417 		len = strlen(namebuf);
418 		cp -= len;
419 		memcpy(cp, namebuf, len);
420 		*(--cp) = '/';
421 		if (cp < &namebuf[EXT2FS_MAXNAMLEN])
422 			break;
423 		ino = idesc.id_number;
424 	}
425 	busy = 0;
426 	if (ino != EXT2_ROOTINO)
427 		*(--cp) = '?';
428 	memcpy(namebuf, cp, (size_t)(&namebuf[buflen] - cp));
429 }
430 
431 /*ARGSUSED*/
432 void
433 catch(int signo)
434 {
435 	ckfini(0);			/* XXX signal race */
436 	_exit(12);
437 }
438 
439 /*
440  * When preening, allow a single quit to signal
441  * a special exit after filesystem checks complete
442  * so that reboot sequence may be interrupted.
443  */
444 /*ARGSUSED*/
445 void
446 catchquit(int signo)
447 {
448 	extern volatile sig_atomic_t returntosingle;
449 	char buf[1024];
450 
451 	snprintf(buf, sizeof buf,
452 	    "returning to single-user after filesystem check\n");
453 	write(STDOUT_FILENO, buf, strlen(buf));
454 	returntosingle = 1;
455 	(void)signal(SIGQUIT, SIG_DFL);
456 }
457 
458 /*
459  * Ignore a single quit signal; wait and flush just in case.
460  * Used by child processes in preen.
461  */
462 /*ARGSUSED*/
463 void
464 voidquit(int signo)
465 {
466 	int save_errno = errno;
467 
468 	sleep(1);
469 	(void)signal(SIGQUIT, SIG_IGN);
470 	(void)signal(SIGQUIT, SIG_DFL);
471 	errno = save_errno;
472 }
473 
474 /*
475  * determine whether an inode should be fixed.
476  */
477 int
478 dofix(struct inodesc *idesc, char *msg)
479 {
480 
481 	switch (idesc->id_fix) {
482 
483 	case DONTKNOW:
484 		if (idesc->id_type == DATA)
485 			direrror(idesc->id_number, msg);
486 		else
487 			pwarn("%s", msg);
488 		if (preen) {
489 			printf(" (SALVAGED)\n");
490 			idesc->id_fix = FIX;
491 			return (ALTERED);
492 		}
493 		if (reply("SALVAGE") == 0) {
494 			idesc->id_fix = NOFIX;
495 			return (0);
496 		}
497 		idesc->id_fix = FIX;
498 		return (ALTERED);
499 
500 	case FIX:
501 		return (ALTERED);
502 
503 	case NOFIX:
504 	case IGNORE:
505 		return (0);
506 
507 	default:
508 		errexit("UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
509 	}
510 	/* NOTREACHED */
511 }
512