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