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