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