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