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