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