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