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