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