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