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